From d758c0053764b323e42c0be11b5c15d816e80f80 Mon Sep 17 00:00:00 2001 From: "Val S." Date: Tue, 9 Sep 2025 12:35:14 -0400 Subject: [PATCH] Tests: Fix a couple of valgrind complaints (#1554) Fix valgrind issues regarding: - Unclosed log file descriptor in libclamav unit test program. Also need to disable debug logging for `iconv_cache_destroy()` for this or else it will try to use that file descriptor after `main()` exits. - Unclosed socket file descriptor in ClamDScan when doing `ping()` function. CLAM-2872 --- clamdscan/client.c | 6 +++++- libclamav/entconv.c | 6 ++++-- unit_tests/check_clamav.c | 8 +++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/clamdscan/client.c b/clamdscan/client.c index 7ab0d23a8..51f9744be 100644 --- a/clamdscan/client.c +++ b/clamdscan/client.c @@ -175,7 +175,7 @@ int16_t ping_clamd(const struct optstruct *opts) char *errchk = NULL; uint64_t i = 0; const struct optstruct *opt = NULL; - int64_t sockd; + int64_t sockd = -1; struct RCVLN rcv; uint16_t ret = 0; @@ -227,6 +227,7 @@ int16_t ping_clamd(const struct optstruct *opts) if (sendln(sockd, zPING, sizeof(zPING))) { logg(LOGG_DEBUG, "PING failed...\n"); closesocket(sockd); + sockd = -1; } else { if (!optget(opts, "wait")->enabled) { logg(LOGG_INFO, "PONG\n"); @@ -262,6 +263,9 @@ int16_t ping_clamd(const struct optstruct *opts) } done: + if (sockd >= 0) { + closesocket(sockd); + } if (attempt_str) { free(attempt_str); } diff --git a/libclamav/entconv.c b/libclamav/entconv.c index 37770cbf9..131a4ed10 100644 --- a/libclamav/entconv.c +++ b/libclamav/entconv.c @@ -526,9 +526,11 @@ static void iconv_cache_init(struct iconv_cache* cache) static void iconv_cache_destroy(struct iconv_cache* cache) { size_t i; - cli_dbgmsg(MODULE_NAME "Destroying iconv pool:%p\n", (void*)cache); + // Don't use cli_dbgmsg() in destroy, because this happens *after* main() exits and we've already closed the log file handle. + //printf(MODULE_NAME "Destroying iconv pool:%p\n", (void*)cache); for (i = 0; i < cache->last; i++) { - cli_dbgmsg(MODULE_NAME "closing iconv:%p\n", cache->tab[i]); + // Don't log on destroy, because this happens *after* main() exits and we've already closed the log file handle. + //printf(MODULE_NAME "closing iconv:%p\n", cache->tab[i]); iconv_close(cache->tab[i]); } cli_hashtab_clear(&cache->hashtab); diff --git a/unit_tests/check_clamav.c b/unit_tests/check_clamav.c index 0bd02d543..d21f511d3 100644 --- a/unit_tests/check_clamav.c +++ b/unit_tests/check_clamav.c @@ -2012,6 +2012,7 @@ int main(int argc, char **argv) int nf; Suite *s; SRunner *sr; + FILE *log_file = NULL; UNUSEDPARAM(argc); UNUSEDPARAM(argv); @@ -2037,7 +2038,8 @@ int main(int argc, char **argv) srunner_add_suite(sr, test_bytecode_suite()); srunner_set_log(sr, OBJDIR PATHSEP "test.log"); - if (freopen(OBJDIR PATHSEP "test-stderr.log", "w+", stderr) == NULL) { + log_file = freopen(OBJDIR PATHSEP "test-stderr.log", "w+", stderr); + if (log_file == NULL) { // The stderr FILE pointer may be closed by `freopen()` even if redirecting to the log file files. // So we will output the error message to stdout instead. fputs("Unable to redirect stderr!\n", stdout); @@ -2052,5 +2054,9 @@ int main(int argc, char **argv) xmlCleanupParser(); + if (log_file) { + fclose(log_file); + } + return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE; }