]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Convert global variables to atomic to prevent possible data race
authorOndřej Surý <ondrej@sury.org>
Mon, 1 Jul 2019 13:19:29 +0000 (15:19 +0200)
committerOndřej Surý <ondrej@sury.org>
Wed, 2 Oct 2019 10:09:44 +0000 (12:09 +0200)
lib/dns/tests/dispatch_test.c

index 9785fbfbae8caf529179d7c3645b1cd441b4cce5..3467fa6923949d32c4f1024631408a9d7d15db4f 100644 (file)
@@ -27,6 +27,7 @@
 
 #include <isc/app.h>
 #include <isc/buffer.h>
+#include <isc/refcount.h>
 #include <isc/socket.h>
 #include <isc/task.h>
 #include <isc/timer.h>
@@ -212,27 +213,20 @@ nameserver(isc_task_t *task, isc_event_t *event) {
 
 static dns_dispatch_t *dispatch = NULL;
 static dns_dispentry_t *dispentry = NULL;
-static bool first = true;
-static isc_mutex_t lock;
+static atomic_bool first = ATOMIC_VAR_INIT(true);
 static isc_sockaddr_t local;
-static unsigned int responses = 0;
+static isc_refcount_t responses;
 
 static void
 response(isc_task_t *task, isc_event_t *event) {
        dns_dispatchevent_t *devent = (dns_dispatchevent_t *)event;
-       isc_result_t result;
-       bool wasfirst;
+       bool exp_true = true;
 
        UNUSED(task);
 
-       LOCK(&lock);
-       wasfirst = first;
-       first = false;
-       responses++;
-       UNLOCK(&lock);
-
-       if (wasfirst) {
-               result = dns_dispatch_getnext(dispentry, &devent);
+       isc_refcount_increment(&responses);
+       if (atomic_compare_exchange_strong(&first, &exp_true, false)) {
+               isc_result_t result = dns_dispatch_getnext(dispentry, &devent);
                assert_int_equal(result, ISC_R_SUCCESS);
        } else {
                dns_dispatch_removeresponse(&dispentry, &devent);
@@ -267,7 +261,7 @@ dispatch_getnext(void **state) {
 
        UNUSED(state);
 
-       isc_mutex_init(&lock);
+       isc_refcount_init(&responses, 0);
 
        result = isc_task_create(taskmgr, 0, &task);
        assert_int_equal(result, ISC_R_SUCCESS);
@@ -298,7 +292,6 @@ dispatch_getnext(void **state) {
        result = isc_socket_getsockname(sock, &local);
        assert_int_equal(result, ISC_R_SUCCESS);
 
-       first = true;
        region.base = rbuf;
        region.length = sizeof(rbuf);
        result = isc_socket_recv(sock, &region, 1, task, nameserver, sock);
@@ -320,7 +313,7 @@ dispatch_getnext(void **state) {
        result = isc_app_run();
        assert_int_equal(result, ISC_R_SUCCESS);
 
-       assert_int_equal(responses, 2);
+       assert_int_equal(isc_refcount_current(&responses), 2);
 
        /*
         * Shutdown nameserver.