Skip to content

Commit ef4897a

Browse files
committed
cleanup: Upgrade to clang-tidy-17 and fix some warnings.
1 parent b7f9367 commit ef4897a

File tree

17 files changed

+178
-106
lines changed

17 files changed

+178
-106
lines changed

.circleci/config.yml

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ workflows:
1515
- ubsan
1616
# Static analysis
1717
- clang-analyze
18-
- clang-tidy
1918
- cpplint
2019
- infer
2120
- static-analysis
@@ -154,24 +153,6 @@ jobs:
154153
- run: git submodule update --init --recursive
155154
- run: other/analysis/run-clang-analyze
156155

157-
clang-tidy:
158-
working_directory: ~/work
159-
docker:
160-
- image: ubuntu
161-
162-
steps:
163-
- run: *apt_install
164-
- run:
165-
apt-get install -y --no-install-recommends
166-
ca-certificates
167-
clang-tidy-14
168-
- checkout
169-
- run: git submodule update --init --recursive
170-
- run:
171-
other/analysis/run-clang-tidy ||
172-
other/analysis/run-clang-tidy ||
173-
other/analysis/run-clang-tidy
174-
175156
cpplint:
176157
working_directory: ~/work
177158
docker:

.clang-tidy

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ CheckOptions:
3535
value: lower_case
3636

3737
- key: llvmlibc-restrict-system-libc-headers.Includes
38-
value: "arpa/inet.h,assert.h,ctype.h,errno.h,fcntl.h,getopt.h,libconfig.h,linux/netdevice.h,math.h,netdb.h,netinet/in.h,opus.h,pthread.h,signal.h,sodium/crypto_scalarmult_curve25519.h,sodium.h,sodium/randombytes.h,stdio.h,stdlib.h,string.h,sys/ioctl.h,syslog.h,sys/resource.h,sys/socket.h,sys/stat.h,sys/time.h,sys/types.h,time.h,unistd.h,vpx/vp8cx.h,vpx/vp8dx.h,vpx/vpx_decoder.h,vpx/vpx_encoder.h,vpx/vpx_image.h"
38+
value: "arpa/inet.h,assert.h,ctype.h,errno.h,fcntl.h,getopt.h,libconfig.h,limits.h,linux/netdevice.h,math.h,netdb.h,netinet/in.h,opus.h,pthread.h,signal.h,sodium/crypto_scalarmult_curve25519.h,sodium.h,sodium/randombytes.h,stdarg.h,stdbool.h,stdint.h,stdio.h,stdlib.h,string.h,sys/ioctl.h,syslog.h,sys/resource.h,sys/socket.h,sys/stat.h,sys/time.h,sys/types.h,time.h,unistd.h,vpx/vp8cx.h,vpx/vp8dx.h,vpx/vpx_decoder.h,vpx/vpx_encoder.h,vpx/vpx_image.h"
3939
- key: hicpp-signed-bitwise.IgnorePositiveIntegerLiterals
4040
value: true
41+
- key: concurrency-mt-unsafe.FunctionSet
42+
value: posix

.github/workflows/ci.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ jobs:
4040
(find . -name "*.py" -and -not -name "conanfile.py"; grep -lR '^#!.*python') \
4141
| xargs -n1 -P8 mypy --strict
4242
43+
clang-tidy:
44+
runs-on: ubuntu-latest
45+
steps:
46+
- name: Set up Docker Buildx
47+
uses: docker/setup-buildx-action@v3
48+
- name: Docker Build
49+
uses: docker/build-push-action@v4
50+
with:
51+
file: other/docker/clang-tidy/Dockerfile
52+
4353
doxygen:
4454
runs-on: ubuntu-latest
4555
steps:

other/DHT_bootstrap.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static const char *motd_str = ""; //Change this to anything within 256 bytes(but
4646
#define PORT 33445
4747

4848

