Skip to content

Commit b7f9367

Browse files
committed
test: Upgrade cppcheck, fix some warnings.
Also started teaching it about toxcore's alloc/dealloc functions in hopes of it catching some errors (it doesn't seem to be very good at this, but maybe better than nothing?).
1 parent 766e62b commit b7f9367

31 files changed

+229
-71
lines changed

.circleci/config.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,13 @@ jobs:
134134
- run:
135135
apt-get install -y --no-install-recommends
136136
ca-certificates
137-
cppcheck
138137
g++
139138
llvm-dev
140139
- checkout
141140
- run: git submodule update --init --recursive
142141
- run: other/analysis/check_includes
143142
- run: other/analysis/check_logger_levels
144143
- run: other/analysis/run-clang
145-
- run: other/analysis/run-cppcheck
146144
- run: other/analysis/run-gcc
147145

148146
clang-analyze:

.github/workflows/ci.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ jobs:
1313
common:
1414
uses: TokTok/ci-tools/.github/workflows/common-ci.yml@master
1515

16+
cppcheck:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- name: Set up Docker Buildx
20+
uses: docker/setup-buildx-action@v3
21+
- name: Docker Build
22+
uses: docker/build-push-action@v4
23+
with:
24+
file: other/docker/cppcheck/Dockerfile
25+
1626
mypy:
1727
runs-on: ubuntu-latest
1828
steps:

other/analysis/run-cppcheck

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ set -e
88

99
CPPCHECK=("--enable=all")
1010
CPPCHECK+=("--inconclusive")
11+
CPPCHECK+=("--check-level=exhaustive")
12+
CPPCHECK+=("--inline-suppr")
13+
CPPCHECK+=("--library=other/docker/cppcheck/toxcore.cfg")
1114
CPPCHECK+=("--error-exitcode=1")
12-
# Used for VLA.
13-
CPPCHECK+=("--suppress=allocaCalled")
15+
# We don't cast function pointers, which cppcheck suggests here.
16+
CPPCHECK+=("--suppress=constParameterCallback")
1417
# False positives in switch statements.
1518
CPPCHECK+=("--suppress=knownConditionTrueFalse")
1619
# Cppcheck does not need standard library headers to get proper results.
@@ -19,27 +22,22 @@ CPPCHECK+=("--suppress=missingIncludeSystem")
1922
CPPCHECK+=("--suppress=signConversion")
2023
# TODO(iphydf): Fixed in the toxav refactor PR.
2124
CPPCHECK+=("--suppress=redundantAssignment")
22-
# We have some redundant nullptr checks in assertions
23-
CPPCHECK+=("--suppress=nullPointerRedundantCheck")
24-
# Triggers a false warning in group.c
25-
CPPCHECK+=("--suppress=AssignmentAddressToInteger")
26-
# TODO(sudden6): This triggers a false positive, check again later to enable it
27-
CPPCHECK+=("--suppress=arrayIndexOutOfBoundsCond")
28-
29-
# We're a library. This only works on whole programs.
30-
CPPCHECK_C=("--suppress=unusedFunction")
3125

26+
# We use this for VLAs.
27+
CPPCHECK_CXX+=("--suppress=allocaCalled")
3228
# False positive in auto_tests.
33-
CPPCHECK_CXX+=("--suppress=shadowArgument")
3429
CPPCHECK_CXX+=("--suppress=shadowFunction")
35-
# False positive for callback functions
36-
CPPCHECK_CXX+=("--suppress=constParameter")
30+
# False positive in group.c.
31+
# Using cppcheck-suppress claims the suppression is unused.
32+
CPPCHECK_CXX+=("--suppress=AssignmentAddressToInteger")
33+
# We use C style casts because we write C code.
34+
CPPCHECK_CXX+=("--suppress=cstyleCast")
3735
# Used in Messenger.c for a static_assert(...)
3836
CPPCHECK_CXX+=("--suppress=sizeofFunctionCall")
3937

