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
60 60
61static AVMutex mutex = AV_MUTEX_INITIALIZER;61static AVMutex mutex = AV_MUTEX_INITIALIZER;
62 62
63ResourceManagerContext *resman_ctx = NULL;63static ResourceManagerContext resman_ctx = { .class = &resman_class };
64 64
65 65
66#if CONFIG_RESOURCE_COMPRESSION66#if CONFIG_RESOURCE_COMPRESSION
@@ -117,39 +117,11 @@ static int decompress_gzip(ResourceManagerContext *ctx, uint8_t *in, unsigned in · 50 unmodified lines
117}117}
118#endif118#endif
119 119
120static 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
138end:
139 ff_mutex_unlock(&mutex);
140 return res;
141}
142
143
144void ff_resman_uninit(void)120void ff_resman_uninit(void)
145{121{
146 ff_mutex_lock(&mutex);122 ff_mutex_lock(&mutex);
147 123
148 if (resman_ctx) {124 av_dict_free(&resman_ctx.resource_dic);
149 if (resman_ctx->resource_dic)
150 av_dict_free(&resman_ctx->resource_dic);
151 av_freep(&resman_ctx);
152 }
153 125
154 ff_mutex_unlock(&mutex);126 ff_mutex_unlock(&mutex);
155}127}
@@ -157,14 +129,11 @@ void ff_resman_uninit(void) · 1 unmodified line
157 129
158char *ff_resman_get_string(FFResourceId resource_id)130char *ff_resman_get_string(FFResourceId resource_id)
159{131{
160 ResourceManagerContext *ctx = get_resman_context();132 ResourceManagerContext *ctx = &resman_ctx;
161 FFResourceDefinition resource_definition = { 0 };133 FFResourceDefinition resource_definition = { 0 };
162 AVDictionaryEntry *dic_entry;134 AVDictionaryEntry *dic_entry;
163 char *res = NULL;135 char *res = NULL;
164 136
165 if (!ctx)
166 return NULL;
167
168 for (unsigned i = 0; i < FF_ARRAY_ELEMS(resource_definitions); ++i) {137 for (unsigned i = 0; i < FF_ARRAY_ELEMS(resource_definitions); ++i) {
169 FFResourceDefinition def = resource_definitions[i];138 FFResourceDefinition def = resource_definitions[i];
170 if (def.resource_id == resource_id) {139 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