cleanup: Upgrade to clang-tidy-17 and fix some warnings.

This commit is contained in:
iphydf 2023-12-26 19:39:22 +00:00
parent b7f9367f6f
commit ef4897a898
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
17 changed files with 178 additions and 106 deletions

View File

@ -15,7 +15,6 @@ workflows:
- ubsan
# Static analysis
- clang-analyze
- clang-tidy
- cpplint
- infer
- static-analysis
@ -154,24 +153,6 @@ jobs:
- run: git submodule update --init --recursive
- run: other/analysis/run-clang-analyze
clang-tidy:
working_directory: ~/work
docker:
- image: ubuntu
steps:
- run: *apt_install
- run:
apt-get install -y --no-install-recommends
ca-certificates
clang-tidy-14
- checkout
- run: git submodule update --init --recursive
- run:
other/analysis/run-clang-tidy ||
other/analysis/run-clang-tidy ||
other/analysis/run-clang-tidy
cpplint:
working_directory: ~/work
docker:

View File

@ -35,6 +35,8 @@ CheckOptions:
value: lower_case
- key: llvmlibc-restrict-system-libc-headers.Includes
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"
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"
- key: hicpp-signed-bitwise.IgnorePositiveIntegerLiterals
value: true
- key: concurrency-mt-unsafe.FunctionSet
value: posix

View File

@ -40,6 +40,16 @@ jobs:
(find . -name "*.py" -and -not -name "conanfile.py"; grep -lR '^#!.*python') \
| xargs -n1 -P8 mypy --strict
clang-tidy:
runs-on: ubuntu-latest
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Docker Build
uses: docker/build-push-action@v4
with:
file: other/docker/clang-tidy/Dockerfile
doxygen:
runs-on: ubuntu-latest
steps:

View File

