From d3627920cd568b660b2c999d2cab5c80d687d885 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 2 Sep 2025 22:27:45 +0200 Subject: [PATCH] fix some cppchecker warnings, missing null checks, consts --- client/src/cmdmqtt.c | 21 +++++++++++------- client/src/fileutils.c | 2 +- client/src/fileutils.h | 4 ++-- client/src/proxguiqt.cpp | 22 +++++++++++++++++++ client/src/proxmark3.c | 10 +++++++++ tools/hitag2crack/crack3/ht2crack3.c | 2 +- tools/hitag2crack/crack4/ht2crack4.c | 2 +- .../crack5gpu_disabled/ht2crack5gpu.c | 2 +- 8 files changed, 51 insertions(+), 14 deletions(-) diff --git a/client/src/cmdmqtt.c b/client/src/cmdmqtt.c index c2d16ce10..fea03a418 100644 --- a/client/src/cmdmqtt.c +++ b/client/src/cmdmqtt.c @@ -43,11 +43,15 @@ static void mqtt_publish_callback(void **unused, struct mqtt_response_publish *p // note that published->topic_name is NOT null-terminated (here we'll change it to a c-string) char *topic_name = (char *) calloc(published->topic_name_size + 1, 1); + if (topic_name == NULL) { + return; + } + memcpy(topic_name, published->topic_name, published->topic_name_size); const char *msg = published->application_message; - char *ps = strstr(msg, "Created\": \"proxmark3"); + const char *ps = strstr(msg, "Created\": \"proxmark3"); if (ps) { int res = saveFileTXT("ice_mqtt", ".json", msg, published->application_message_size, spDefault); if (res == PM3_SUCCESS) { @@ -68,7 +72,8 @@ static void *mqtt_client_refresher(void *client) { } return NULL; } -static int mqtt_exit(int status, mqtt_pal_socket_handle sockfd, pthread_t *client_daemon) { + +static int mqtt_exit(int status, mqtt_pal_socket_handle sockfd, const pthread_t *client_daemon) { close_nb_socket(sockfd); if (client_daemon != NULL) { mqtt_client_should_exit = 1; @@ -277,15 +282,15 @@ static int CmdMqttSend(const char *Cmd) { int plen = 0; char port[10 + 1] = {0x00}; - res = CLIParamStrToBuf(arg_get_str(ctx, 2), (uint8_t *)port, sizeof(port), &plen); + res |= CLIParamStrToBuf(arg_get_str(ctx, 2), (uint8_t *)port, sizeof(port), &plen); int tlen = 0; char topic[128] = {0x00}; - res = CLIParamStrToBuf(arg_get_str(ctx, 3), (uint8_t *)topic, sizeof(topic), &tlen); + res |= CLIParamStrToBuf(arg_get_str(ctx, 3), (uint8_t *)topic, sizeof(topic), &tlen); int mlen = 0; char msg[128] = {0x00}; - res = CLIParamStrToBuf(arg_get_str(ctx, 4), (uint8_t *)msg, sizeof(msg), &mlen); + res |= CLIParamStrToBuf(arg_get_str(ctx, 4), (uint8_t *)msg, sizeof(msg), &mlen); int fnlen = 0; char filename[FILE_PATH_SIZE] = {0}; @@ -358,11 +363,11 @@ static int CmdMqttReceive(const char *Cmd) { int plen = 0; char port[10 + 1] = {0x00}; - res = CLIParamStrToBuf(arg_get_str(ctx, 2), (uint8_t *)port, sizeof(port), &plen); + res |= CLIParamStrToBuf(arg_get_str(ctx, 2), (uint8_t *)port, sizeof(port), &plen); int tlen = 0; char topic[128] = {0x00}; - res = CLIParamStrToBuf(arg_get_str(ctx, 3), (uint8_t *)topic, sizeof(topic), &tlen); + res |= CLIParamStrToBuf(arg_get_str(ctx, 3), (uint8_t *)topic, sizeof(topic), &tlen); int fnlen = 0; char filename[FILE_PATH_SIZE] = {0}; @@ -419,4 +424,4 @@ static int CmdHelp(const char *Cmd) { int CmdMqtt(const char *Cmd) { clearCommandBuffer(); return CmdsParse(CommandTable, Cmd); -} +} \ No newline at end of file diff --git a/client/src/fileutils.c b/client/src/fileutils.c index 1cbe50fba..0ed0b75ab 100644 --- a/client/src/fileutils.c +++ b/client/src/fileutils.c @@ -3171,7 +3171,7 @@ int searchFile(char **foundpath, const char *pm3dir, const char *searchname, con * Returns PM3_SUCCES or, PM3_EFILE; * * @param filepath Path to the file. - * @param line Line to insert (should not contain a trailing newline). + * @param keystr Line to insert (should not contain a trailing newline). */ int insert_line_if_not_exists(const char *preferredName, const char *keystr) { diff --git a/client/src/fileutils.h b/client/src/fileutils.h index a2d31c196..33407b886 100644 --- a/client/src/fileutils.h +++ b/client/src/fileutils.h @@ -426,8 +426,8 @@ int pm3_save_fm11rf08s_nonces(const char *fn, iso14a_fm11rf08s_nonces_with_data_ * Returns PM3_SUCCES or, PM3_EFILE; * * @param filepath Path to the file. - * @param line Line to insert (should not contain a trailing newline). + * @param keystr Line to insert (should not contain a trailing newline). */ -int insert_line_if_not_exists(const char *preferredName, const char *line); +int insert_line_if_not_exists(const char *preferredName, const char *keystr); #endif // FILEUTILS_H diff --git a/client/src/proxguiqt.cpp b/client/src/proxguiqt.cpp index 768b88c45..b20376e88 100644 --- a/client/src/proxguiqt.cpp +++ b/client/src/proxguiqt.cpp @@ -784,6 +784,9 @@ void Plot::drawAnnotations(QRect annotationRect, QPainter *painter) { length = ((sizeof(graphText)) + (sizeof(uint32_t) * 3) + sizeof(scalestr) + sizeof(float_t)); annotation = (char *)calloc(1, length); + if (annotation == NULL) { + return; + } snprintf(annotation, length, graphText, g_GraphStart, @@ -805,6 +808,9 @@ void Plot::drawAnnotations(QRect annotationRect, QPainter *painter) { length = (sizeof(gridText) + (sizeof(double) * 3) + sizeof(gridLocked)); annotation = (char *)calloc(1, length); + if (annotation == NULL) { + return; + } snprintf(annotation, length, gridText, g_DefaultGridX, @@ -831,7 +837,14 @@ void Plot::drawAnnotations(QRect annotationRect, QPainter *painter) { size_t value; annotation = (char *)calloc(1, length); + if (annotation == NULL) { + return; + } + char *textA = (char *)calloc(1, length); + if (textA == NULL) { + return; + } strcat(textA, markerText); strcat(textA, " (%s%u)"); @@ -863,6 +876,9 @@ void Plot::drawAnnotations(QRect annotationRect, QPainter *painter) { pos = g_MarkerB.pos; annotation = (char *)calloc(1, length); + if (annotation == NULL) { + return; + } snprintf(annotation, length, markerText, "B", @@ -880,6 +896,9 @@ void Plot::drawAnnotations(QRect annotationRect, QPainter *painter) { pos = g_MarkerC.pos; annotation = (char *)calloc(1, length); + if (annotation == NULL) { + return; + } snprintf(annotation, length, markerText, "C", @@ -897,6 +916,9 @@ void Plot::drawAnnotations(QRect annotationRect, QPainter *painter) { pos = g_MarkerD.pos; annotation = (char *)calloc(1, length); + if (annotation == NULL) { + return; + } snprintf(annotation, length, markerText, "D", diff --git a/client/src/proxmark3.c b/client/src/proxmark3.c index 2904aaab8..5723c3905 100644 --- a/client/src/proxmark3.c +++ b/client/src/proxmark3.c @@ -635,10 +635,20 @@ static void set_my_executable_path(void) { } my_executable_path = (char *)calloc(path_length + 1, sizeof(uint8_t)); + if (my_executable_path == NULL) { + PrintAndLogEx(WARNING, "Failed to allocate memory"); + return; + } + int dirname_length = 0; if (wai_getExecutablePath(my_executable_path, path_length, &dirname_length) != -1) { my_executable_path[path_length] = '\0'; my_executable_directory = (char *)calloc(dirname_length + 2, sizeof(uint8_t)); + if (my_executable_path == NULL) { + PrintAndLogEx(WARNING, "Failed to allocate memory"); + return; + } + strncpy(my_executable_directory, my_executable_path, dirname_length + 1); my_executable_directory[dirname_length + 1] = '\0'; } diff --git a/tools/hitag2crack/crack3/ht2crack3.c b/tools/hitag2crack/crack3/ht2crack3.c index 91e99c48d..3b4b73eea 100644 --- a/tools/hitag2crack/crack3/ht2crack3.c +++ b/tools/hitag2crack/crack3/ht2crack3.c @@ -333,7 +333,7 @@ int main(int argc, char *argv[]) { // read in nR aR pairs numnrar = 0; buf = (char *)calloc(1, lenbuf); - if (!buf) { + if (buf == NULL) { printf("cannot calloc buf\n"); exit(1); } diff --git a/tools/hitag2crack/crack4/ht2crack4.c b/tools/hitag2crack/crack4/ht2crack4.c index db66ab1a5..172d3607b 100644 --- a/tools/hitag2crack/crack4/ht2crack4.c +++ b/tools/hitag2crack/crack4/ht2crack4.c @@ -266,7 +266,7 @@ static void init_guess_table(char *filename, char *uidstr) { num_nRaR = 0; buf = (char *)calloc(1, lenbuf); - if (!buf) { + if (buf == NULL) { printf("cannot calloc buf\n"); exit(1); } diff --git a/tools/hitag2crack/crack5gpu_disabled/ht2crack5gpu.c b/tools/hitag2crack/crack5gpu_disabled/ht2crack5gpu.c index 77d470f22..f0e24a543 100644 --- a/tools/hitag2crack/crack5gpu_disabled/ht2crack5gpu.c +++ b/tools/hitag2crack/crack5gpu_disabled/ht2crack5gpu.c @@ -262,7 +262,7 @@ int main(int argc, char *argv[]) { } ctx.kernelSource = (char *)calloc(1, filestat.st_size); - if (!ctx.kernelSource) { + if (ctx.kernelSource == NULL) { printf("Cannot calloc kernelSource\n"); exit(1); }