Skip to content

Commit

Permalink
tests/optional_mutex: check counter within a lock
Browse files Browse the repository at this point in the history
The variables "value" and "counter" are modified within mutex locks, so
it's good "lock hygiene" to do the same for the final check.

Reported by:	Coverity scan
  • Loading branch information
gperciva committed Feb 7, 2024
1 parent f70da0e commit 8a14c0a
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion tests/optional_mutex/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,38 @@ test_pthread(void)
}
#endif

/* Return 0 if everything matches; 1 if they do not match; -1 on error. */
static int
check_counter(int counter_expected)
{
int rc, match;

/* 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))
match = 0;
else
match = 1;

/* Unlock if we're using pthread. */
if ((rc = optional_mutex_unlock(&mutex)) != 0) {
warn0("optional_mutex_unlock: %s", strerror(rc));
goto err0;
}

/* Success! */
return (match);

err0:
/* Failure! */
return (-1);
}

int
main(int argc, char * argv[])
{
Expand All @@ -113,7 +145,7 @@ main(int argc, char * argv[])
#endif

/* Check the counter. */
if ((value != 0) || (counter != counter_expected))
if (check_counter(counter_expected))
goto err0;

/* Success! */
Expand Down

0 comments on commit 8a14c0a

Please sign in to comment.