49-
static void manage_keys(DHT *dht)
49+
static bool manage_keys(DHT *dht)
5050
{
5151
enum { KEYS_SIZE = CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SECRET_KEY_SIZE };
5252
uint8_t keys[KEYS_SIZE];
@@ -60,7 +60,8 @@ static void manage_keys(DHT *dht)
6060

6161
if (read_size != KEYS_SIZE) {
6262
printf("Error while reading the key file\nExiting.\n");
63-
exit(1);
63+
fclose(keys_file);
64+
return false;
6465
}
6566

6667
dht_set_self_public_key(dht, keys);
@@ -73,18 +74,20 @@ static void manage_keys(DHT *dht)
7374

7475
if (keys_file == nullptr) {
7576
printf("Error opening key file in write mode.\nKeys will not be saved.\n");
76-
return;
77+
return false;
7778
}
7879

7980
if (fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file) != KEYS_SIZE) {
8081
printf("Error while writing the key file.\nExiting.\n");
81-
exit(1);
82+
fclose(keys_file);
83+
return false;
8284
}
8385

8486
printf("Keys saved successfully.\n");
8587
}
8688

8789
fclose(keys_file);
90+
return true;
8891
}
8992

9093
static const char *strlevel(Logger_Level level)
@@ -121,15 +124,15 @@ int main(int argc, char *argv[])
121124
if (argc == 2 && !tox_strncasecmp(argv[1], "-h", 3)) {
122125
printf("Usage (connected) : %s [--ipv4|--ipv6] IP PORT KEY\n", argv[0]);
123126
printf("Usage (unconnected): %s [--ipv4|--ipv6]\n", argv[0]);
124-
exit(0);
127+
return 0;
125128
}
126129

127130
/* let user override default by cmdline */
128131
bool ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
129132
int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
130133

131134
if (argvoffset < 0) {
132-
exit(1);
135+
return 1;
133136
}
134137

