From e48dfad49a392fc4523a7c49ce51c3fe64a3571b Mon Sep 17 00:00:00 2001 From: Micah Snyder Date: Wed, 20 Mar 2024 12:21:40 -0400 Subject: [PATCH] Windows: Fix C/Rust FFI compat issue + Windows compile warnings Primarily this commit fixes an issue with the size of the parameters passed to cli_checklimits(). The parameters were "unsigned long", which varies in size depending on platform. I've switched them to uint64_t / u64. While working on this, I observed some concerning warnigns on Windows, and some less serious ones, primarily regarding inconsistencies with `const` parameters. Finally, in `scanmem.c`, there is a warning regarding use of `wchar_t *` with `GetModuleFileNameEx()` instead of `GetModuleFileNameExW()`. This made me realize this code assumes we're not defining `UNICODE`, which would have such macros use the 'A' variant. I have fixed it the best I can, although I'm still a little uncomfortable with some of this code that uses `char` or `wchar_t` instead of TCHAR. I also remove the `if (GetModuleFileNameEx) {` conditional, because this macro/function will always be defined. The original code was checking a function pointer, and so this was a bug when integrating into ClamAV. Regarding the changes to `rijndael.c`, I found that this module assumes `unsigned long` == 32bits. It does not. I have corrected it to use `uint32_t`. --- clamd/tcpserver.c | 3 +- clamd/thrmgr.c | 4 +- clamdtop/clamdtop.c | 2 +- common/scanmem.c | 94 ++++++++++++++++++------------------- common/win/cert_util_win.c | 2 +- libclamav/dmg.c | 4 +- libclamav/hfsplus.c | 2 +- libclamav/jsparse/js-norm.c | 2 +- libclamav/matcher-hash.c | 8 ++-- libclamav/ole2_extract.c | 6 +-- libclamav/others.c | 10 ++-- libclamav/others.h | 69 ++++++++++++++------------- libclamav/others_common.c | 2 +- libclamav/pdf.c | 8 ++-- libclamav/rijndael.c | 66 ++++++++++++-------------- libclamav/rijndael.h | 12 +++-- libclamav/scanners.c | 4 +- libclamav/sis.c | 6 +-- libclamav/untar.c | 2 +- libclamav_rust/src/sys.rs | 6 +-- unit_tests/check_clamav.c | 5 +- unit_tests/check_clamd.c | 5 +- unit_tests/check_jsnorm.c | 2 +- unit_tests/check_regex.c | 2 +- win32/compat/net.c | 2 +- win32/compat/net.h | 2 +- win32/compat/utf8_util.c | 6 +-- win32/compat/utf8_util.h | 2 +- win32/compat/w32_errno.c | 2 +- win32/compat/w32_errno.h | 2 +- 30 files changed, 172 insertions(+), 170 deletions(-) diff --git a/clamd/tcpserver.c b/clamd/tcpserver.c index 92ce15ab9c..f7784ba698 100644 --- a/clamd/tcpserver.c +++ b/clamd/tcpserver.c @@ -56,7 +56,8 @@ int tcpserver(int **lsockets, unsigned int *nlsockets, char *ipaddr, const struc int *sockets; int sockfd = 0, backlog; int *t; - char *estr, port[10]; + const char *estr = NULL; + char port[10]; int yes = 1; int res; unsigned int i = 0; diff --git a/clamd/thrmgr.c b/clamd/thrmgr.c index d798ca20ee..4df6af90b0 100644 --- a/clamd/thrmgr.c +++ b/clamd/thrmgr.c @@ -267,7 +267,7 @@ int thrmgr_printstats(int f, char term) const struct cl_engine **s; /* new engine */ ++seen_cnt; - s = realloc(seen, seen_cnt * sizeof(*seen)); + s = realloc((void *)seen, seen_cnt * sizeof(*seen)); if (!s) { error_flag = 1; break; @@ -285,7 +285,7 @@ int thrmgr_printstats(int f, char term) } mdprintf(f, "\n"); } - free(seen); + free((void *)seen); #ifdef HAVE_MALLINFO { struct mallinfo inf = mallinfo(); diff --git a/clamdtop/clamdtop.c b/clamdtop/clamdtop.c index 181f987aff..b4dca64d3c 100644 --- a/clamdtop/clamdtop.c +++ b/clamdtop/clamdtop.c @@ -183,7 +183,7 @@ static WINDOW *stats_window = NULL; static WINDOW *status_bar_window = NULL; static WINDOW *mem_window = NULL; -static const char *status_bar_keys[10]; +static char *status_bar_keys[10]; static unsigned maxy = 0, maxx = 0; static char *queue_header = NULL; static char *multi_queue_header = NULL; diff --git a/common/scanmem.c b/common/scanmem.c index a0b4d4b5c2..a211d8e223 100644 --- a/common/scanmem.c +++ b/common/scanmem.c @@ -227,47 +227,47 @@ int walkmodules_th(proc_callback callback, void *data, struct mem_info *info) } /* Check and transform non ANSI filenames to ANSI using altnames */ - if (GetModuleFileNameEx) { - HANDLE hFile = CreateFile( - me32.szExePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL); - - if (hFile == INVALID_HANDLE_VALUE) { - DWORD err = GetLastError(); - wchar_t name[MAX_PATH + 1]; - char *converted = NULL; - HANDLE p; - - if (err == ERROR_BAD_NETPATH) { - logg(LOGG_WARNING, "Warning scanning files on non-ansi network paths is not " - "supported\n"); - logg(LOGG_WARNING, "File: %s\n", me32.szExePath); - continue; - } - - if ((err != ERROR_INVALID_NAME) && (err != ERROR_PATH_NOT_FOUND)) { - logg(LOGG_WARNING, "Expected ERROR_INVALID_NAME/ERROR_PATH_NOT_FOUND but got %d\n", - err); - continue; - } - - p = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, - ps.th32ProcessID); - if (!GetModuleFileNameEx(p, NULL, name, MAX_PATH)) { - logg(LOGG_WARNING, "GetModuleFileNameExW() failed %d\n", GetLastError()); - CloseHandle(p); - continue; - } + HANDLE hFile = CreateFileA( + me32.szExePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL); + + if (hFile == INVALID_HANDLE_VALUE) { + DWORD err = GetLastError(); + wchar_t nameW[MAX_PATH + 1]; + char *converted = NULL; + HANDLE p; + + if (err == ERROR_BAD_NETPATH) { + logg(LOGG_WARNING, "Warning scanning files on non-ansi network paths is not " + "supported\n"); + logg(LOGG_WARNING, "File: %s\n", me32.szExePath); + continue; + } + + if ((err != ERROR_INVALID_NAME) && (err != ERROR_PATH_NOT_FOUND)) { + logg(LOGG_WARNING, "Expected ERROR_INVALID_NAME/ERROR_PATH_NOT_FOUND but got %d\n", + err); + continue; + } + + p = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, + ps.th32ProcessID); + + if (!GetModuleFileNameExW(p, NULL, nameW, MAX_PATH)) { + logg(LOGG_WARNING, "GetModuleFileNameExW() failed %d\n", GetLastError()); CloseHandle(p); + continue; + } + CloseHandle(p); - if (!(converted = getaltpath(name))) { - logg(LOGG_WARNING, "Cannot map filename to ANSI codepage\n"); - continue; - } - strcpy(me32.szExePath, converted); - free(converted); - } else - CloseHandle(hFile); + if (!(converted = getaltpath(nameW))) { + logg(LOGG_WARNING, "Cannot map filename to ANSI codepage\n"); + continue; + } + strcpy(me32.szExePath, converted); + free(converted); + } else { + CloseHandle(hFile); } do @@ -319,8 +319,8 @@ int walkmodules_psapi(proc_callback callback, void *data, struct mem_info *info) continue; } - if (!GetModuleBaseName(hProc, mods[0], ps.szExeFile, - MAX_PATH - 1)) { + if (!GetModuleBaseNameA(hProc, mods[0], ps.szExeFile, + MAX_PATH - 1)) { CloseHandle(hProc); continue; } @@ -515,8 +515,8 @@ int dump_pe(const char *filename, PROCESSENTRY32 ProcStruct, /* PE Realignment */ align_pe(buffer, me32.modBaseSize); - hFile = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL, - CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL, + CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { logg(LOGG_INFO, "Error creating %s\n", filename); free(buffer); @@ -598,7 +598,7 @@ int scanmem_cb(PROCESSENTRY32 ProcStruct, MODULEENTRY32 me32, void *data, struct snprintf(expandmodule, MAX_PATH - 1, "%%SystemRoot%%\\%s", &me32.szExePath[12]); expandmodule[MAX_PATH - 1] = 0; - ExpandEnvironmentStrings(expandmodule, modulename, MAX_PATH - 1); + ExpandEnvironmentStringsA(expandmodule, modulename, MAX_PATH - 1); modulename[MAX_PATH - 1] = 0; } @@ -630,7 +630,7 @@ int scanmem_cb(PROCESSENTRY32 ProcStruct, MODULEENTRY32 me32, void *data, struct if ((fd = dump_pe(dumped, ProcStruct, me32)) > 0) { close(fd); scan_data->res = scanfile(dumped, scan_data, info); - DeleteFile(dumped); + DeleteFileA(dumped); } free(dumped); } @@ -667,8 +667,8 @@ int scanmem(struct mem_info *info) data.processes = 0; data.modules = 0; - HMODULE psapi_ok = LoadLibrary("psapi.dll"); - HMODULE k32_ok = LoadLibrary("kernel32.dll"); + HMODULE psapi_ok = LoadLibraryA("psapi.dll"); + HMODULE k32_ok = LoadLibraryA("kernel32.dll"); if (!(psapi_ok || k32_ok)) { logg(LOGG_INFO, " *** Memory Scanning is not supported on this OS ***\n\n"); diff --git a/common/win/cert_util_win.c b/common/win/cert_util_win.c index 022d5b7907..dbc7e9886e 100644 --- a/common/win/cert_util_win.c +++ b/common/win/cert_util_win.c @@ -49,7 +49,7 @@ cl_error_t cert_store_load(X509 **trusted_certs, size_t trusted_cert_count) cert_store_t *store = NULL; bool locked = false; - hStore = CertOpenSystemStoreA(NULL, "ROOT"); + hStore = CertOpenSystemStoreA(0, "ROOT"); if (NULL == hStore) { mprintf(LOGG_ERROR, "Failed to open system certificate store.\n"); goto done; diff --git a/libclamav/dmg.c b/libclamav/dmg.c index 61696c2024..e7fde7ef22 100644 --- a/libclamav/dmg.c +++ b/libclamav/dmg.c @@ -924,7 +924,7 @@ static int dmg_stripe_bzip(cli_ctx *ctx, int fd, uint32_t index, struct dmg_mish break; } - ret = cli_checklimits("dmg_stripe_bzip", ctx, (unsigned long)(size_so_far + sizeof(obuf)), 0, 0); + ret = cli_checklimits("dmg_stripe_bzip", ctx, size_so_far + sizeof(obuf), 0, 0); if (ret != CL_CLEAN) { break; } @@ -953,7 +953,7 @@ static int dmg_stripe_bzip(cli_ctx *ctx, int fd, uint32_t index, struct dmg_mish size_so_far += next_write; dmg_bzipmsg("dmg_stripe_bzip: size_so_far: " STDu64 " next_write: %zu\n", size_so_far, next_write); - ret = cli_checklimits("dmg_stripe_bzip", ctx, (unsigned long)(size_so_far + sizeof(obuf)), 0, 0); + ret = cli_checklimits("dmg_stripe_bzip", ctx, size_so_far + sizeof(obuf), 0, 0); if (ret != CL_CLEAN) { break; } diff --git a/libclamav/hfsplus.c b/libclamav/hfsplus.c index d9b510c1b8..034dcbf74e 100644 --- a/libclamav/hfsplus.c +++ b/libclamav/hfsplus.c @@ -344,7 +344,7 @@ static cl_error_t hfsplus_scanfile(cli_ctx *ctx, hfsPlusVolumeHeader *volHeader, goto done; } #endif - status = cli_checklimits("hfsplus_scanfile", ctx, (unsigned long)targetSize, 0, 0); + status = cli_checklimits("hfsplus_scanfile", ctx, targetSize, 0, 0); if (status != CL_SUCCESS) { goto done; } diff --git a/libclamav/jsparse/js-norm.c b/libclamav/jsparse/js-norm.c index 2a6d8354d3..a958028385 100644 --- a/libclamav/jsparse/js-norm.c +++ b/libclamav/jsparse/js-norm.c @@ -623,7 +623,7 @@ static void decode_de(yystype *params[], struct text_buffer *txtbuf) else textbuffer_append(txtbuf, tokens[val]); } while (*p); - free(tokens); + free((void *)tokens); textbuffer_append(txtbuf, "\0"); } diff --git a/libclamav/matcher-hash.c b/libclamav/matcher-hash.c index 809746f02b..75c1e72902 100644 --- a/libclamav/matcher-hash.c +++ b/libclamav/matcher-hash.c @@ -116,12 +116,12 @@ int hm_addhash_bin(struct cli_matcher *root, const void *binhash, cli_hash_type_ if (!szh->hash_array) { cli_errmsg("hm_addhash_bin: failed to grow hash array to %u entries\n", szh->items); szh->items = 0; - MPOOL_FREE(root->mempool, szh->virusnames); + MPOOL_FREE(root->mempool, (void *)szh->virusnames); szh->virusnames = NULL; return CL_EMEM; } - szh->virusnames = MPOOL_REALLOC2(root->mempool, szh->virusnames, sizeof(*szh->virusnames) * szh->items); + szh->virusnames = MPOOL_REALLOC2(root->mempool, (void *)szh->virusnames, sizeof(*szh->virusnames) * szh->items); if (!szh->virusnames) { cli_errmsg("hm_addhash_bin: failed to grow virusname array to %u entries\n", szh->items); szh->items = 0; @@ -319,7 +319,7 @@ void hm_free(struct cli_matcher *root) MPOOL_FREE(root->mempool, szh->hash_array); while (szh->items) MPOOL_FREE(root->mempool, (void *)szh->virusnames[--szh->items]); - MPOOL_FREE(root->mempool, szh->virusnames); + MPOOL_FREE(root->mempool, (void *)szh->virusnames); MPOOL_FREE(root->mempool, szh); } CLI_HTU32_FREE(ht, root->mempool); @@ -334,6 +334,6 @@ void hm_free(struct cli_matcher *root) MPOOL_FREE(root->mempool, szh->hash_array); while (szh->items) MPOOL_FREE(root->mempool, (void *)szh->virusnames[--szh->items]); - MPOOL_FREE(root->mempool, szh->virusnames); + MPOOL_FREE(root->mempool, (void *)szh->virusnames); } } diff --git a/libclamav/ole2_extract.c b/libclamav/ole2_extract.c index 38af43a541..9a49a1e8b7 100644 --- a/libclamav/ole2_extract.c +++ b/libclamav/ole2_extract.c @@ -1726,7 +1726,7 @@ static cl_error_t handler_otf_encrypted(ole2_header_t *hdr, property_t *prop, co int nrounds = 0; uint8_t *decryptDst = NULL; encryption_key_t *key = (encryption_key_t *)handler_ctx; - uint64_t *rk = NULL; + uint32_t *rk = NULL; uint32_t bytesRead = 0; uint64_t actualFileLength; uint64_t bytesWritten = 0; @@ -1746,7 +1746,7 @@ static cl_error_t handler_otf_encrypted(ole2_header_t *hdr, property_t *prop, co goto done; } - CLI_MAX_MALLOC_OR_GOTO_DONE(rk, RKLENGTH(key->key_length_bits) * sizeof(uint64_t), ret = CL_EMEM); + CLI_MAX_MALLOC_OR_GOTO_DONE(rk, RKLENGTH(key->key_length_bits) * sizeof(uint32_t), ret = CL_EMEM); print_ole2_property(prop); @@ -2143,7 +2143,7 @@ static cl_error_t generate_key_aes(const char *const password, encryption_key_t static bool aes_128ecb_decrypt(const unsigned char *in, size_t length, unsigned char *out, const encryption_key_t *const key) { - uint64_t rk[RKLENGTH(128)]; + uint32_t rk[RKLENGTH(128)]; int nrounds; size_t i; bool bRet = false; diff --git a/libclamav/others.c b/libclamav/others.c index d032840c28..9e6f9cecdd 100644 --- a/libclamav/others.c +++ b/libclamav/others.c @@ -250,7 +250,7 @@ static void *get_module_function(HMODULE handle, const char *name) NULL, lasterr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPCSTR)&err, + (LPSTR)&err, 0, NULL); } @@ -1132,10 +1132,10 @@ void cli_append_potentially_unwanted_if_heur_exceedsmax(cli_ctx *ctx, char *vnam } } -cl_error_t cli_checklimits(const char *who, cli_ctx *ctx, unsigned long need1, unsigned long need2, unsigned long need3) +cl_error_t cli_checklimits(const char *who, cli_ctx *ctx, uint64_t need1, uint64_t need2, uint64_t need3) { cl_error_t ret = CL_SUCCESS; - unsigned long needed; + uint64_t needed; if (!ctx) { /* if called without limits, go on, unpack, scan */ @@ -1156,7 +1156,7 @@ cl_error_t cli_checklimits(const char *who, cli_ctx *ctx, unsigned long need1, u /* Enforce global scan-size limit, if limit enabled */ if (needed && (ctx->engine->maxscansize != 0) && (ctx->engine->maxscansize - ctx->scansize < needed)) { /* The size needed is greater than the remaining scansize ... Skip this file. */ - cli_dbgmsg("%s: scansize exceeded (initial: %lu, consumed: %lu, needed: %lu)\n", who, (unsigned long int)ctx->engine->maxscansize, (unsigned long int)ctx->scansize, needed); + cli_dbgmsg("%s: scansize exceeded (initial: %lu, consumed: %lu, needed: %lu)\n", who, ctx->engine->maxscansize, ctx->scansize, needed); ret = CL_EMAXSIZE; cli_append_potentially_unwanted_if_heur_exceedsmax(ctx, "Heuristics.Limits.Exceeded.MaxScanSize"); goto done; @@ -1165,7 +1165,7 @@ cl_error_t cli_checklimits(const char *who, cli_ctx *ctx, unsigned long need1, u /* Enforce per-file file-size limit, if limit enabled */ if (needed && (ctx->engine->maxfilesize != 0) && (ctx->engine->maxfilesize < needed)) { /* The size needed is greater than that limit ... Skip this file. */ - cli_dbgmsg("%s: filesize exceeded (allowed: %lu, needed: %lu)\n", who, (unsigned long int)ctx->engine->maxfilesize, needed); + cli_dbgmsg("%s: filesize exceeded (allowed: %lu, needed: %lu)\n", who, ctx->engine->maxfilesize, needed); ret = CL_EMAXSIZE; cli_append_potentially_unwanted_if_heur_exceedsmax(ctx, "Heuristics.Limits.Exceeded.MaxFileSize"); goto done; diff --git a/libclamav/others.h b/libclamav/others.h index 4c12d6ea30..bd852326f4 100644 --- a/libclamav/others.h +++ b/libclamav/others.h @@ -593,16 +593,16 @@ extern LIBCLAMAV_EXPORT int have_rar; /* based on macros from A. Melnikoff */ #define cbswap16(v) (((v & 0xff) << 8) | (((v) >> 8) & 0xff)) -#define cbswap32(v) ((((v)&0x000000ff) << 24) | (((v)&0x0000ff00) << 8) | \ - (((v)&0x00ff0000) >> 8) | (((v)&0xff000000) >> 24)) -#define cbswap64(v) ((((v)&0x00000000000000ffULL) << 56) | \ - (((v)&0x000000000000ff00ULL) << 40) | \ - (((v)&0x0000000000ff0000ULL) << 24) | \ - (((v)&0x00000000ff000000ULL) << 8) | \ - (((v)&0x000000ff00000000ULL) >> 8) | \ - (((v)&0x0000ff0000000000ULL) >> 24) | \ - (((v)&0x00ff000000000000ULL) >> 40) | \ - (((v)&0xff00000000000000ULL) >> 56)) +#define cbswap32(v) ((((v) & 0x000000ff) << 24) | (((v) & 0x0000ff00) << 8) | \ + (((v) & 0x00ff0000) >> 8) | (((v) & 0xff000000) >> 24)) +#define cbswap64(v) ((((v) & 0x00000000000000ffULL) << 56) | \ + (((v) & 0x000000000000ff00ULL) << 40) | \ + (((v) & 0x0000000000ff0000ULL) << 24) | \ + (((v) & 0x00000000ff000000ULL) << 8) | \ + (((v) & 0x000000ff00000000ULL) >> 8) | \ + (((v) & 0x0000ff0000000000ULL) >> 24) | \ + (((v) & 0x00ff000000000000ULL) >> 40) | \ + (((v) & 0xff00000000000000ULL) >> 56)) #ifndef HAVE_ATTRIB_PACKED #define __attribute__(x) @@ -828,8 +828,8 @@ size_t cli_recursion_stack_get_size(cli_ctx *ctx, int index); /* used by: spin, yc (C) aCaB */ #define __SHIFTBITS(a) (sizeof(a) << 3) #define __SHIFTMASK(a) (__SHIFTBITS(a) - 1) -#define CLI_ROL(a, b) a = (a << ((b)&__SHIFTMASK(a))) | (a >> ((__SHIFTBITS(a) - (b)) & __SHIFTMASK(a))) -#define CLI_ROR(a, b) a = (a >> ((b)&__SHIFTMASK(a))) | (a << ((__SHIFTBITS(a) - (b)) & __SHIFTMASK(a))) +#define CLI_ROL(a, b) a = (a << ((b) & __SHIFTMASK(a))) | (a >> ((__SHIFTBITS(a) - (b)) & __SHIFTMASK(a))) +#define CLI_ROR(a, b) a = (a >> ((b) & __SHIFTMASK(a))) | (a << ((__SHIFTBITS(a) - (b)) & __SHIFTMASK(a))) /* Implementation independent sign-extended signed right shift */ #ifdef HAVE_SAR @@ -1143,7 +1143,7 @@ int cli_bitset_set(bitset_t *bs, unsigned long bit_offset); int cli_bitset_test(bitset_t *bs, unsigned long bit_offset); const char *cli_ctime(const time_t *timep, char *buf, const size_t bufsize); -cl_error_t cli_checklimits(const char *who, cli_ctx *ctx, unsigned long need1, unsigned long need2, unsigned long need3); +cl_error_t cli_checklimits(const char *who, cli_ctx *ctx, uint64_t need1, uint64_t need2, uint64_t need3); /** * @brief Call before scanning a file to determine if we should scan it, skip it, or abort the entire scanning process. @@ -1157,7 +1157,6 @@ cl_error_t cli_checklimits(const char *who, cli_ctx *ctx, unsigned long need1, u */ cl_error_t cli_updatelimits(cli_ctx *ctx, size_t needed); -unsigned long cli_getsizelimit(cli_ctx *, unsigned long); int cli_matchregex(const char *str, const char *regex); void cli_qsort(void *a, size_t n, size_t es, int (*cmp)(const void *, const void *)); void cli_qsort_r(void *a, size_t n, size_t es, int (*cmp)(const void *, const void *, const void *), void *arg); @@ -1333,7 +1332,7 @@ uint8_t cli_set_debug_flag(uint8_t debug_flag); #define CLI_FREE_AND_SET_NULL(var) \ do { \ if (NULL != var) { \ - free(var); \ + free((void *)var); \ var = NULL; \ } \ } while (0) @@ -1461,16 +1460,16 @@ uint8_t cli_set_debug_flag(uint8_t debug_flag); * @param ... The error handling code to execute if the allocation fails. */ #ifndef CLI_MAX_REALLOC_OR_GOTO_DONE -#define CLI_MAX_REALLOC_OR_GOTO_DONE(ptr, size, ...) \ - do { \ - void *vTmp = cli_max_realloc(ptr, size); \ - if (NULL == vTmp) { \ - do { \ - __VA_ARGS__; \ - } while (0); \ - goto done; \ - } \ - ptr = vTmp; \ +#define CLI_MAX_REALLOC_OR_GOTO_DONE(ptr, size, ...) \ + do { \ + void *vTmp = cli_max_realloc((void *)ptr, size); \ + if (NULL == vTmp) { \ + do { \ + __VA_ARGS__; \ + } while (0); \ + goto done; \ + } \ + ptr = vTmp; \ } while (0) #endif @@ -1486,16 +1485,16 @@ uint8_t cli_set_debug_flag(uint8_t debug_flag); * @param ... The error handling code to execute if the allocation fails. */ #ifndef CLI_SAFER_REALLOC_OR_GOTO_DONE -#define CLI_SAFER_REALLOC_OR_GOTO_DONE(ptr, size, ...) \ - do { \ - void *vTmp = cli_safer_realloc(ptr, size); \ - if (NULL == vTmp) { \ - do { \ - __VA_ARGS__; \ - } while (0); \ - goto done; \ - } \ - ptr = vTmp; \ +#define CLI_SAFER_REALLOC_OR_GOTO_DONE(ptr, size, ...) \ + do { \ + void *vTmp = cli_safer_realloc((void *)ptr, size); \ + if (NULL == vTmp) { \ + do { \ + __VA_ARGS__; \ + } while (0); \ + goto done; \ + } \ + ptr = vTmp; \ } while (0) #endif diff --git a/libclamav/others_common.c b/libclamav/others_common.c index ce549f0787..60d0179d2d 100644 --- a/libclamav/others_common.c +++ b/libclamav/others_common.c @@ -980,7 +980,7 @@ static cl_error_t cli_ftw_dir(const char *dirname, int flags, int maxdepth, cli_ * used */ const char *cli_strerror(int errnum, char *buf, size_t len) { - char *err; + const char *err; #ifdef CL_THREAD_SAFE pthread_mutex_lock(&cli_strerror_mutex); #endif diff --git a/libclamav/pdf.c b/libclamav/pdf.c index 7f1e506e4f..50080199b7 100644 --- a/libclamav/pdf.c +++ b/libclamav/pdf.c @@ -742,8 +742,8 @@ static size_t filter_writen(struct pdf_struct *pdf, struct pdf_obj *obj, int fou { UNUSEDPARAM(obj); - if (cli_checklimits("pdf", pdf->ctx, (unsigned long)*sum, 0, 0)) /* TODO: May truncate for large values on 64-bit platforms */ - return len; /* pretend it was a successful write to suppress CL_EWRITE */ + if (cli_checklimits("pdf", pdf->ctx, (uint64_t)*sum, 0, 0)) + return len; *sum += len; @@ -1083,7 +1083,7 @@ static void dbg_printhex(const char *msg, const char *hex, unsigned len); static void aes_256cbc_decrypt(const unsigned char *in, size_t *length, unsigned char *q, char *key, unsigned key_n, int has_iv) { - unsigned long rk[RKLENGTH(256)]; + uint32_t rk[RKLENGTH(256)]; unsigned char iv[16]; size_t len = 0; unsigned char pad, i; @@ -1171,7 +1171,7 @@ static void aes_256cbc_decrypt(const unsigned char *in, size_t *length, unsigned static void aes_128cbc_encrypt(const unsigned char *in, size_t in_length, unsigned char *out, size_t *out_length, const unsigned char *key, size_t key_n, const unsigned char *iv) { - unsigned long rk[RKLENGTH(128)]; + uint32_t rk[RKLENGTH(128)]; unsigned char real_iv[16] = {0}; int nrounds; uint8_t i = 0; diff --git a/libclamav/rijndael.c b/libclamav/rijndael.c index 4e54aa5abe..e2db914536 100644 --- a/libclamav/rijndael.c +++ b/libclamav/rijndael.c @@ -3,10 +3,7 @@ #include "rijndael.h" -typedef unsigned long u32; -typedef unsigned char u8; - -static const u32 Te0[256] = +static const uint32_t Te0[256] = { 0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU, 0xfff2f20dU, 0xd66b6bbdU, 0xde6f6fb1U, 0x91c5c554U, @@ -74,7 +71,7 @@ static const u32 Te0[256] = 0x7bb0b0cbU, 0xa85454fcU, 0x6dbbbbd6U, 0x2c16163aU, }; -static const u32 Te1[256] = +static const uint32_t Te1[256] = { 0xa5c66363U, 0x84f87c7cU, 0x99ee7777U, 0x8df67b7bU, 0x0dfff2f2U, 0xbdd66b6bU, 0xb1de6f6fU, 0x5491c5c5U, @@ -142,7 +139,7 @@ static const u32 Te1[256] = 0xcb7bb0b0U, 0xfca85454U, 0xd66dbbbbU, 0x3a2c1616U, }; -static const u32 Te2[256] = +static const uint32_t Te2[256] = { 0x63a5c663U, 0x7c84f87cU, 0x7799ee77U, 0x7b8df67bU, 0xf20dfff2U, 0x6bbdd66bU, 0x6fb1de6fU, 0xc55491c5U, @@ -210,7 +207,7 @@ static const u32 Te2[256] = 0xb0cb7bb0U, 0x54fca854U, 0xbbd66dbbU, 0x163a2c16U, }; -static const u32 Te3[256] = +static const uint32_t Te3[256] = { 0x6363a5c6U, 0x7c7c84f8U, 0x777799eeU, 0x7b7b8df6U, 0xf2f20dffU, 0x6b6bbdd6U, 0x6f6fb1deU, 0xc5c55491U, @@ -278,7 +275,7 @@ static const u32 Te3[256] = 0xb0b0cb7bU, 0x5454fca8U, 0xbbbbd66dU, 0x16163a2cU, }; -static const u32 Te4[256] = +static const uint32_t Te4[256] = { 0x63636363U, 0x7c7c7c7cU, 0x77777777U, 0x7b7b7b7bU, 0xf2f2f2f2U, 0x6b6b6b6bU, 0x6f6f6f6fU, 0xc5c5c5c5U, @@ -346,7 +343,7 @@ static const u32 Te4[256] = 0xb0b0b0b0U, 0x54545454U, 0xbbbbbbbbU, 0x16161616U, }; -static const u32 Td0[256] = +static const uint32_t Td0[256] = { 0x51f4a750U, 0x7e416553U, 0x1a17a4c3U, 0x3a275e96U, 0x3bab6bcbU, 0x1f9d45f1U, 0xacfa58abU, 0x4be30393U, @@ -414,7 +411,7 @@ static const u32 Td0[256] = 0x7bcb8461U, 0xd532b670U, 0x486c5c74U, 0xd0b85742U, }; -static const u32 Td1[256] = +static const uint32_t Td1[256] = { 0x5051f4a7U, 0x537e4165U, 0xc31a17a4U, 0x963a275eU, 0xcb3bab6bU, 0xf11f9d45U, 0xabacfa58U, 0x934be303U, @@ -482,7 +479,7 @@ static const u32 Td1[256] = 0x617bcb84U, 0x70d532b6U, 0x74486c5cU, 0x42d0b857U, }; -static const u32 Td2[256] = +static const uint32_t Td2[256] = { 0xa75051f4U, 0x65537e41U, 0xa4c31a17U, 0x5e963a27U, 0x6bcb3babU, 0x45f11f9dU, 0x58abacfaU, 0x03934be3U, @@ -550,7 +547,7 @@ static const u32 Td2[256] = 0x84617bcbU, 0xb670d532U, 0x5c74486cU, 0x5742d0b8U, }; -static const u32 Td3[256] = +static const uint32_t Td3[256] = { 0xf4a75051U, 0x4165537eU, 0x17a4c31aU, 0x275e963aU, 0xab6bcb3bU, 0x9d45f11fU, 0xfa58abacU, 0xe303934bU, @@ -618,7 +615,7 @@ static const u32 Td3[256] = 0xcb84617bU, 0x32b670d5U, 0x6c5c7448U, 0xb85742d0U, }; -static const u32 Td4[256] = +static const uint32_t Td4[256] = { 0x52525252U, 0x09090909U, 0x6a6a6a6aU, 0xd5d5d5d5U, 0x30303030U, 0x36363636U, 0xa5a5a5a5U, 0x38383838U, @@ -686,7 +683,7 @@ static const u32 Td4[256] = 0x55555555U, 0x21212121U, 0x0c0c0c0cU, 0x7d7d7d7dU, }; -static const u32 rcon[] = +static const uint32_t rcon[] = { 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000, 0x40000000, 0x80000000, @@ -694,28 +691,28 @@ static const u32 rcon[] = /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */ }; -#define GETU32(plaintext) (((u32)(plaintext)[0] << 24) ^ \ - ((u32)(plaintext)[1] << 16) ^ \ - ((u32)(plaintext)[2] << 8) ^ \ - ((u32)(plaintext)[3])) +#define GETU32(plaintext) (((uint32_t)(plaintext)[0] << 24) ^ \ + ((uint32_t)(plaintext)[1] << 16) ^ \ + ((uint32_t)(plaintext)[2] << 8) ^ \ + ((uint32_t)(plaintext)[3])) -#define PUTU32(ciphertext, st) { (ciphertext)[0] = (u8)((st) >> 24); \ - (ciphertext)[1] = (u8)((st) >> 16); \ - (ciphertext)[2] = (u8)((st) >> 8); \ - (ciphertext)[3] = (u8)(st); } +#define PUTU32(ciphertext, st) { (ciphertext)[0] = (uint8_t)((st) >> 24); \ + (ciphertext)[1] = (uint8_t)((st) >> 16); \ + (ciphertext)[2] = (uint8_t)((st) >> 8); \ + (ciphertext)[3] = (uint8_t)(st); } -int rijndaelSetupEncrypt(u32 *rk, const u8 *key, int keybits); -void rijndaelEncrypt(const u32 *rk, int nrounds, const u8 plaintext[16], u8 ciphertext[16]); +int rijndaelSetupEncrypt(uint32_t *rk, const uint8_t *key, int keybits); +void rijndaelEncrypt(const uint32_t *rk, int nrounds, const uint8_t plaintext[16], uint8_t ciphertext[16]); /** * Expand the cipher key into the encryption key schedule. * * @return the number of rounds for the given cipher key size. */ -int rijndaelSetupEncrypt(u32 *rk, const u8 *key, int keybits) +int rijndaelSetupEncrypt(uint32_t *rk, const uint8_t *key, int keybits) { int i = 0; - u32 temp; + uint32_t temp; rk[0] = GETU32(key ); rk[1] = GETU32(key + 4); @@ -801,10 +798,10 @@ int rijndaelSetupEncrypt(u32 *rk, const u8 *key, int keybits) * * @return the number of rounds for the given cipher key size. */ -int rijndaelSetupDecrypt(u32 *rk, const u8 *key, int keybits) +int rijndaelSetupDecrypt(uint32_t *rk, const uint8_t *key, int keybits) { int nrounds, i, j; - u32 temp; + uint32_t temp; /* expand the cipher key: */ nrounds = rijndaelSetupEncrypt(rk, key, keybits); @@ -844,10 +841,10 @@ int rijndaelSetupDecrypt(u32 *rk, const u8 *key, int keybits) return nrounds; } -void rijndaelEncrypt(const u32 *rk, int nrounds, const u8 plaintext[16], - u8 ciphertext[16]) +void rijndaelEncrypt(const uint32_t *rk, int nrounds, const uint8_t plaintext[16], + uint8_t ciphertext[16]) { - u32 s0, s1, s2, s3, t0, t1, t2, t3; + uint32_t s0, s1, s2, s3, t0, t1, t2, t3; #ifndef FULL_UNROLL int r; #endif /* ?FULL_UNROLL */ @@ -1026,10 +1023,10 @@ void rijndaelEncrypt(const u32 *rk, int nrounds, const u8 plaintext[16], PUTU32(ciphertext + 12, s3); } -void rijndaelDecrypt(const u32 *rk, int nrounds, const u8 ciphertext[16], - u8 plaintext[16]) +void rijndaelDecrypt(const uint32_t *rk, int nrounds, const uint8_t ciphertext[16], + uint8_t plaintext[16]) { - u32 s0, s1, s2, s3, t0, t1, t2, t3; + uint32_t s0, s1, s2, s3, t0, t1, t2, t3; #ifndef FULL_UNROLL int r; #endif /* ?FULL_UNROLL */ @@ -1208,4 +1205,3 @@ void rijndaelDecrypt(const u32 *rk, int nrounds, const u8 ciphertext[16], rk[3]; PUTU32(plaintext + 12, s3); } - diff --git a/libclamav/rijndael.h b/libclamav/rijndael.h index 153673cd54..cc6d8cad05 100644 --- a/libclamav/rijndael.h +++ b/libclamav/rijndael.h @@ -1,15 +1,17 @@ +/* public domain code from http://www.efgh.com/software/rijndael.htm */ #ifndef H__RIJNDAEL #define H__RIJNDAEL -int rijndaelSetupDecrypt(unsigned long *rk, const unsigned char *key, int keybits); -void rijndaelDecrypt(const unsigned long *rk, int nrounds, const unsigned char ciphertext[16], unsigned char plaintext[16]); +#include "clamav-types.h" -int rijndaelSetupEncrypt(unsigned long *rk, const unsigned char *key, int keybits); -void rijndaelEncrypt(const unsigned long *rk, int nrounds, const unsigned char plaintext[16], unsigned char ciphertext[16]); +int rijndaelSetupDecrypt(uint32_t *rk, const unsigned char *key, int keybits); +void rijndaelDecrypt(const uint32_t *rk, int nrounds, const unsigned char ciphertext[16], unsigned char plaintext[16]); + +int rijndaelSetupEncrypt(uint32_t *rk, const unsigned char *key, int keybits); +void rijndaelEncrypt(const uint32_t *rk, int nrounds, const unsigned char plaintext[16], unsigned char ciphertext[16]); #define KEYLENGTH(keybits) ((keybits)/8) #define RKLENGTH(keybits) ((keybits)/8+28) #define NROUNDS(keybits) ((keybits)/32+6) #endif - diff --git a/libclamav/scanners.c b/libclamav/scanners.c index 035a990c07..34544323b3 100644 --- a/libclamav/scanners.c +++ b/libclamav/scanners.c @@ -1556,8 +1556,6 @@ static cl_error_t vba_scandata(const unsigned char *data, size_t len, cli_ctx *c return ret; } -#define min(x, y) ((x) < (y) ? (x) : (y)) - /** * Find a file in a directory tree. * \param filename Name of the file to find @@ -1596,7 +1594,7 @@ cl_error_t find_file(const char *filename, const char *dir, char *result, size_t } } else if (S_ISREG(statbuf.st_mode)) { if (strcmp(dent->d_name, filename) == 0) { - len = min(strlen(dir) + 1, result_size); + len = MIN(strlen(dir) + 1, result_size); memcpy(result, dir, len); result[len - 1] = '\0'; closedir(dd); diff --git a/libclamav/sis.c b/libclamav/sis.c index 7c277b4358..c3de1983c0 100644 --- a/libclamav/sis.c +++ b/libclamav/sis.c @@ -166,14 +166,14 @@ enum { else { \ if ((N) < sleft) { \ cli_dbgmsg("SIS: Refusing to seek back\n"); \ - free(alangs); \ + free((void *)alangs); \ return CL_CLEAN; \ } \ pos += (N)-sleft; \ size_t tmp = fmap_readn(map, buff, pos, BUFSIZ); \ if (((size_t)-1) == tmp) { \ cli_dbgmsg("SIS: Read failed during SKIP\n"); \ - free(alangs); \ + free((void *)alangs); \ return CL_CLEAN; \ } \ sleft = smax = tmp; \ @@ -628,7 +628,7 @@ enum { T_INVALID, const char *sisfields[] = {"Invalid", "String", "Array", "Compressed", "Version", "VersionRange", "Date", "Time", "DateTime", "Uid", "Unused", "Language", "Contents", "Controller", "Info", "SupportedLanguages", "SupportedOptions", "Prerequisites", "Dependency", "Properties", "Property", "Signatures", "CertificateChain", "Logo", "FileDescription", "Hash", "If", "ElseIf", "InstallBlock", "Expression", "Data", "DataUnit", "FileData", "SupportedOption", "ControllerChecksum", "DataChecksum", "Signature", "Blob", "SignatureAlgorithm", "SignatureCertificateChain", "DataIndex", "Capabilities"}; -#define ALIGN4(x) (((x) & ~3) + ((((x)&1) | (((x) >> 1) & 1)) << 2)) +#define ALIGN4(x) (((x) & ~3) + ((((x) & 1) | (((x) >> 1) & 1)) << 2)) #define HERE printf("here\n"), abort(); diff --git a/libclamav/untar.c b/libclamav/untar.c index 8bb026e0e2..aea8aea61c 100644 --- a/libclamav/untar.c +++ b/libclamav/untar.c @@ -331,7 +331,7 @@ cl_error_t cli_untar(const char *dir, unsigned int posix, cli_ctx *ctx) if (limitnear > 0) { currsize += nbytes; cli_dbgmsg("cli_untar: Approaching limit...\n"); - if (cli_checklimits("cli_untar", ctx, (unsigned long)currsize, 0, 0) != CL_SUCCESS) { + if (cli_checklimits("cli_untar", ctx, (uint64_t)currsize, 0, 0) != CL_SUCCESS) { // Limit would be exceeded by this file, suppress writing beyond limit // Need to keep reading to get to end of file chunk skipwrite++; diff --git a/libclamav_rust/src/sys.rs b/libclamav_rust/src/sys.rs index df3578eb48..399ec438a4 100644 --- a/libclamav_rust/src/sys.rs +++ b/libclamav_rust/src/sys.rs @@ -811,9 +811,9 @@ extern "C" { pub fn cli_checklimits( who: *const ::std::os::raw::c_char, ctx: *mut cli_ctx, - need1: ::std::os::raw::c_ulong, - need2: ::std::os::raw::c_ulong, - need3: ::std::os::raw::c_ulong, + need1: u64, + need2: u64, + need3: u64, ) -> cl_error_t; } extern "C" { diff --git a/unit_tests/check_clamav.c b/unit_tests/check_clamav.c index 0a46e7d45d..36e9a08e10 100644 --- a/unit_tests/check_clamav.c +++ b/unit_tests/check_clamav.c @@ -2027,12 +2027,15 @@ static void check_version_compatible() } #endif -int main(void) +int main(int argc, char **argv) { int nf; Suite *s; SRunner *sr; + UNUSEDPARAM(argc); + UNUSEDPARAM(argv); + cl_initialize_crypto(); fpu_words = get_fpu_endian(); diff --git a/unit_tests/check_clamd.c b/unit_tests/check_clamd.c index 78b90b637c..2f526709a7 100644 --- a/unit_tests/check_clamd.c +++ b/unit_tests/check_clamd.c @@ -890,10 +890,13 @@ static Suite *test_clamd_suite(void) return s; } -int main(void) +int main(int argc, char **argv) { int num_fds; + UNUSEDPARAM(argc); + UNUSEDPARAM(argv); + #ifdef _WIN32 WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != NO_ERROR) { diff --git a/unit_tests/check_jsnorm.c b/unit_tests/check_jsnorm.c index 7763e7cb4f..50939ba88d 100644 --- a/unit_tests/check_jsnorm.c +++ b/unit_tests/check_jsnorm.c @@ -114,7 +114,7 @@ END_TEST START_TEST(test_token_scope) { - struct scope *sc = (struct scope *)0xdeadbeef; + struct scope *sc = (struct scope *)(uint64_t)0xdeadbeef; yystype tok; memset(&tok, 0, sizeof(tok)); diff --git a/unit_tests/check_regex.c b/unit_tests/check_regex.c index 56593dee72..db24de6616 100644 --- a/unit_tests/check_regex.c +++ b/unit_tests/check_regex.c @@ -143,7 +143,7 @@ START_TEST(test_suffix) ck_assert_msg(!!pattern, "test pattern"); preg = malloc(sizeof(*regex.preg)); ck_assert_msg(!!preg, "malloc"); - rc = cli_regex2suffix(pattern, preg, cb_expect_multi, tests[_i]); + rc = cli_regex2suffix(pattern, preg, cb_expect_multi, (void *)tests[_i]); ck_assert_msg(rc == CL_SUCCESS, "single character pattern"); cli_regfree(preg); free(preg); diff --git a/win32/compat/net.c b/win32/compat/net.c index 9f442a6bb9..792a848626 100644 --- a/win32/compat/net.c +++ b/win32/compat/net.c @@ -355,7 +355,7 @@ int w32_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, s return ret; } -int w32_accept(SOCKET sockfd, const struct sockaddr *addr, socklen_t *addrlen) +int w32_accept(SOCKET sockfd, struct sockaddr *addr, socklen_t *addrlen) { if ((sockfd = accept(sockfd, addr, addrlen)) == INVALID_SOCKET) { wsock2errno(); diff --git a/win32/compat/net.h b/win32/compat/net.h index 4b928d2644..f2eadef79f 100644 --- a/win32/compat/net.h +++ b/win32/compat/net.h @@ -53,7 +53,7 @@ void w32_freeaddrinfo(struct addrinfo *res); const char *w32_inet_ntop(int af, const void *src, char *dst, socklen_t size); int w32_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); int poll_with_event(struct pollfd *fds, int nfds, int timeout, HANDLE event); -int w32_accept(SOCKET sockfd, const struct sockaddr *addr, socklen_t *addrlen); +int w32_accept(SOCKET sockfd, struct sockaddr *addr, socklen_t *addrlen); int w32_listen(int sockfd, int backlog); int w32_shutdown(int sockfd, int how); int w32_getpeername(int sd, struct sockaddr *name, int *namelen); diff --git a/win32/compat/utf8_util.c b/win32/compat/utf8_util.c index 13e077ee4d..d5fe20a108 100644 --- a/win32/compat/utf8_util.c +++ b/win32/compat/utf8_util.c @@ -30,10 +30,10 @@ char *cli_strdup_to_utf8(const char *s) { - char *r = cli_to_utf8_maybe_alloc(s); + const char *r = cli_to_utf8_maybe_alloc(s); if (!r) return NULL; if (r == s) return _strdup(r); - return r; + return (char *)r; } #define MAYBE_FREE_W \ @@ -44,7 +44,7 @@ char *cli_strdup_to_utf8(const char *s) do { \ if (utf8 != tmpu) free(utf8); \ } while (0) -char *cli_to_utf8_maybe_alloc(const char *s) +const char *cli_to_utf8_maybe_alloc(const char *s) { int len = strlen(s) + 1; wchar_t tmpw[1024], *wdup; diff --git a/win32/compat/utf8_util.h b/win32/compat/utf8_util.h index 96db15fe9b..24bd1cf36a 100644 --- a/win32/compat/utf8_util.h +++ b/win32/compat/utf8_util.h @@ -26,7 +26,7 @@ #include #include -char *cli_to_utf8_maybe_alloc(const char *s); +const char *cli_to_utf8_maybe_alloc(const char *s); char *cli_strdup_to_utf8(const char *s); #endif diff --git a/win32/compat/w32_errno.c b/win32/compat/w32_errno.c index a8272c06e1..a4a211a745 100644 --- a/win32/compat/w32_errno.c +++ b/win32/compat/w32_errno.c @@ -22,7 +22,7 @@ #include #include "w32_errno.h" -char *w32_strerror(int errnum) +const char *w32_strerror(int errnum) { size_t i; for (i = 0; i < sizeof(w32_errnos) / sizeof(w32_errnos[0]); i++) { diff --git a/win32/compat/w32_errno.h b/win32/compat/w32_errno.h index 700fb31371..5e3e585632 100644 --- a/win32/compat/w32_errno.h +++ b/win32/compat/w32_errno.h @@ -28,7 +28,7 @@ #include #include "w32_errno_defs.c" -char *w32_strerror(int errnum); +const char *w32_strerror(int errnum); int w32_strerror_r(int errnum, char *buf, size_t buflen); #endif