- Fix #1424: cachedb:testframe is not thread safe.

git-svn-id: file:///svn/unbound/trunk@4323 be551aaa-1e26-0410-a405-d3ace91eadb9
This commit is contained in:
Wouter Wijngaards 2017-08-31 07:35:08 +00:00
parent fe18bbcb1f
commit 79c45131d1
2 changed files with 20 additions and 2 deletions

View file

@ -61,6 +61,8 @@
/** the unit test testframe for cachedb, its module state contains /** the unit test testframe for cachedb, its module state contains
* a cache for a couple queries (in memory). */ * a cache for a couple queries (in memory). */
struct testframe_moddata { struct testframe_moddata {
/** lock for mutex */
lock_basic_type lock;
/** key for single stored data element, NULL if none */ /** key for single stored data element, NULL if none */
char* stored_key; char* stored_key;
/** data for single stored data element, NULL if none */ /** data for single stored data element, NULL if none */
@ -72,14 +74,17 @@ struct testframe_moddata {
static int static int
testframe_init(struct module_env* env, struct cachedb_env* cachedb_env) testframe_init(struct module_env* env, struct cachedb_env* cachedb_env)
{ {
struct testframe_moddata* d;
(void)env; (void)env;
verbose(VERB_ALGO, "testframe_init"); verbose(VERB_ALGO, "testframe_init");
cachedb_env->backend_data = (void*)calloc(1, d = (struct testframe_moddata*)calloc(1,
sizeof(struct testframe_moddata)); sizeof(struct testframe_moddata));
cachedb_env->backend_data = (void*)d;
if(!cachedb_env->backend_data) { if(!cachedb_env->backend_data) {
log_err("out of memory"); log_err("out of memory");
return 0; return 0;
} }
lock_basic_init(&d->lock);
return 1; return 1;
} }
@ -92,6 +97,7 @@ testframe_deinit(struct module_env* env, struct cachedb_env* cachedb_env)
verbose(VERB_ALGO, "testframe_deinit"); verbose(VERB_ALGO, "testframe_deinit");
if(!d) if(!d)
return; return;
lock_basic_destroy(&d->lock);
free(d->stored_key); free(d->stored_key);
free(d->stored_data); free(d->stored_data);
free(d); free(d);
@ -105,17 +111,22 @@ testframe_lookup(struct module_env* env, struct cachedb_env* cachedb_env,
cachedb_env->backend_data; cachedb_env->backend_data;
(void)env; (void)env;
verbose(VERB_ALGO, "testframe_lookup of %s", key); 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_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 */ return 0; /* too large */
}
verbose(VERB_ALGO, "testframe_lookup found %d bytes", verbose(VERB_ALGO, "testframe_lookup found %d bytes",
(int)d->stored_datalen); (int)d->stored_datalen);
sldns_buffer_clear(result_buffer); sldns_buffer_clear(result_buffer);
sldns_buffer_write(result_buffer, d->stored_data, sldns_buffer_write(result_buffer, d->stored_data,
d->stored_datalen); d->stored_datalen);
sldns_buffer_flip(result_buffer); sldns_buffer_flip(result_buffer);
lock_basic_unlock(&d->lock);
return 1; return 1;
} }
lock_basic_unlock(&d->lock);
return 0; return 0;
} }
@ -126,6 +137,7 @@ testframe_store(struct module_env* env, struct cachedb_env* cachedb_env,
struct testframe_moddata* d = (struct testframe_moddata*) struct testframe_moddata* d = (struct testframe_moddata*)
cachedb_env->backend_data; cachedb_env->backend_data;
(void)env; (void)env;
lock_basic_lock(&d->lock);
verbose(VERB_ALGO, "testframe_store %s (%d bytes)", key, (int)data_len); verbose(VERB_ALGO, "testframe_store %s (%d bytes)", key, (int)data_len);
/* free old data element (if any) */ /* 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); d->stored_data = memdup(data, data_len);
if(!d->stored_data) { if(!d->stored_data) {
lock_basic_unlock(&d->lock);
log_err("out of memory"); log_err("out of memory");
return; return;
} }
@ -146,8 +159,10 @@ testframe_store(struct module_env* env, struct cachedb_env* cachedb_env,
free(d->stored_data); free(d->stored_data);
d->stored_data = NULL; d->stored_data = NULL;
d->stored_datalen = 0; d->stored_datalen = 0;
lock_basic_unlock(&d->lock);
return; return;
} }
lock_basic_unlock(&d->lock);
/* (key,data) successfully stored */ /* (key,data) successfully stored */
} }

View file

@ -1,3 +1,6 @@
31 August 2017: Wouter
- Fix #1424: cachedb:testframe is not thread safe.
30 August 2017: Wouter 30 August 2017: Wouter
- updated contrib/fastrpz.patch to apply with configparser changes. - updated contrib/fastrpz.patch to apply with configparser changes.
- Fix 1416: qname-minimisation breaks TLSA lookups with CNAMEs. - Fix 1416: qname-minimisation breaks TLSA lookups with CNAMEs.