Skip to content

Commit 0c45e5c

Browse files
committed
Tests: Fix a couple of valgrind complaints
Fix valgrind issues regarding: - Unclosed socket file descriptor in libclamav unit test program. Will just suppress this one. - 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. - Attempting to close the same socket file descriptor(s) more than once when shutting down clamd.
1 parent 7fab05a commit 0c45e5c

File tree

6 files changed

+27
-4
lines changed

6 files changed

+27
-4
lines changed

clamd/clamd.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,10 @@ int main(int argc, char **argv)
904904
logg(LOGG_DEBUG, "Closing the main socket%s.\n", (nlsockets > 1) ? "s" : "");
905905

906906
for (i = 0; i < nlsockets; i++) {
907+
if (lsockets[i] == -1)
908+
continue;
907909
closesocket(lsockets[i]);
910+
lsockets[i] = -1;
908911
}
909912
#ifndef _WIN32
910913
if (nlsockets && localsock) {

clamd/server-th.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,7 @@ static void *acceptloop_th(void *arg)
652652
logg(LOGG_DEBUG_NV, "Shutdown: closed fd %d\n", fds->buf[i].fd);
653653
shutdown(fds->buf[i].fd, 2);
654654
closesocket(fds->buf[i].fd);
655+
fds->buf[i].fd = -1;
655656
}
656657
}
657658

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
}

unit_tests/valgrind.supp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,10 @@
376376
fun:start_thread
377377
fun:clone
378378
}
379+
{
380+
<clamd_localserver_socket>
381+
CoreError:FdNotClosed
382+
fun:socket
383+
fun:localserver
384+
fun:main
385+
}

0 commit comments

Comments
 (0)