135138
/* Initialize networking -
@@ -162,14 +165,16 @@ int main(int argc, char *argv[])
162165

163166
if (!(onion && forwarding && onion_a)) {
164167
printf("Something failed to initialize.\n");
165-
exit(1);
168+
return 1;
166169
}
167170

168171
gca_onion_init(gc_announces_list, onion_a);
169172

170173
perror("Initialization");
171174

172-
manage_keys(dht);
175+
if (!manage_keys(dht)) {
176+
return 1;
177+
}
173178
printf("Public key: ");
174179

175180
#ifdef TCP_RELAY_ENABLED
@@ -179,7 +184,7 @@ int main(int argc, char *argv[])
179184

180185
if (tcp_s == nullptr) {
181186
printf("TCP server failed to initialize.\n");
182-
exit(1);
187+
return 1;
183188
}
184189

185190
#endif
@@ -189,7 +194,7 @@ int main(int argc, char *argv[])
189194

190195
if (file == nullptr) {
191196
printf("Could not open file \"%s\" for writing. Exiting...\n", public_id_filename);
192-
exit(1);
197+
return 1;
193198
}
194199

195200
for (uint32_t i = 0; i < 32; ++i) {
@@ -210,7 +215,7 @@ int main(int argc, char *argv[])
210215

211216
if (port_conv <= 0 || port_conv > UINT16_MAX) {
212217
printf("Failed to convert \"%s\" into a valid port. Exiting...\n", argv[argvoffset + 2]);
213-
exit(1);
218+
return 1;
214219
}
215220

216221
const uint16_t port = net_htons((uint16_t)port_conv);
@@ -222,7 +227,7 @@ int main(int argc, char *argv[])
222227

223228
if (!res) {
224229
printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]);
225-
exit(1);
230+
return 1;
226231
}
227232
}
228233

other/analysis/run-clang-tidy

Lines changed: 65 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,55 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22

33
CHECKS="*"
4+
ERRORS="*"
5+
6+
# Need to investigate or disable and document these.
7+
# =========================================================
8+
9+
# TODO(iphydf): Fix these.
10+
ERRORS="$ERRORS,-cert-err34-c"
11+
ERRORS="$ERRORS,-cert-str34-c"
12+
ERRORS="$ERRORS,-readability-suspicious-call-argument"
13+
14+
# TODO(iphydf): Fix these.
15+
CHECKS="$CHECKS,-bugprone-switch-missing-default-case"
16+
CHECKS="$CHECKS,-misc-include-cleaner"
17+
18+
# TODO(iphydf): We might want some of these. For the ones we don't want, add a
19+
# comment explaining why not.
20+
CHECKS="$CHECKS,-clang-analyzer-optin.performance.Padding"
21+
CHECKS="$CHECKS,-hicpp-signed-bitwise"
22+
CHECKS="$CHECKS,-readability-function-cognitive-complexity"
23+
24+
# TODO(iphydf): Maybe fix these?
25+
CHECKS="$CHECKS,-bugprone-easily-swappable-parameters"
26+
CHECKS="$CHECKS,-bugprone-implicit-widening-of-multiplication-result"
27+
CHECKS="$CHECKS,-bugprone-integer-division"
28+
CHECKS="$CHECKS,-clang-analyzer-core.NullDereference"
29+
CHECKS="$CHECKS,-clang-analyzer-valist.Uninitialized"
30+
CHECKS="$CHECKS,-cppcoreguidelines-avoid-non-const-global-variables"
31+
CHECKS="$CHECKS,-misc-no-recursion"
32+
33+
# TODO(iphydf): Probably fix these.
34+
CHECKS="$CHECKS,-cert-err33-c"
35+
CHECKS="$CHECKS,-cppcoreguidelines-avoid-magic-numbers"
36+
CHECKS="$CHECKS,-google-readability-casting"
37+
CHECKS="$CHECKS,-modernize-macro-to-enum"
38+
CHECKS="$CHECKS,-readability-magic-numbers"
39+
40+
# Documented disabled checks. We don't want these for sure.
41+
# =========================================================
42+
43+
# Callback handlers often don't use all their parameters. There's
44+
# IgnoreVirtual, but that doesn't work for C-style callbacks.
45+
CHECKS="$CHECKS,-misc-unused-parameters"
46+
47+
# We sometimes use #if 0.
48+
CHECKS="$CHECKS,-readability-avoid-unconditional-preprocessor-if"
49+
50+
# We have better macro hygiene checks with tokstyle. We can never pass macro
51+
# arguments that require parentheses inside the macro.
52+
CHECKS="$CHECKS,-bugprone-macro-parentheses"
453

554
# We don't use memcpy_s.
655
CHECKS="$CHECKS,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling"
@@ -81,42 +130,6 @@ CHECKS="$CHECKS,-cert-dcl03-c"
81130
CHECKS="$CHECKS,-hicpp-static-assert"
82131
CHECKS="$CHECKS,-misc-static-assert"
83132

84-
# TODO(iphydf): We might want some of these. For the ones we don't want, add a
85-
# comment explaining why not.
86-
CHECKS="$CHECKS,-clang-analyzer-optin.performance.Padding"
87-
CHECKS="$CHECKS,-hicpp-signed-bitwise"
88-
CHECKS="$CHECKS,-misc-unused-parameters"
89-
CHECKS="$CHECKS,-readability-function-cognitive-complexity"
90-
91-
# TODO(iphydf): Maybe fix these?
92-
CHECKS="$CHECKS,-bugprone-easily-swappable-parameters"
93-
CHECKS="$CHECKS,-bugprone-implicit-widening-of-multiplication-result"
94-
CHECKS="$CHECKS,-bugprone-integer-division"
95-
CHECKS="$CHECKS,-clang-analyzer-core.NullDereference"
96-
CHECKS="$CHECKS,-clang-analyzer-valist.Uninitialized"
97-
CHECKS="$CHECKS,-concurrency-mt-unsafe"
98-
CHECKS="$CHECKS,-cppcoreguidelines-avoid-non-const-global-variables"
99-
CHECKS="$CHECKS,-misc-no-recursion"
100-
101-
# TODO(iphydf): Probably fix these.
102-
CHECKS="$CHECKS,-cert-err33-c"
103-
CHECKS="$CHECKS,-cppcoreguidelines-avoid-magic-numbers"
104-
CHECKS="$CHECKS,-google-readability-casting"
105-
CHECKS="$CHECKS,-modernize-macro-to-enum"
106-
CHECKS="$CHECKS,-readability-magic-numbers"
107-
108-
# TODO(iphydf): These two trip on list.c. Investigate why.
109-
CHECKS="$CHECKS,-clang-analyzer-core.NonNullParamChecker"
110-
CHECKS="$CHECKS,-clang-analyzer-unix.Malloc"
111-
112-
ERRORS="*"
113-
114-
# TODO(iphydf): Fix these.
115-
ERRORS="$ERRORS,-bugprone-macro-parentheses"
116-
ERRORS="$ERRORS,-cert-err34-c"
117-
ERRORS="$ERRORS,-cert-str34-c"
118-
ERRORS="$ERRORS,-readability-suspicious-call-argument"
119-
120133
set -eux
121134

122135
run() {
@@ -125,18 +138,21 @@ run() {
125138
for i in "${!EXTRA_ARGS[@]}"; do
126139
EXTRA_ARGS[$i]="--extra-arg=${EXTRA_ARGS[$i]}"
127140
done
128-
clang-tidy-14 \
129-
-p=_build \
130-
--extra-arg=-DMIN_LOGGER_LEVEL=LOGGER_LEVEL_TRACE \
131-
"${EXTRA_ARGS[@]}" \
132-
--checks="$CHECKS" \
133-
--warnings-as-errors="$ERRORS" \
134-
--use-color \
135-
other/bootstrap_daemon/src/*.c \
136-
other/*.c \
137-
toxav/*.c \
138-
toxcore/*.c \
139-
toxencryptsave/*.c
141+
find \
142+
other/bootstrap_daemon/src \
143+
other \
144+
toxav \
145+
toxcore \
146+
toxcore/events \
147+
toxencryptsave \
148+
-maxdepth 1 -name "*.c" -print0 \
149+
| xargs -0 -n15 -P"$(nproc)" clang-tidy \
150+
-p=_build \
151+
--extra-arg=-DMIN_LOGGER_LEVEL=LOGGER_LEVEL_TRACE \
152+
"${EXTRA_ARGS[@]}" \
153+
--checks="$CHECKS" \
154+
--warnings-as-errors="$ERRORS" \
155+
--use-color
140156
}
141157

142158
cmake . -B_build -GNinja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
68432689967d06dd144e5cdfe37751ccc62b2aa85b73a9cc55aff3732dc47fde /usr/local/bin/tox-bootstrapd
1+
793cce1916b0d406b8e337d1a5243dc71b07727974cc83a3ef39b7af6cbf6cdb /usr/local/bin/tox-bootstrapd

other/bootstrap_daemon/src/command_line_arguments.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,14 @@ static void print_help(void)
4747
" --version Print version information.\n");
4848
}
4949

50-
void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path, LOG_BACKEND *log_backend,
51-
bool *run_in_foreground)
50+
Cli_Status handle_command_line_arguments(
51+
int argc, char *argv[], char **cfg_file_path, LOG_BACKEND *log_backend,
52+
bool *run_in_foreground)
5253
{
5354
if (argc < 2) {
5455
log_write(LOG_LEVEL_ERROR, "Error: No arguments provided.\n\n");
5556
print_help();
56-
exit(1);
57+
return CLI_STATUS_ERROR;
5758
}
5859

5960
opterr = 0;
@@ -89,7 +90,7 @@ void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path,
8990

9091
case 'h':
9192
print_help();
92-
exit(0);
93+
return CLI_STATUS_DONE;
9394

9495
case 'l':
9596
if (strcmp(optarg, "syslog") == 0) {
@@ -101,24 +102,24 @@ void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path,
101102
} else {
102103
log_write(LOG_LEVEL_ERROR, "Error: Invalid BACKEND value for --log-backend option passed: %s\n\n", optarg);
103104
print_help();
104-
exit(1);
105+
return CLI_STATUS_ERROR;
105106
}
106107

107108
break;
108109

109110
case 'v':
110111
log_write(LOG_LEVEL_INFO, "Version: %lu\n", DAEMON_VERSION_NUMBER);
111-
exit(0);
112+
return CLI_STATUS_DONE;
112113

113114
case '?':
114115
log_write(LOG_LEVEL_ERROR, "Error: Unrecognized option %s\n\n", argv[optind - 1]);
115116
print_help();
116-
exit(1);
117+
return CLI_STATUS_ERROR;
117118

118119
case ':':
119120
log_write(LOG_LEVEL_ERROR, "Error: No argument provided for option %s\n\n", argv[optind - 1]);
120121
print_help();
121-
exit(1);
122+
return CLI_STATUS_ERROR;
122123
}
123124
}
124125

@@ -129,6 +130,8 @@ void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path,
129130
if (!cfg_file_path_set) {
130131
log_write(LOG_LEVEL_ERROR, "Error: The required --config option wasn't specified\n\n");
131132
print_help();
132-
exit(1);
133+
return CLI_STATUS_ERROR;
133134
}
135+
136+
return CLI_STATUS_OK;
134137
}

0 commit comments

Comments
 (0)