Skip to content

Commit

Permalink
Fix Coverity LOCK_EVASION error
Browse files Browse the repository at this point in the history
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.
```
  • Loading branch information
sophimao committed Aug 21, 2024
1 parent 15f1e3a commit 10bd4e6
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/acl_threadsupport/test/acl_threadsupport_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ void *test_thread(void *) {
break;
acl_thread_yield();
}
if (threadtest_state == END_STATE) {
acl_mutex_lock(&mymutex);
const bool is_end_state = (threadtest_state == END_STATE);
acl_mutex_unlock(&mymutex);
if (is_end_state) {
return 0;
}
// CLIENT_STATE
Expand Down

0 comments on commit 10bd4e6

Please sign in to comment.