Skip to content

Commit 0727528

Browse files
The threadsafety.py addon now flags MT-Unsafe symbols and functions. (cppcheck-opensource#5086)
1 parent 520a664 commit 0727528

6 files changed

Lines changed: 643 additions & 23 deletions

File tree

.github/workflows/CI-unixish.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,8 @@ jobs:
378378
# TODO: move to scriptcheck.yml so these are tested with all Python versions?
379379
- name: Test addons
380380
run: |
381-
./cppcheck --addon=threadsafety addons/test/threadsafety
382-
./cppcheck --addon=threadsafety --std=c++03 addons/test/threadsafety
381+
./cppcheck --error-exitcode=1 --inline-suppr --addon=threadsafety addons/test/threadsafety
382+
./cppcheck --error-exitcode=1 --inline-suppr --addon=threadsafety --std=c++03 addons/test/threadsafety
383383
./cppcheck --addon=misra --enable=style --inline-suppr --enable=information --error-exitcode=1 addons/test/misra/misra-ctu-*-test.c
384384
pushd addons/test
385385
# We'll force C89 standard to enable an additional verification for
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2023 Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
/*
20+
* This does not match the standard cppchek test code,
21+
* because I haven't figured that out yet.
22+
* This code does compile and run, and does demonstrate
23+
* the issues that the threadsafety.py addon is supposed to find.
24+
* It does not use threads !
25+
*/
26+
27+
#include <stdio.h>
28+
#include <time.h>
29+
#include <string.h>
30+
31+
void threadsafety_static()
32+
{
33+
// cppcheck-suppress threadsafety-threadsafety
34+
static unsigned int nCount = 0;
35+
36+
nCount++;
37+
printf("%d\n", nCount);
38+
}
39+
40+
void threadsafety_call()
41+
{
42+
time_t now = time(nullptr);
43+
// cppcheck-suppress threadsafety-unsafe-call
44+
printf("%s\n", ctime(&now));
45+
}
46+
47+
// cppcheck --addon=threadsafety
48+
// should *not* find any problems with this function.
49+
void threadsafety_safecall()
50+
{
51+
char haystack[] = "alphabet";
52+
char needle[] = "Alph";
53+
char* found = strcasestr(haystack, needle);
54+
printf("%s %sin %s\n", needle, found ? "" : "not ", haystack);
55+
}
56+
57+
int main() {
58+
threadsafety_static();
59+
60+
threadsafety_call();
61+
62+
threadsafety_safecall();
63+
64+
threadsafety_static();
65+
66+
return 0;
67+
}

addons/test/threadsafety/local_static.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ struct Dummy {
22
int x;
33
};
44
void func() {
5-
static Dummy dummy;
5+
// cppcheck-suppress threadsafety-threadsafety
6+
static Dummy dummy;
67
}

addons/test/threadsafety/local_static_const.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ struct Dummy {
22
int x;
33
};
44
void func() {
5+
// cppcheck-suppress threadsafety-threadsafety-const
56
static const Dummy dummy;
67
}

0 commit comments

Comments
 (0)