4038
run() {
4139
echo "Running cppcheck in variant '$*'"
42-
cppcheck "${CPPCHECK[@]}" "${CPPCHECK_C[@]}" tox*/*.[ch] tox*/*/*.[ch] "${CPPFLAGS[@]}" "$@"
40+
cppcheck -j8 "${CPPCHECK[@]}" "${CPPCHECK_C[@]}" tox*/*.[ch] tox*/*/*.[ch] "${CPPFLAGS[@]}" "$@"
4341
cppcheck "${CPPCHECK[@]}" "${CPPCHECK_CXX[@]}" amalgamation.cc "${CPPFLAGS[@]}" "$@"
4442
}
4543

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
f0bff9fe04d56543d95a457afd76c618139eef99a4302337c66d07759d108e8b /usr/local/bin/tox-bootstrapd
1+
68432689967d06dd144e5cdfe37751ccc62b2aa85b73a9cc55aff3732dc47fde /usr/local/bin/tox-bootstrapd

other/bootstrap_daemon/src/tox-bootstrapd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static void sleep_milliseconds(uint32_t ms)
5757
// returns 1 on success
5858
// 0 on failure - no keys were read or stored
5959

60-
static int manage_keys(DHT *dht, char *keys_file_path)
60+
static int manage_keys(DHT *dht, const char *keys_file_path)
6161
{
6262
enum { KEYS_SIZE = CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SECRET_KEY_SIZE };
6363
uint8_t keys[KEYS_SIZE];

other/docker/cppcheck/Dockerfile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
FROM alpine:3.19.0
2+
3+
RUN ["apk", "add", "--no-cache", \
4+
"bash", \
5+
"cppcheck", \
6+
"findutils", \
7+
"libconfig-dev", \
8+
"libsodium-dev", \
9+
"libvpx-dev", \
10+
"linux-headers", \
11+
"make", \
12+
"opus-dev"]
13+
14+
COPY other/bootstrap_daemon/ /src/workspace/c-toxcore/other/bootstrap_daemon/
15+
COPY other/bootstrap_node_packets.* /src/workspace/c-toxcore/other/
16+
COPY other/fun/ /src/workspace/c-toxcore/other/fun/
17+
COPY auto_tests/check_compat.h /src/workspace/c-toxcore/auto_tests/
18+
COPY testing/ /src/workspace/c-toxcore/testing/
19+
COPY toxav/ /src/workspace/c-toxcore/toxav/
20+
COPY toxcore/ /src/workspace/c-toxcore/toxcore/
21+
COPY toxencryptsave/ /src/workspace/c-toxcore/toxencryptsave/
22+
COPY third_party/cmp/cmp.h /src/workspace/c-toxcore/third_party/cmp/
23+
COPY other/analysis/run-cppcheck \
24+
other/analysis/gen-file.sh \
25+
other/analysis/variants.sh \
26+
/src/workspace/c-toxcore/other/analysis/
27+
COPY other/docker/cppcheck/toxcore.cfg \
28+
/src/workspace/c-toxcore/other/docker/cppcheck/
29+
WORKDIR /src/workspace/c-toxcore
30+
RUN ["other/analysis/run-cppcheck"]

other/docker/cppcheck/run

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
3+
set -eux
4+
BUILD=cppcheck
5+
docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/Dockerfile" .

other/docker/cppcheck/toxcore.cfg

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?xml version="1.0"?>
2+
<def format="2">
3+
<memory>
4+
<alloc init="false" buffer-size="malloc:2">mem_balloc</alloc>
5+
<alloc init="true" buffer-size="malloc:2">mem_alloc</alloc>
6+
<alloc init="true" buffer-size="calloc:2,3">mem_valloc</alloc>
7+
<realloc init="false" buffer-size="calloc:3,4">mem_vrealloc</realloc>
8+
<dealloc arg="2">mem_delete</dealloc>
9+
</memory>
10+
<resource>
11+
<alloc init="true">bin_pack_new</alloc>
12+
<dealloc arg="1">bin_pack_free</dealloc>
13+
</resource>
14+
<resource>
15+
<alloc init="true">bin_unpack_new</alloc>
16+
<dealloc arg="1">bin_unpack_free</dealloc>
17+
</resource>
18+
<resource>
19+
<alloc init="true">friendreq_new</alloc>
20+
<dealloc arg="1">friendreq_kill</dealloc>
21+
</resource>
22+
<resource>
23+
<alloc init="true">logger_new</alloc>
24+
<dealloc arg="1">logger_kill</dealloc>
25+
</resource>
26+
<resource>
27+
<alloc init="true">mono_time_new</alloc>
28+
<dealloc arg="1">mono_time_free</dealloc>
29+
</resource>
30+
<resource>
31+
<alloc init="true">ping_array_new</alloc>
32+
<dealloc arg="1">ping_array_kill</dealloc>
33+
</resource>
34+
<resource>
35+
<alloc init="true">ping_new</alloc>
36+
<dealloc arg="1">ping_kill</dealloc>
37+
</resource>
38+
<resource>
39+
<alloc init="true">shared_key_cache_new</alloc>
40+
<dealloc arg="1">shared_key_cache_free</dealloc>
41+
</resource>
42+
<resource>
43+
<alloc init="true">tox_dispatch_new</alloc>
44+
<dealloc arg="1">tox_dispatch_free</dealloc>
45+
</resource>
46+
<resource>
47+
<alloc init="true">tox_new</alloc>
48+
<dealloc arg="1">tox_kill</dealloc>
49+
</resource>
50+
<resource>
51+
<alloc init="true">tox_options_new</alloc>
52+
<dealloc arg="1">tox_options_free</dealloc>
53+
</resource>
54+
<resource>
55+
<alloc init="true">new_announcements</alloc>
56+
<dealloc arg="1">kill_announcements</dealloc>
57+
</resource>
58+
<resource>
59+
<alloc init="true">new_dht</alloc>
60+
<dealloc arg="1">kill_dht</dealloc>
61+
</resource>
62+
<resource>
63+
<alloc init="true">new_dht_groupchats</alloc>
64+
<dealloc arg="1">kill_dht_groupchats</dealloc>
65+
</resource>
66+
<resource>
67+
<alloc init="true">new_forwarding</alloc>
68+
<dealloc arg="1">kill_forwarding</dealloc>
69+
</resource>
70+
<resource>
71+
<alloc init="true">new_friend_connections</alloc>
72+
<dealloc arg="1">kill_friend_connections</dealloc>
73+
</resource>
74+
<resource>
75+
<alloc init="true">new_gca_list</alloc>
76+
<dealloc arg="1">kill_gca_list</dealloc>
77+
</resource>
78+
<resource>
79+
<alloc init="true">new_groupchats</alloc>
80+
<dealloc arg="1">kill_groupchats</dealloc>
81+
</resource>
82+
<resource>
83+
<alloc init="true">new_messenger</alloc>
84+
<dealloc arg="1">kill_messenger</dealloc>
85+
</resource>
86+
<resource>
87+
<alloc init="true">new_net_crypto</alloc>
88+
<dealloc arg="1">kill_net_crypto</dealloc>
89+
</resource>
90+
<resource>
91+
<alloc init="true">new_networking_ex</alloc>
92+
<alloc init="true">new_networking_no_udp</alloc>
93+
<dealloc arg="1">kill_networking</dealloc>
94+
</resource>
95+
<resource>
96+
<alloc init="true">new_onion</alloc>
97+
<dealloc arg="1">kill_onion</dealloc>
98+
</resource>
99+
<resource>
100+
<alloc init="true">new_onion_announce</alloc>
101+
<dealloc arg="1">kill_onion_announce</dealloc>
102+
</resource>
103+
<resource>
104+
<alloc init="true">new_onion_client</alloc>
105+
<dealloc arg="1">kill_onion_client</dealloc>
106+
</resource>
107+
<resource>
108+
<alloc init="true">new_tcp_connections</alloc>
109+
<dealloc arg="1">kill_tcp_connections</dealloc>
110+
</resource>
111+
<resource>
112+
<alloc init="true">new_tcp_server</alloc>
113+
<dealloc arg="1">kill_tcp_server</dealloc>
114+
</resource>
115+
</def>
116+
<!-- vim:ft=xml
117+
-->

other/fun/create_savedata.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static bool create_tox(const unsigned char *const secret_key, Tox **const tox)
4141
return true;
4242
}
4343

44-
static bool print_savedata(Tox *const tox)
44+
static bool print_savedata(const Tox *const tox)
4545
{
4646
const size_t savedata_size = tox_get_savedata_size(tox);
4747
uint8_t *const savedata = (uint8_t *)malloc(savedata_size);

other/fun/save-generator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static void tox_connection_callback(Tox *tox, Tox_Connection connection, void *u
6161
}
6262
}
6363

64-
static void print_information(Tox *tox)
64+
static void print_information(const Tox *tox)
6565
{
6666
uint8_t tox_id[TOX_ADDRESS_SIZE];
6767
char tox_id_str[TOX_ADDRESS_SIZE * 2];

0 commit comments

Comments
 (0)