]> git.ipfire.org Git - thirdparty/unbound.git/commitdiff
- Fix #1424: cachedb:testframe is not thread safe.
authorWouter Wijngaards <wouter@nlnetlabs.nl>
Thu, 31 Aug 2017 07:35:08 +0000 (07:35 +0000)
committerWouter Wijngaards <wouter@nlnetlabs.nl>
Thu, 31 Aug 2017 07:35:08 +0000 (07:35 +0000)
git-svn-id: file:///svn/unbound/trunk@4323 be551aaa-1e26-0410-a405-d3ace91eadb9

cachedb/cachedb.c
doc/Changelog

index 4e26c837e487462ec91cc89bd1d2fa54c577f049..b4f2bda0f2cce95b0a562d063bca29ab1c682afa 100644 (file)
@@ -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 */
 }
 
index e4c32367917e4d692c1548f5157dc6146f89597c..d5fc4c9068a6fe4ee7b31a46686cc6d4a7ae37bf 100644 (file)
@@ -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.