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
This commit is contained in:
Val S. 2025-09-09 12:35:14 -04:00 committed by GitHub
parent 47b9e08e1a
commit d758c00537
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 16 additions and 4 deletions

View file

@ -175,7 +175,7 @@ int16_t ping_clamd(const struct optstruct *opts)
char *errchk = NULL; char *errchk = NULL;
uint64_t i = 0; uint64_t i = 0;
const struct optstruct *opt = NULL; const struct optstruct *opt = NULL;
int64_t sockd; int64_t sockd = -1;
struct RCVLN rcv; struct RCVLN rcv;
uint16_t ret = 0; uint16_t ret = 0;
@ -227,6 +227,7 @@ int16_t ping_clamd(const struct optstruct *opts)
if (sendln(sockd, zPING, sizeof(zPING))) { if (sendln(sockd, zPING, sizeof(zPING))) {
logg(LOGG_DEBUG, "PING failed...\n"); logg(LOGG_DEBUG, "PING failed...\n");
closesocket(sockd); closesocket(sockd);
sockd = -1;
} else { } else {
if (!optget(opts, "wait")->enabled) { if (!optget(opts, "wait")->enabled) {
logg(LOGG_INFO, "PONG\n"); logg(LOGG_INFO, "PONG\n");
@ -262,6 +263,9 @@ int16_t ping_clamd(const struct optstruct *opts)
} }
done: done:
if (sockd >= 0) {
closesocket(sockd);
}
if (attempt_str) { if (attempt_str) {
free(attempt_str); free(attempt_str);
} }

View file

@ -526,9 +526,11 @@ static void iconv_cache_init(struct iconv_cache* cache)
static void iconv_cache_destroy(struct iconv_cache* cache) static void iconv_cache_destroy(struct iconv_cache* cache)
{ {
size_t i; 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++) { 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]); iconv_close(cache->tab[i]);
} }
cli_hashtab_clear(&cache->hashtab); cli_hashtab_clear(&cache->hashtab);

View file

@ -2012,6 +2012,7 @@ int main(int argc, char **argv)
int nf; int nf;
Suite *s; Suite *s;
SRunner *sr; SRunner *sr;
FILE *log_file = NULL;
UNUSEDPARAM(argc); UNUSEDPARAM(argc);
UNUSEDPARAM(argv); UNUSEDPARAM(argv);
@ -2037,7 +2038,8 @@ int main(int argc, char **argv)
srunner_add_suite(sr, test_bytecode_suite()); srunner_add_suite(sr, test_bytecode_suite());
srunner_set_log(sr, OBJDIR PATHSEP "test.log"); 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. // 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. // So we will output the error message to stdout instead.
fputs("Unable to redirect stderr!\n", stdout); fputs("Unable to redirect stderr!\n", stdout);
@ -2052,5 +2054,9 @@ int main(int argc, char **argv)
xmlCleanupParser(); xmlCleanupParser();
if (log_file) {
fclose(log_file);
}
return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE; return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
} }