From: Ivan Maidanski Date: Tue, 28 Nov 2017 08:06:36 +0000 (+0300) Subject: Eliminate data race in cons() of test_malloc X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=67b83ba4160cda9b07ce295a8b5aadb10e1474ec;p=libatomic_ops Eliminate data race in cons() of test_malloc (fix commit c058d9d) The data race has not affected the functionality of the test but this commit allows to avoid no_sanitize attribute for the function. * tests/test_malloc.c (cons): Remove AO_ATTR_NO_SANITIZE_THREAD attribute; Do not reset extra (use "%" operator to get my_extra value). * tests/test_malloc.c [AO_HAVE_fetch_and_add1] (cons): Change type of extra to AO_t, add volatile qualifier; use AO_fetch_and_add1 instead of "++" operator to update extra. --- diff --git a/tests/test_malloc.c b/tests/test_malloc.c index a322099..2735e6b 100644 --- a/tests/test_malloc.c +++ b/tests/test_malloc.c @@ -68,19 +68,19 @@ typedef struct list_node { int data; } ln; -AO_ATTR_NO_SANITIZE_THREAD ln *cons(int d, ln *tail) { - static size_t extra = 0; /* data race in extra is OK */ - size_t my_extra = extra; +# ifdef AO_HAVE_fetch_and_add1 + static volatile AO_t extra = 0; + size_t my_extra = (size_t)AO_fetch_and_add1(&extra) % 101; +# else + static size_t extra = 0; /* data race in extra is OK */ + size_t my_extra = (extra++) % 101; +# endif ln *result; int * extras; unsigned i; - if (my_extra > 100) - extra = my_extra = 0; - else - ++extra; result = AO_malloc(sizeof(ln) + sizeof(int)*my_extra); if (result == 0) {