From b3d47f0a225d047452acc1201622507265167201 Mon Sep 17 00:00:00 2001 From: Graham Percival Date: Sun, 14 Jan 2024 10:11:25 -0800 Subject: [PATCH] tests/optional_mutex: check counter within a lock The variables "value" and "counter" are modified within mutex locks, so it makes sense for the final check to also be within a mutex lock. However, the final check happens after pthread_join(), so there isn't any danger of synchronization problems. Reported by: Coverity scan --- tests/optional_mutex/main.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/tests/optional_mutex/main.c b/tests/optional_mutex/main.c index fbe5248e..6aed98da 100644 --- a/tests/optional_mutex/main.c +++ b/tests/optional_mutex/main.c @@ -95,6 +95,32 @@ test_pthread(void) } #endif +static int +check_counter(int counter_expected) +{ + int rc = 0; + + /* Lock if we're using pthread. */ + if ((rc = optional_mutex_lock(&mutex)) != 0) { + warn0("optional_mutex_lock: %s", strerror(rc)); + goto err0; + } + + /* Check the value and counter. */ + if ((value != 0) || (counter != counter_expected)) + rc = -1; + + /* Unlock if we're using pthread. */ + if ((rc = optional_mutex_unlock(&mutex)) != 0) { + warn0("optional_mutex_unlock: %s", strerror(rc)); + goto err0; + } + +err0: + /* Success or failure! */ + return (rc); +} + int main(int argc, char * argv[]) { @@ -112,8 +138,8 @@ main(int argc, char * argv[]) inc_dec_counter(NULL); #endif - /* Check the counter. */ - if ((value != 0) || (counter != counter_expected)) + /* Check that the counter has the expected value. */ + if (check_counter(counter_expected)) goto err0; /* Success! */