Skip to content

Commit 50dbcc3

Browse files
committed
Fix Coverity LOCK_EVASION error
Fixes: ``` lib/acl_threadsupport/test/acl_threadsupport_test.cpp:111:5: Type: Check of thread-shared field evades lock acquisition (LOCK_EVASION) lib/acl_threadsupport/test/acl_threadsupport_test.cpp:106:9: 1. thread1_checks_field: Thread1 uses the value read from static field "threadtest_state" in the condition "threadtest_state == 2". It sees that the condition is false. Control is switched to Thread2. lib/acl_threadsupport/test/acl_threadsupport_test.cpp:106:9: 2. thread2_checks_field: Thread2 uses the value read from static field "threadtest_state" in the condition "threadtest_state == 2". It sees that the condition is false. lib/acl_threadsupport/test/acl_threadsupport_test.cpp:110:5: 3. thread2_acquires_lock: Thread2 acquires lock "mymutex". lib/acl_threadsupport/src/acl_threadsupport.c:100:42: 3.1. lock: "pthread_mutex_lock" locks "mutex". lib/acl_threadsupport/test/acl_threadsupport_test.cpp:111:5: 4. thread2_modifies_field: Thread2 sets "threadtest_state" to a new value. Note that this write can be reordered at runtime to occur before instructions that do not access this field within this locked region. After Thread2 leaves the critical section, control is switched back to Thread1. lib/acl_threadsupport/test/acl_threadsupport_test.cpp:110:5: 5. thread1_acquires_lock: Thread1 acquires lock "mymutex". lib/acl_threadsupport/src/acl_threadsupport.c:100:42: 5.1. lock: "pthread_mutex_lock" locks "mutex". lib/acl_threadsupport/test/acl_threadsupport_test.cpp:111:5: 6. thread1_overwrites_value_in_field: Thread1 sets "threadtest_state" to a new value. Now the two threads have an inconsistent view of "threadtest_state" and updates to fields correlated with "threadtest_state" may be lost. lib/acl_threadsupport/test/acl_threadsupport_test.cpp:106:9: 7. use_same_locks_for_read_and_modify: Guard the modification of "threadtest_state" and the read used to decide whether to modify "threadtest_state" with the same set of locks. ```
1 parent a3dd066 commit 50dbcc3

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

lib/acl_threadsupport/test/acl_threadsupport_test.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ void *test_thread(void *) {
103103
break;
104104
acl_thread_yield();
105105
}
106-
if (threadtest_state == END_STATE) {
106+
acl_mutex_lock(&mymutex);
107+
const bool is_end_state = (threadtest_state == END_STATE);
108+
acl_mutex_unlock(&mymutex);
109+
if (is_end_state) {
107110
return 0;
108111
}
109112
// CLIENT_STATE

0 commit comments

Comments
 (0)