The resman_ctx pointer was accessed outside of its guarding mutex. Also make the ResourceManager static.
Unified · Split
| @@ -60,7 +60,7 @@ typedef struct ResourceManagerContext { · 59 unmodified lines | |||
| 60 | 60 | ||
| 61 | static AVMutex mutex = AV_MUTEX_INITIALIZER; | 61 | static AVMutex mutex = AV_MUTEX_INITIALIZER; |
| 62 | 62 | ||
| 63 | ResourceManagerContext *resman_ctx = NULL; | 63 | static ResourceManagerContext resman_ctx = { .class = &resman_class }; |
| 64 | 64 | ||
| 65 | 65 | ||
| 66 | #if CONFIG_RESOURCE_COMPRESSION | 66 | #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 | #endif | 118 | #endif |
| 119 | 119 | ||
| 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 | |||
| 144 | void ff_resman_uninit(void) | 120 | void 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 | ||
| 158 | char *ff_resman_get_string(FFResourceId resource_id) | 130 | char *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) { |