@ -46,7 +46,7 @@ static const char *motd_str = ""; //Change this to anything within 256 bytes(but
#define PORT 33445
static void manage_keys(DHT *dht)
static bool manage_keys(DHT *dht)
{
enum { KEYS_SIZE = CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SECRET_KEY_SIZE };
uint8_t keys[KEYS_SIZE];
@ -60,7 +60,8 @@ static void manage_keys(DHT *dht)
if (read_size != KEYS_SIZE) {
printf("Error while reading the key file\nExiting.\n");
exit(1);
fclose(keys_file);
return false;
}
dht_set_self_public_key(dht, keys);
@ -73,18 +74,20 @@ static void manage_keys(DHT *dht)
if (keys_file == nullptr) {
printf("Error opening key file in write mode.\nKeys will not be saved.\n");
return;
return false;
}
if (fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file) != KEYS_SIZE) {
printf("Error while writing the key file.\nExiting.\n");
exit(1);
fclose(keys_file);
return false;
}
printf("Keys saved successfully.\n");
}
fclose(keys_file);
return true;
}
static const char *strlevel(Logger_Level level)
@ -121,7 +124,7 @@ int main(int argc, char *argv[])
if (argc == 2 && !tox_strncasecmp(argv[1], "-h", 3)) {
printf("Usage (connected) : %s [--ipv4|--ipv6] IP PORT KEY\n", argv[0]);
printf("Usage (unconnected): %s [--ipv4|--ipv6]\n", argv[0]);
exit(0);
return 0;
}
/* let user override default by cmdline */
@ -129,7 +132,7 @@ int main(int argc, char *argv[])
int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
if (argvoffset < 0) {
exit(1);
return 1;
}
/* Initialize networking -
@ -162,14 +165,16 @@ int main(int argc, char *argv[])
if (!(onion && forwarding && onion_a)) {
printf("Something failed to initialize.\n");
exit(1);
return 1;
}
gca_onion_init(gc_announces_list, onion_a);
perror("Initialization");
manage_keys(dht);
if (!manage_keys(dht)) {
return 1;
}
printf("Public key: ");
#ifdef TCP_RELAY_ENABLED
@ -179,7 +184,7 @@ int main(int argc, char *argv[])
if (tcp_s == nullptr) {
printf("TCP server failed to initialize.\n");
exit(1);
return 1;
}
#endif
@ -189,7 +194,7 @@ int main(int argc, char *argv[])
if (file == nullptr) {
printf("Could not open file \"%s\" for writing. Exiting...\n", public_id_filename);
exit(1);
return 1;
}
for (uint32_t i = 0; i < 32; ++i) {
@ -210,7 +215,7 @@ int main(int argc, char *argv[])
if (port_conv <= 0 || port_conv > UINT16_MAX) {
printf("Failed to convert \"%s\" into a valid port. Exiting...\n", argv[argvoffset + 2]);
exit(1);
return 1;
}
const uint16_t port = net_htons((uint16_t)port_conv);
@ -222,7 +227,7 @@ int main(int argc, char *argv[])
if (!res) {
printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]);
exit(1);
return 1;
}
}

View File

@ -1,6 +1,55 @@
#!/bin/bash
#!/usr/bin/env bash
CHECKS="*"
ERRORS="*"
# Need to investigate or disable and document these.
# =========================================================
# TODO(iphydf): Fix these.
ERRORS="$ERRORS,-cert-err34-c"
ERRORS="$ERRORS,-cert-str34-c"
ERRORS="$ERRORS,-readability-suspicious-call-argument"
# TODO(iphydf): Fix these.
CHECKS="$CHECKS,-bugprone-switch-missing-default-case"
CHECKS="$CHECKS,-misc-include-cleaner"
# TODO(iphydf): We might want some of these. For the ones we don't want, add a
# comment explaining why not.
CHECKS="$CHECKS,-clang-analyzer-optin.performance.Padding"
CHECKS="$CHECKS,-hicpp-signed-bitwise"
CHECKS="$CHECKS,-readability-function-cognitive-complexity"
# TODO(iphydf): Maybe fix these?
CHECKS="$CHECKS,-bugprone-easily-swappable-parameters"
CHECKS="$CHECKS,-bugprone-implicit-widening-of-multiplication-result"
CHECKS="$CHECKS,-bugprone-integer-division"
CHECKS="$CHECKS,-clang-analyzer-core.NullDereference"
CHECKS="$CHECKS,-clang-analyzer-valist.Uninitialized"
CHECKS="$CHECKS,-cppcoreguidelines-avoid-non-const-global-variables"
CHECKS="$CHECKS,-misc-no-recursion"
# TODO(iphydf): Probably fix these.
CHECKS="$CHECKS,-cert-err33-c"
CHECKS="$CHECKS,-cppcoreguidelines-avoid-magic-numbers"
CHECKS="$CHECKS,-google-readability-casting"
CHECKS="$CHECKS,-modernize-macro-to-enum"
CHECKS="$CHECKS,-readability-magic-numbers"
# Documented disabled checks. We don't want these for sure.
# =========================================================
# Callback handlers often don't use all their parameters. There's
# IgnoreVirtual, but that doesn't work for C-style callbacks.
CHECKS="$CHECKS,-misc-unused-parameters"
# We sometimes use #if 0.
CHECKS="$CHECKS,-readability-avoid-unconditional-preprocessor-if"
# We have better macro hygiene checks with tokstyle. We can never pass macro
# arguments that require parentheses inside the macro.
CHECKS="$CHECKS,-bugprone-macro-parentheses"
# We don't use memcpy_s.
CHECKS="$CHECKS,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling"
@ -81,42 +130,6 @@ CHECKS="$CHECKS,-cert-dcl03-c"
CHECKS="$CHECKS,-hicpp-static-assert"
CHECKS="$CHECKS,-misc-static-assert"
# TODO(iphydf): We might want some of these. For the ones we don't want, add a
# comment explaining why not.
CHECKS="$CHECKS,-clang-analyzer-optin.performance.Padding"
CHECKS="$CHECKS,-hicpp-signed-bitwise"
CHECKS="$CHECKS,-misc-unused-parameters"
CHECKS="$CHECKS,-readability-function-cognitive-complexity"
# TODO(iphydf): Maybe fix these?
CHECKS="$CHECKS,-bugprone-easily-swappable-parameters"
CHECKS="$CHECKS,-bugprone-implicit-widening-of-multiplication-result"
CHECKS="$CHECKS,-bugprone-integer-division"
CHECKS="$CHECKS,-clang-analyzer-core.NullDereference"
CHECKS="$CHECKS,-clang-analyzer-valist.Uninitialized"
CHECKS="$CHECKS,-concurrency-mt-unsafe"
CHECKS="$CHECKS,-cppcoreguidelines-avoid-non-const-global-variables"
CHECKS="$CHECKS,-misc-no-recursion"
# TODO(iphydf): Probably fix these.
CHECKS="$CHECKS,-cert-err33-c"
CHECKS="$CHECKS,-cppcoreguidelines-avoid-magic-numbers"
CHECKS="$CHECKS,-google-readability-casting"
CHECKS="$CHECKS,-modernize-macro-to-enum"
CHECKS="$CHECKS,-readability-magic-numbers"
# TODO(iphydf): These two trip on list.c. Investigate why.
CHECKS="$CHECKS,-clang-analyzer-core.NonNullParamChecker"
CHECKS="$CHECKS,-clang-analyzer-unix.Malloc"
ERRORS="*"
# TODO(iphydf): Fix these.
ERRORS="$ERRORS,-bugprone-macro-parentheses"
ERRORS="$ERRORS,-cert-err34-c"
ERRORS="$ERRORS,-cert-str34-c"
ERRORS="$ERRORS,-readability-suspicious-call-argument"
set -eux
run() {
@ -125,18 +138,21 @@ run() {
for i in "${!EXTRA_ARGS[@]}"; do
EXTRA_ARGS[$i]="--extra-arg=${EXTRA_ARGS[$i]}"
done
clang-tidy-14 \
-p=_build \
--extra-arg=-DMIN_LOGGER_LEVEL=LOGGER_LEVEL_TRACE \
"${EXTRA_ARGS[@]}" \
--checks="$CHECKS" \
--warnings-as-errors="$ERRORS" \
--use-color \
other/bootstrap_daemon/src/*.c \
other/*.c \
toxav/*.c \
toxcore/*.c \
toxencryptsave/*.c
find \
other/bootstrap_daemon/src \
other \
toxav \
toxcore \
toxcore/events \
toxencryptsave \
-maxdepth 1 -name "*.c" -print0 \
| xargs -0 -n15 -P"$(nproc)" clang-tidy \
-p=_build \
--extra-arg=-DMIN_LOGGER_LEVEL=LOGGER_LEVEL_TRACE \
"${EXTRA_ARGS[@]}" \
--checks="$CHECKS" \
--warnings-as-errors="$ERRORS" \
--use-color
}
cmake . -B_build -GNinja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON

View File

@ -1 +1 @@
68432689967d06dd144e5cdfe37751ccc62b2aa85b73a9cc55aff3732dc47fde /usr/local/bin/tox-bootstrapd
793cce1916b0d406b8e337d1a5243dc71b07727974cc83a3ef39b7af6cbf6cdb /usr/local/bin/tox-bootstrapd

View File

@ -47,13 +47,14 @@ static void print_help(void)
" --version Print version information.\n");
}
void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path, LOG_BACKEND *log_backend,
bool *run_in_foreground)
Cli_Status handle_command_line_arguments(
int argc, char *argv[], char **cfg_file_path, LOG_BACKEND *log_backend,
bool *run_in_foreground)
{
if (argc < 2) {
log_write(LOG_LEVEL_ERROR, "Error: No arguments provided.\n\n");
print_help();
exit(1);
return CLI_STATUS_ERROR;
}
opterr = 0;
@ -89,7 +90,7 @@ void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path,
case 'h':
print_help();
exit(0);
return CLI_STATUS_DONE;
case 'l':
if (strcmp(optarg, "syslog") == 0) {
@ -101,24 +102,24 @@ void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path,
} else {
log_write(LOG_LEVEL_ERROR, "Error: Invalid BACKEND value for --log-backend option passed: %s\n\n", optarg);
print_help();
exit(1);
return CLI_STATUS_ERROR;
}
break;
case 'v':
log_write(LOG_LEVEL_INFO, "Version: %lu\n", DAEMON_VERSION_NUMBER);
exit(0);
return CLI_STATUS_DONE;
case '?':
log_write(LOG_LEVEL_ERROR, "Error: Unrecognized option %s\n\n", argv[optind - 1]);
print_help();
exit(1);
return CLI_STATUS_ERROR;
case ':':
log_write(LOG_LEVEL_ERROR, "Error: No argument provided for option %s\n\n", argv[optind - 1]);
print_help();
exit(1);
return CLI_STATUS_ERROR;
}
}
@ -129,6 +130,8 @@ void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path,
if (!cfg_file_path_set) {
log_write(LOG_LEVEL_ERROR, "Error: The required --config option wasn't specified\n\n");
print_help();
exit(1);
return CLI_STATUS_ERROR;
}
return CLI_STATUS_OK;
}

View File

@ -12,6 +12,15 @@
#include "log.h"
typedef enum Cli_Status {
/** Continue the program. Command line processing completed. */
CLI_STATUS_OK,
/** Stop the program with success status. */
CLI_STATUS_DONE,
/** Stop the program with error status. */
CLI_STATUS_ERROR,
} Cli_Status;
/**
* Handles command line arguments, setting cfg_file_path and log_backend.
* Terminates the application if incorrect arguments are specified.
@ -22,7 +31,8 @@
* @param log_backend Sets to the provided by the user log backend option.
* @param run_in_foreground Sets to the provided by the user foreground option.
*/
void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path, LOG_BACKEND *log_backend,
bool *run_in_foreground);
Cli_Status handle_command_line_arguments(
int argc, char *argv[], char **cfg_file_path, LOG_BACKEND *log_backend,
bool *run_in_foreground);
#endif // C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_COMMAND_LINE_ARGUMENTS_H

