History

fftools/resources/resman: Don't alloc ResourceManager, fix race

The resman_ctx pointer was accessed outside of its guarding mutex. Also make the ResourceManager static.

Andreas Rheinhardt committed 1 year ago

Commit 06cd9081 parent b4c53971 file · +3 −34Browse files

Unified · Split

fftools/resources/resman.c+3−34
@@ -60,7 +60,7 @@ typedef struct ResourceManagerContext { · 59 unmodified lines
6060
6161 static AVMutex mutex = AV_MUTEX_INITIALIZER;
6262
63−ResourceManagerContext *resman_ctx = NULL;
63+static ResourceManagerContext resman_ctx = { .class = &resman_class };
6464
6565
6666 #if CONFIG_RESOURCE_COMPRESSION
@@ -117,39 +117,11 @@ static int decompress_gzip(ResourceManagerContext *ctx, uint8_t *in, unsigned in · 50 unmodified lines
117117 }
118118 #endif
119119
120−static ResourceManagerContext *get_resman_context(void)
121−{
122− ResourceManagerContext *res = resman_ctx;
123−
124− ff_mutex_lock(&mutex);
125−
126− if (res)
127− goto end;
128−
129− res = av_mallocz(sizeof(ResourceManagerContext));
130− if (!res) {
131− av_log(NULL, AV_LOG_ERROR, "Failed to allocate resource manager context\n");
132− goto end;
133− }
134−
135− res->class = &resman_class;
136− resman_ctx = res;
137−
138−end:
139− ff_mutex_unlock(&mutex);
140− return res;
141−}
142−
143−
144120 void ff_resman_uninit(void)
145121 {
146122 ff_mutex_lock(&mutex);
147123
148− if (resman_ctx) {
149− if (resman_ctx->resource_dic)
150− av_dict_free(&resman_ctx->resource_dic);
151− av_freep(&resman_ctx);
152− }
124+ av_dict_free(&resman_ctx.resource_dic);
153125
154126 ff_mutex_unlock(&mutex);
155127 }
@@ -157,14 +129,11 @@ void ff_resman_uninit(void) · 1 unmodified line
157129
158130 char *ff_resman_get_string(FFResourceId resource_id)
159131 {
160− ResourceManagerContext *ctx = get_resman_context();
132+ ResourceManagerContext *ctx = &resman_ctx;
161133 FFResourceDefinition resource_definition = { 0 };
162134 AVDictionaryEntry *dic_entry;
163135 char *res = NULL;
164136
165− if (!ctx)
166− return NULL;
167−
168137 for (unsigned i = 0; i < FF_ARRAY_ELEMS(resource_definitions); ++i) {
169138 FFResourceDefinition def = resource_definitions[i];
170139 if (def.resource_id == resource_id) {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t