From 5aa8ee7b6b55f31aae558758d98c689ebe87bc75 Mon Sep 17 00:00:00 2001 From: Patrick Zhang Date: Tue, 26 Aug 2025 21:53:19 +0000 Subject: [PATCH] Fix issues found by Coverity --- misc/dictionary.txt | 1 + src/aws_sigv4.c | 8 ++++---- src/ring_buffer.c | 4 ++-- src/system-log-forwarder.c | 6 ++++-- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/misc/dictionary.txt b/misc/dictionary.txt index 41c50e4..7c206a4 100644 --- a/misc/dictionary.txt +++ b/misc/dictionary.txt @@ -8,6 +8,7 @@ cflags Chnage clangd coreutils +coverity DENABLE DFETCHCONTENT dont diff --git a/src/aws_sigv4.c b/src/aws_sigv4.c index c20eca2..317a081 100644 --- a/src/aws_sigv4.c +++ b/src/aws_sigv4.c @@ -26,7 +26,7 @@ static int32_t hash_init(void *ctx) { ret = 0; } else { unsigned long err = ERR_get_error(); - char err_buf[256]; + char err_buf[256] = { 0 }; ERR_error_string_n(err, err_buf, sizeof(err_buf)); GGL_LOGE("EVP_DigestInit failed: %s", err_buf); ret = -1; @@ -45,7 +45,7 @@ static int32_t hash_update(void *ctx, const uint8_t *data, size_t data_len) { ret = 0; } else { unsigned long err = ERR_get_error(); - char err_buf[256]; + char err_buf[256] = { 0 }; ERR_error_string_n(err, err_buf, sizeof(err_buf)); GGL_LOGE("EVP_DigestUpdate failed: %s", err_buf); ret = -1; @@ -66,7 +66,7 @@ static int32_t hash_final( ret = 0; } else { unsigned long err = ERR_get_error(); - char err_buf[256]; + char err_buf[256] = { 0 }; ERR_error_string_n(err, err_buf, sizeof(err_buf)); GGL_LOGE("EVP_DigestFinal failed: %s", err_buf); ret = -1; @@ -76,7 +76,7 @@ static int32_t hash_final( } static GglError translate_sigv4_error(SigV4Status_t status) { - GglError ret = { 0 }; + GglError ret; switch (status) { case SigV4Success: diff --git a/src/ring_buffer.c b/src/ring_buffer.c index f94b4c6..a488bbe 100644 --- a/src/ring_buffer.c +++ b/src/ring_buffer.c @@ -212,14 +212,14 @@ void slf_log_store_wait_for_upload_trigger(time_t timeout_sec) { pthread_mutex_lock(&upload_mutex); int wait_result = 0; - do { + while (!upload_now) { wait_result = pthread_cond_timedwait(&upload_cond, &upload_mutex, &timeout); if (wait_result == ETIMEDOUT) { GGL_LOGW("Timed out waiting for a upload."); break; } - } while (!upload_now); + } upload_now = false; pthread_mutex_unlock(&upload_mutex); } diff --git a/src/system-log-forwarder.c b/src/system-log-forwarder.c index fe34a50..0fded46 100644 --- a/src/system-log-forwarder.c +++ b/src/system-log-forwarder.c @@ -117,7 +117,7 @@ static GglError drain_ring_buf_and_upload( static void *consumer_thread(void *arg) { Config *config = (Config *) arg; - uint8_t upload_mem[MAX_UPLOAD_SIZE] = { 0 }; + static uint8_t upload_mem[MAX_UPLOAD_SIZE] = { 0 }; GglByteVec upload_doc = GGL_BYTE_VEC(upload_mem); static uint8_t timestamp_mem[MAX_TIMESTAMP_DIGITS] = { 0 }; GglBuffer timestamp_as_buffer = GGL_BUF(timestamp_mem); @@ -132,6 +132,7 @@ static void *consumer_thread(void *arg) { ); } + // coverity[infinite_loop] while (true) { time_t now = time(NULL); if (last_uploaded == 0) { @@ -171,7 +172,6 @@ static void *consumer_thread(void *arg) { ret ); } - continue; } } return NULL; @@ -361,9 +361,11 @@ static error_t safe_str_to_int(const char *str, int *result) { static error_t safe_str_to_size_t(const char *str, size_t *result) { unsigned long temp_val = strtoul(str, NULL, 10); +#if ULONG_MAX > SIZE_MAX if (temp_val > SIZE_MAX) { return ARGP_ERR_UNKNOWN; } +#endif *result = (size_t) temp_val; return 0; }