View File

@ -116,7 +116,7 @@ static void print_public_key(const uint8_t *public_key)
// Demonizes the process, appending PID to the PID file and closing file descriptors based on log backend
// Terminates the application if the daemonization fails.
static void daemonize(LOG_BACKEND log_backend, char *pid_file_path)
static Cli_Status daemonize(LOG_BACKEND log_backend, char *pid_file_path)
{
// Check if the PID file exists
FILE *pid_file = fopen(pid_file_path, "r");
@ -131,7 +131,7 @@ static void daemonize(LOG_BACKEND log_backend, char *pid_file_path)
if (pid_file == nullptr) {
log_write(LOG_LEVEL_ERROR, "Couldn't open the PID file for writing: %s. Exiting.\n", pid_file_path);
exit(1);
return CLI_STATUS_ERROR;
}
// Fork off from the parent process
@ -141,27 +141,27 @@ static void daemonize(LOG_BACKEND log_backend, char *pid_file_path)
fprintf(pid_file, "%d", pid);
fclose(pid_file);
log_write(LOG_LEVEL_INFO, "Forked successfully: PID: %d.\n", pid);
exit(0);
return CLI_STATUS_DONE;
} else {
fclose(pid_file);
}
if (pid < 0) {
log_write(LOG_LEVEL_ERROR, "Forking failed. Exiting.\n");
exit(1);
return CLI_STATUS_ERROR;
}
// Create a new SID for the child process
if (setsid() < 0) {
log_write(LOG_LEVEL_ERROR, "SID creation failure. Exiting.\n");
exit(1);
return CLI_STATUS_ERROR;
}
// Change the current working directory
if ((chdir("/")) < 0) {
log_write(LOG_LEVEL_ERROR, "Couldn't change working directory to '/'. Exiting.\n");
exit(1);
return CLI_STATUS_ERROR;
}
// Go quiet
@ -170,6 +170,8 @@ static void daemonize(LOG_BACKEND log_backend, char *pid_file_path)
close(STDIN_FILENO);
close(STDERR_FILENO);
}
return CLI_STATUS_OK;
}
// Logs toxcore logger message using our logger facility
@ -216,7 +218,14 @@ int main(int argc, char *argv[])
LOG_BACKEND log_backend = isatty(STDOUT_FILENO) ? LOG_BACKEND_STDOUT : LOG_BACKEND_SYSLOG;
log_open(log_backend);
handle_command_line_arguments(argc, argv, &cfg_file_path, &log_backend, &run_in_foreground);
switch (handle_command_line_arguments(argc, argv, &cfg_file_path, &log_backend, &run_in_foreground)) {
case CLI_STATUS_OK:
break;
case CLI_STATUS_DONE:
return 0;
case CLI_STATUS_ERROR:
return 1;
}
log_close();
log_open(log_backend);
@ -254,7 +263,14 @@ int main(int argc, char *argv[])
}
if (!run_in_foreground) {
daemonize(log_backend, pid_file_path);
switch (daemonize(log_backend, pid_file_path)) {
case CLI_STATUS_OK:
break;
case CLI_STATUS_DONE:
return 0;
case CLI_STATUS_ERROR:
return 1;
}
}
free(pid_file_path);

