Skip to content

Commit 149c721

Browse files
committed
Tests: Fix a couple of valgrind complaints
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
1 parent 1e29025 commit 149c721

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

clamdscan/client.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ int16_t ping_clamd(const struct optstruct *opts)
175175
char *errchk = NULL;
176176
uint64_t i = 0;
177177
const struct optstruct *opt = NULL;
178-
int64_t sockd;
178+
int64_t sockd = -1;
179179
struct RCVLN rcv;
180180
uint16_t ret = 0;
181181

@@ -227,6 +227,7 @@ int16_t ping_clamd(const struct optstruct *opts)
227227
if (sendln(sockd, zPING, sizeof(zPING))) {
228228
logg(LOGG_DEBUG, "PING failed...\n");
229229
closesocket(sockd);
230+
sockd = -1;
230231
} else {
231232
if (!optget(opts, "wait")->enabled) {
232233
logg(LOGG_INFO, "PONG\n");
@@ -262,6 +263,9 @@ int16_t ping_clamd(const struct optstruct *opts)
262263
}
263264

264265
done:
266+
if (sockd >= 0) {
267+
closesocket(sockd);
268+
}
265269
if (attempt_str) {
266270
free(attempt_str);
267271
}

libclamav/entconv.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,9 +526,11 @@ static void iconv_cache_init(struct iconv_cache* cache)
526526
static void iconv_cache_destroy(struct iconv_cache* cache)
527527
{
528528
size_t i;
529-
cli_dbgmsg(MODULE_NAME "Destroying iconv pool:%p\n", (void*)cache);
529+
// Don't use cli_dbgmsg() in destroy, because this happens *after* main() exits and we've already closed the log file handle.
530+
//printf(MODULE_NAME "Destroying iconv pool:%p\n", (void*)cache);
530531
for (i = 0; i < cache->last; i++) {
531-
cli_dbgmsg(MODULE_NAME "closing iconv:%p\n", cache->tab[i]);
532+
// Don't log on destroy, because this happens *after* main() exits and we've already closed the log file handle.
533+
//printf(MODULE_NAME "closing iconv:%p\n", cache->tab[i]);
532534
iconv_close(cache->tab[i]);
533535
}
534536
cli_hashtab_clear(&cache->hashtab);

unit_tests/check_clamav.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2012,6 +2012,7 @@ int main(int argc, char **argv)
20122012
int nf;
20132013
Suite *s;
20142014
SRunner *sr;
2015+
FILE *log_file = NULL;
20152016

20162017
UNUSEDPARAM(argc);
20172018
UNUSEDPARAM(argv);
@@ -2037,7 +2038,8 @@ int main(int argc, char **argv)
20372038
srunner_add_suite(sr, test_bytecode_suite());
20382039

20392040
srunner_set_log(sr, OBJDIR PATHSEP "test.log");
2040-
if (freopen(OBJDIR PATHSEP "test-stderr.log", "w+", stderr) == NULL) {
2041+
log_file = freopen(OBJDIR PATHSEP "test-stderr.log", "w+", stderr);
2042+
if (log_file == NULL) {
20412043
// The stderr FILE pointer may be closed by `freopen()` even if redirecting to the log file files.
20422044
// So we will output the error message to stdout instead.
20432045
fputs("Unable to redirect stderr!\n", stdout);
@@ -2052,5 +2054,9 @@ int main(int argc, char **argv)
20522054

20532055
xmlCleanupParser();
20542056

2057+
if (log_file) {
2058+
fclose(log_file);
2059+
}
2060+
20552061
return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
20562062
}

0 commit comments

Comments
 (0)