Skip to content

Commit a8284d5

Browse files
committed
C++: Add mutex test case.
1 parent e8d7925 commit a8284d5

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

cpp/ql/test/query-tests/Security/CWE/CWE-764/semmle/tests/UnreleasedLock.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
| test.cpp:303:11:303:18 | call to try_lock | This lock might not be unlocked or might be locked more times than it is unlocked. |
88
| test.cpp:313:11:313:18 | call to try_lock | This lock might not be unlocked or might be locked more times than it is unlocked. |
99
| test.cpp:442:8:442:17 | call to mutex_lock | This lock might not be unlocked or might be locked more times than it is unlocked. |
10+
| test.cpp:482:2:482:19 | call to pthread_mutex_lock | This lock might not be unlocked or might be locked more times than it is unlocked. |

cpp/ql/test/query-tests/Security/CWE/CWE-764/semmle/tests/test.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,3 +445,46 @@ bool test_mutex(data_t *data)
445445

446446
return true;
447447
}
448+
449+
// ---
450+
451+
struct pthread_mutex
452+
{
453+
// ...
454+
};
455+
456+
void pthread_mutex_lock(pthread_mutex *m);
457+
void pthread_mutex_unlock(pthread_mutex *m);
458+
459+
class MyClass
460+
{
461+
public:
462+
pthread_mutex lock;
463+
};
464+
465+
bool maybe();
466+
467+
int test_MyClass_good(MyClass *obj)
468+
{
469+
pthread_mutex_lock(&obj->lock);
470+
471+
if (maybe()) {
472+
pthread_mutex_unlock(&obj->lock);
473+
return -1; // GOOD
474+
}
475+
476+
pthread_mutex_unlock(&obj->lock); // GOOD
477+
return 0;
478+
}
479+
480+
int test_MyClass_bad(MyClass *obj)
481+
{
482+
pthread_mutex_lock(&obj->lock);
483+
484+
if (maybe()) {
485+
return -1; // BAD
486+
}
487+
488+
pthread_mutex_unlock(&obj->lock); // GOOD
489+
return 0;
490+
}

0 commit comments

Comments
 (0)