diff --git a/cachedb/cachedb.c b/cachedb/cachedb.c index 4e26c837e..b4f2bda0f 100644 --- a/cachedb/cachedb.c +++ b/cachedb/cachedb.c @@ -61,6 +61,8 @@ /** the unit test testframe for cachedb, its module state contains * a cache for a couple queries (in memory). */ struct testframe_moddata { + /** lock for mutex */ + lock_basic_type lock; /** key for single stored data element, NULL if none */ char* stored_key; /** data for single stored data element, NULL if none */ @@ -72,14 +74,17 @@ struct testframe_moddata { static int testframe_init(struct module_env* env, struct cachedb_env* cachedb_env) { + struct testframe_moddata* d; (void)env; verbose(VERB_ALGO, "testframe_init"); - cachedb_env->backend_data = (void*)calloc(1, + d = (struct testframe_moddata*)calloc(1, sizeof(struct testframe_moddata)); + cachedb_env->backend_data = (void*)d; if(!cachedb_env->backend_data) { log_err("out of memory"); return 0; } + lock_basic_init(&d->lock); return 1; } @@ -92,6 +97,7 @@ testframe_deinit(struct module_env* env, struct cachedb_env* cachedb_env) verbose(VERB_ALGO, "testframe_deinit"); if(!d) return; + lock_basic_destroy(&d->lock); free(d->stored_key); free(d->stored_data); free(d); @@ -105,17 +111,22 @@ testframe_lookup(struct module_env* env, struct cachedb_env* cachedb_env, cachedb_env->backend_data; (void)env; verbose(VERB_ALGO, "testframe_lookup of %s", key); + lock_basic_lock(&d->lock); if(d->stored_key && strcmp(d->stored_key, key) == 0) { - if(d->stored_datalen > sldns_buffer_capacity(result_buffer)) + if(d->stored_datalen > sldns_buffer_capacity(result_buffer)) { + lock_basic_unlock(&d->lock); return 0; /* too large */ + } verbose(VERB_ALGO, "testframe_lookup found %d bytes", (int)d->stored_datalen); sldns_buffer_clear(result_buffer); sldns_buffer_write(result_buffer, d->stored_data, d->stored_datalen); sldns_buffer_flip(result_buffer); + lock_basic_unlock(&d->lock); return 1; } + lock_basic_unlock(&d->lock); return 0; } @@ -126,6 +137,7 @@ testframe_store(struct module_env* env, struct cachedb_env* cachedb_env, struct testframe_moddata* d = (struct testframe_moddata*) cachedb_env->backend_data; (void)env; + lock_basic_lock(&d->lock); verbose(VERB_ALGO, "testframe_store %s (%d bytes)", key, (int)data_len); /* free old data element (if any) */ @@ -137,6 +149,7 @@ testframe_store(struct module_env* env, struct cachedb_env* cachedb_env, d->stored_data = memdup(data, data_len); if(!d->stored_data) { + lock_basic_unlock(&d->lock); log_err("out of memory"); return; } @@ -146,8 +159,10 @@ testframe_store(struct module_env* env, struct cachedb_env* cachedb_env, free(d->stored_data); d->stored_data = NULL; d->stored_datalen = 0; + lock_basic_unlock(&d->lock); return; } + lock_basic_unlock(&d->lock); /* (key,data) successfully stored */ } diff --git a/doc/Changelog b/doc/Changelog index e4c323679..d5fc4c906 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -1,3 +1,6 @@ +31 August 2017: Wouter + - Fix #1424: cachedb:testframe is not thread safe. + 30 August 2017: Wouter - updated contrib/fastrpz.patch to apply with configparser changes. - Fix 1416: qname-minimisation breaks TLSA lookups with CNAMEs.