View File

@ -2,7 +2,8 @@
# autotools-linux
FROM ubuntu:22.04
RUN apt-get update && apt-get install --no-install-recommends -y \
RUN apt-get update && \
DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends \
autoconf \
automake \
ca-certificates \

View File

@ -0,0 +1,20 @@
FROM alpine:3.19.0
RUN ["apk", "add", "--no-cache", \
"bash", \
"clang", \
"clang-extra-tools", \
"cmake", \
"libconfig-dev", \
"libsodium-dev", \
"libvpx-dev", \
"linux-headers", \
"opus-dev", \
"pkgconfig", \
"samurai"]
ENV CC=clang CXX=clang++
COPY . /src/workspace/c-toxcore/
WORKDIR /src/workspace/c-toxcore
RUN other/analysis/run-clang-tidy

5
other/docker/clang-tidy/run Executable file
View File

@ -0,0 +1,5 @@
#!/bin/sh
set -eux
BUILD=clang-tidy
docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/Dockerfile" .

View File

@ -1,6 +1,7 @@
FROM ubuntu:20.04
RUN apt-get update && apt-get install --no-install-recommends -y \
RUN apt-get update && \
DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends \
ca-certificates \
cppcheck \
libopus-dev \

View File

@ -1,7 +1,8 @@
FROM toxchat/haskell:hs-tokstyle AS tokstyle
FROM ubuntu:22.04
RUN apt-get update && apt-get install --no-install-recommends -y \
RUN apt-get update && \
DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends \
ca-certificates \
clang \
git \

View File

@ -13,7 +13,6 @@ sh_test(
args = ["$(locations %s)" % f for f in CIMPLE_FILES] + [
"-Wno-boolean-return",
"-Wno-callback-names",
"-Wno-enum-names",
"+RTS",
"-N4",
"-RTS",

View File

@ -7,9 +7,9 @@
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "../bin_unpack.h"
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../tox.h"
#include "../tox_events.h"

View File

@ -10,6 +10,7 @@
*/
#include "list.h"
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
@ -206,6 +207,7 @@ bool bs_list_add(BS_List *list, const uint8_t *data, int id)
}
// insert data to element array
assert(list->data != nullptr);
memmove(list->data + (i + 1) * list->element_size, list->data + i * list->element_size,
(list->n - i) * list->element_size);
memcpy(list->data + i * list->element_size, data, list->element_size);