Refine max-allocation and safer-allocation function and macro names

We add the _OR_GOTO_DONE suffix to the macros that go to done if the
allocation fails. This makes it obvious what is different about the
macro versus the equivalent function, and that error handling is
built-in.

Renamed the cli_strdup to safer_strdup to make it obvious that it exists
because it is safer than regular strdup. Regular strdup doesn't have the
NULL check before trying to dup, and so may result in a NULL-deref
crash.

Also remove unused STRDUP (_OR_GOTO_DONE) macro, since the one with the
NULL-check is preferred.
pull/859/head^2
Micah Snyder 1 year ago committed by Micah Snyder
parent 39070d1c76
commit 405829ee88
  1. 2
      clamdscan/client.c
  2. 2
      clamonacc/client/client.c
  3. 2
      clamonacc/fanotif/fanotif.c
  4. 2
      clamonacc/inotif/inotif.c
  5. 8
      freshclam/freshclam.c
  6. 4
      libclamav/blob.c
  7. 8
      libclamav/bytecode.c
  8. 2
      libclamav/crtmgr.c
  9. 2
      libclamav/cvd.c
  10. 24
      libclamav/egg.c
  11. 4
      libclamav/entconv.c
  12. 4
      libclamav/events.c
  13. 8
      libclamav/fmap.c
  14. 32
      libclamav/htmlnorm.c
  15. 4
      libclamav/ishield.c
  16. 6
      libclamav/jsparse/js-norm.c
  17. 2
      libclamav/libclamav.map
  18. 2
      libclamav/macho.c
  19. 8
      libclamav/matcher-ac.c
  20. 2
      libclamav/matcher-byte-comp.c
  21. 42
      libclamav/mbox.c
  22. 26
      libclamav/message.c
  23. 6
      libclamav/mpool.h
  24. 48
      libclamav/ole2_extract.c
  25. 285
      libclamav/others.h
  26. 26
      libclamav/others_common.c
  27. 8
      libclamav/pdf.c
  28. 4
      libclamav/pe.c
  29. 4
      libclamav/phishcheck.c
  30. 32
      libclamav/readdb.c
  31. 18
      libclamav/regex_list.c
  32. 20
      libclamav/regex_suffix.c
  33. 6
      libclamav/scanners.c
  34. 18
      libclamav/sis.c
  35. 2
      libclamav/str.c
  36. 4
      libclamav/table.c
  37. 8
      libclamav/udf.c
  38. 2
      libclamav/vba_extract.c
  39. 10
      libclamav/xlm_extract.c
  40. 2
      libclamav/yara_clam.h
  41. 4
      libclamav/yara_parser.c
  42. 18
      libfreshclam/libfreshclam.c
  43. 24
      libfreshclam/libfreshclam_internal.c
  44. 22
      unit_tests/check_regex.c

@ -188,7 +188,7 @@ int16_t ping_clamd(const struct optstruct *opts)
/* ping command takes the form --ping [attempts[:interval]] */
if (NULL != (opt = optget(opts, "ping"))) {
if (NULL != opt->strarg) {
if (NULL == (attempt_str = cli_strdup(opt->strarg))) {
if (NULL == (attempt_str = cli_safer_strdup(opt->strarg))) {
logg(LOGG_ERROR, "could not allocate memory for string\n");
ret = -1;
goto done;

@ -208,7 +208,7 @@ int16_t onas_ping_clamd(struct onas_context **ctx)
opt = optget((*ctx)->opts, "ping");
if (opt->enabled) {
attempt_str = cli_strdup(opt->strarg);
attempt_str = cli_safer_strdup(opt->strarg);
if (attempt_str) {
if (NULL == attempt_str) {
logg(LOGG_ERROR, "could not allocate memory for string\n");

@ -255,7 +255,7 @@ int onas_fan_eloop(struct onas_context **ctx)
return 2;
}
memcpy(event_data->fmd, fmd, sizeof(struct fanotify_event_metadata));
event_data->pathname = cli_strdup(fname);
event_data->pathname = cli_safer_strdup(fname);
if (NULL == event_data->pathname) {
close(fmd->fd);
free(event_data->fmd);

@ -821,7 +821,7 @@ static void onas_ddd_handle_extra_scanning(struct onas_context *ctx, const char
/* general mapping */
onas_map_context_info_to_event_data(ctx, &event_data);
event_data->pathname = cli_strdup(pathname);
event_data->pathname = cli_safer_strdup(pathname);
event_data->bool_opts |= ONAS_SCTH_B_SCAN;
/* inotify specific stuffs */

@ -480,7 +480,7 @@ static fc_error_t get_server_node(
* Ensure that URL contains protocol.
*/
if (!strncmp(server, "db.", 3) && strstr(server, ".clamav.net")) {
url = cli_strdup("https://database.clamav.net");
url = cli_safer_strdup("https://database.clamav.net");
if (NULL == url) {
logg(LOGG_ERROR, "get_server_node: Failed to duplicate string for database.clamav.net url.\n");
status = FC_EMEM;
@ -497,7 +497,7 @@ static fc_error_t get_server_node(
snprintf(url, urlLen + 1, "%s://%s", defaultProtocol, server);
} else {
urlLen = strlen(server);
url = cli_strdup(server);
url = cli_safer_strdup(server);
if (NULL == url) {
logg(LOGG_ERROR, "get_server_node: Failed to duplicate string for server url.\n");
status = FC_EMEM;
@ -544,7 +544,7 @@ static fc_error_t string_list_add(const char *item, char ***stringList, uint32_t
*stringList = newList;
newList[nItems - 1] = cli_strdup(item);
newList[nItems - 1] = cli_safer_strdup(item);
if (newList[nItems - 1] == NULL) {
mprintf(LOGG_ERROR, "string_list_add: Failed to allocate memory for optional database list item.\n");
status = FC_EMEM;
@ -1663,7 +1663,7 @@ int main(int argc, char **argv)
/*
* Parse the config file.
*/
cfgfile = cli_strdup(optget(opts, "config-file")->strarg);
cfgfile = cli_safer_strdup(optget(opts, "config-file")->strarg);
if ((opts = optparse(cfgfile, 0, NULL, 1, OPT_FRESHCLAM, 0, opts)) == NULL) {
fprintf(stderr, "ERROR: Can't open/parse the config file %s\n", cfgfile);
status = FC_EINIT;

@ -154,7 +154,7 @@ void blobSetFilename(blob *b, const char *dir, const char *filename)
if (b->name)
free(b->name);
b->name = cli_strdup(filename);
b->name = cli_safer_strdup(filename);
if (b->name)
sanitiseName(b->name);
@ -526,7 +526,7 @@ void fileblobPartialSet(fileblob *fb, const char *fullname, const char *arg)
fb->b.len = fb->b.size = 0;
fb->isNotEmpty = 1;
}
fb->fullname = cli_strdup(fullname);
fb->fullname = cli_safer_strdup(fullname);
}
void fileblobSetFilename(fileblob *fb, const char *dir, const char *filename)

@ -507,7 +507,7 @@ static inline operand_t readOperand(struct cli_bc_func *func, unsigned char *p,
uint16_t ty;
p[*off] |= 0x20;
/* TODO: unique constants */
func->constants = cli_safer_realloc2(func->constants, (func->numConstants + 1) * sizeof(*func->constants));
func->constants = cli_safer_realloc_or_free(func->constants, (func->numConstants + 1) * sizeof(*func->constants));
if (!func->constants) {
*ok = false;
return MAX_OP;
@ -698,13 +698,13 @@ static cl_error_t parseLSig(struct cli_bc *bc, char *buffer)
// char *vnames;
char *vend = strchr(buffer, ';');
if (vend) {
bc->lsig = cli_strdup(buffer);
bc->lsig = cli_safer_strdup(buffer);
*vend++ = '\0';
// prefix = buffer;
// vnames = strchr(vend, '{');
} else {
/* Not a logical signature, but we still have a virusname */
bc->hook_name = cli_strdup(buffer);
bc->hook_name = cli_safer_strdup(buffer);
bc->lsig = NULL;
}
@ -2407,7 +2407,7 @@ static cl_error_t add_selfcheck(struct cli_all_bc *bcs)
struct cli_bc_inst *inst;
struct cli_bc *bc;
bcs->all_bcs = cli_safer_realloc2(bcs->all_bcs, sizeof(*bcs->all_bcs) * (bcs->count + 1));
bcs->all_bcs = cli_safer_realloc_or_free(bcs->all_bcs, sizeof(*bcs->all_bcs) * (bcs->count + 1));
if (!bcs->all_bcs) {
cli_errmsg("cli_loadcbc: Can't allocate memory for bytecode entry\n");
return CL_EMEM;

@ -415,7 +415,7 @@ static cl_error_t crtmgr_get_recov_data(BIGNUM *sig, cli_crt *x509,
if (!x)
goto done;
MALLOC(d, keylen);
CLI_MALLOC_OR_GOTO_DONE(d, keylen);
if (!BN_mod_exp(x, sig, x509->e, x509->n, bnctx)) {
cli_warnmsg("crtmgr_rsa_verify: verification failed: BN_mod_exp failed.\n");

@ -633,7 +633,7 @@ cl_error_t cli_cvdload(FILE *fs, struct cl_engine *engine, unsigned int *signo,
if (dbtype <= 1) {
/* check for duplicate db */
dupname = cli_strdup(filename);
dupname = cli_safer_strdup(filename);
if (!dupname)
return CL_EMEM;
dupname[strlen(dupname) - 2] = (dbtype == 1 ? 'v' : 'l');

@ -1221,7 +1221,7 @@ static cl_error_t egg_parse_file_extra_field(egg_handle* handle, egg_file* eggFi
/*
* Comment found. Add comment to our list.
*/
CLI_SAFER_REALLOC(eggFile->comments,
CLI_SAFER_REALLOC_OR_GOTO_DONE(eggFile->comments,
sizeof(char*) * (eggFile->nComments + 1),
free(comment),
status = CL_EMEM);
@ -1667,7 +1667,7 @@ cl_error_t cli_egg_open(fmap_t* map, void** hArchive, char*** comments, uint32_t
goto done;
} else {
/* Add file to list. */
CLI_SAFER_REALLOC(handle->files,
CLI_SAFER_REALLOC_OR_GOTO_DONE(handle->files,
sizeof(egg_file*) * (handle->nFiles + 1),
egg_free_egg_file(found_file),
status = CL_EMEM);
@ -1689,7 +1689,7 @@ cl_error_t cli_egg_open(fmap_t* map, void** hArchive, char*** comments, uint32_t
} else {
/* Add block to list. */
if (handle->bSolid) {
CLI_SAFER_REALLOC(handle->blocks,
CLI_SAFER_REALLOC_OR_GOTO_DONE(handle->blocks,
sizeof(egg_block*) * (handle->nBlocks + 1),
egg_free_egg_block(found_block),
status = CL_EMEM);
@ -1707,7 +1707,7 @@ cl_error_t cli_egg_open(fmap_t* map, void** hArchive, char*** comments, uint32_t
} else {
eggFile = handle->files[handle->nFiles - 1];
CLI_SAFER_REALLOC(eggFile->blocks,
CLI_SAFER_REALLOC_OR_GOTO_DONE(eggFile->blocks,
sizeof(egg_block*) * (eggFile->nBlocks + 1),
egg_free_egg_block(found_block),
status = CL_EMEM);
@ -1785,7 +1785,7 @@ cl_error_t cli_egg_open(fmap_t* map, void** hArchive, char*** comments, uint32_t
/*
* Comment found. Add comment to our list.
*/
CLI_SAFER_REALLOC(handle->comments,
CLI_SAFER_REALLOC_OR_GOTO_DONE(handle->comments,
sizeof(char*) * (handle->nComments + 1),
free(comment),
status = CL_EMEM);
@ -1972,7 +1972,7 @@ cl_error_t cli_egg_deflate_decompress(char* compressed, size_t compressed_size,
while (zstat == Z_OK && stream.avail_in) {
/* extend output capacity if needed,*/
if (stream.avail_out == 0) {
CLI_SAFER_REALLOC(decoded,
CLI_SAFER_REALLOC_OR_GOTO_DONE(decoded,
capacity + BUFSIZ,
cli_errmsg("cli_egg_deflate_decompress: cannot reallocate memory for decompressed output\n"),
status = CL_EMEM);
@ -2094,7 +2094,7 @@ cl_error_t cli_egg_bzip2_decompress(char* compressed, size_t compressed_size, ch
while (bzstat == BZ_OK && stream.avail_in) {
/* extend output capacity if needed,*/
if (stream.avail_out == 0) {
CLI_SAFER_REALLOC(decoded,
CLI_SAFER_REALLOC_OR_GOTO_DONE(decoded,
capacity + BUFSIZ,
cli_errmsg("cli_egg_bzip2_decompress: cannot reallocate memory for decompressed output\n");
status = CL_EMEM);
@ -2211,7 +2211,7 @@ cl_error_t cli_egg_lzma_decompress(char* compressed, size_t compressed_size, cha
while (lzmastat == LZMA_RESULT_OK && stream.avail_in) {
/* extend output capacity if needed,*/
if (stream.avail_out == 0) {
CLI_SAFER_REALLOC(decoded,
CLI_SAFER_REALLOC_OR_GOTO_DONE(decoded,
capacity + BUFSIZ,
cli_errmsg("cli_egg_lzma_decompress: cannot reallocate memory for decompressed output\n");
status = CL_EMEM);
@ -2361,7 +2361,7 @@ cl_error_t cli_egg_extract_file(void* hArchive, const char** filename, const cha
break;
}
CLI_SAFER_REALLOC(decompressed,
CLI_SAFER_REALLOC_OR_GOTO_DONE(decompressed,
(size_t)decompressed_size + currBlock->blockHeader->compress_size,
cli_errmsg("cli_egg_extract_file: Failed to allocate %" PRIu64 " bytes for decompressed file!\n",
decompressed_size),
@ -2386,7 +2386,7 @@ cl_error_t cli_egg_extract_file(void* hArchive, const char** filename, const cha
goto done;
}
/* Decompressed block. Add it to the file data */
CLI_SAFER_REALLOC(decompressed,
CLI_SAFER_REALLOC_OR_GOTO_DONE(decompressed,
(size_t)decompressed_size + decompressed_block_size,
cli_errmsg("cli_egg_extract_file: Failed to allocate %" PRIu64 " bytes for decompressed file!\n",
decompressed_size),
@ -2415,7 +2415,7 @@ cl_error_t cli_egg_extract_file(void* hArchive, const char** filename, const cha
goto done;
}
/* Decompressed block. Add it to the file data */
CLI_SAFER_REALLOC(decompressed,
CLI_SAFER_REALLOC_OR_GOTO_DONE(decompressed,
(size_t)decompressed_size + decompressed_block_size,
cli_errmsg("cli_egg_extract_file: Failed to allocate %" PRIu64 " bytes for decompressed file!\n",
decompressed_size),
@ -2454,7 +2454,7 @@ cl_error_t cli_egg_extract_file(void* hArchive, const char** filename, const cha
// goto done;
// }
// /* Decompressed block. Add it to the file data */
// CLI_SAFER_REALLOC(decompressed,
// CLI_SAFER_REALLOC_OR_GOTO_DONE(decompressed,
// (size_t)decompressed_size + decompressed_block_size,
// cli_errmsg("cli_egg_extract_file: Failed to allocate %" PRIu64 " bytes for decompressed file!\n",
// decompressed_size),

@ -656,7 +656,7 @@ static iconv_t iconv_open_cached(const char* fromcode)
idx = cache->last++;
if (idx >= cache->len) {
cache->len += 16;
cache->tab = cli_max_realloc2(cache->tab, cache->len * sizeof(cache->tab[0]));
cache->tab = cli_max_realloc_or_free(cache->tab, cache->len * sizeof(cache->tab[0]));
if (!cache->tab) {
cli_dbgmsg(MODULE_NAME "!Out of mem in iconv-pool\n");
errno = ENOMEM;
@ -1121,7 +1121,7 @@ char* cli_utf16_to_utf8(const char* utf16, size_t length, encoding_t type)
char* s2;
if (length < 2)
return cli_strdup("");
return cli_safer_strdup("");
if (length % 2) {
cli_warnmsg("utf16 length is not multiple of two: %lu\n", (long)length);
length--;

@ -294,7 +294,7 @@ void cli_event_data(cli_events_t *ctx, unsigned id, const void *data, uint32_t l
}
switch (ev->multiple) {
case multiple_last: {
void *v_data = cli_safer_realloc2(ev->u.v_data, len);
void *v_data = cli_safer_realloc_or_free(ev->u.v_data, len);
if (v_data) {
ev->u.v_data = v_data;
memcpy(v_data, data, len);
@ -305,7 +305,7 @@ void cli_event_data(cli_events_t *ctx, unsigned id, const void *data, uint32_t l
break;
}
case multiple_concat: {
void *v_data = cli_safer_realloc2(ev->u.v_data, ev->count + len);
void *v_data = cli_safer_realloc_or_free(ev->u.v_data, ev->count + len);
if (v_data) {
ev->u.v_data = v_data;
memcpy((char *)v_data + ev->count, data, len);

@ -135,7 +135,7 @@ fmap_t *fmap_check_empty(int fd, off_t offset, size_t len, int *empty, const cha
m->mtime = (uint64_t)st.st_mtime;
if (NULL != name) {
m->name = cli_strdup(name);
m->name = cli_safer_strdup(name);
if (NULL == m->name) {
funmap(m);
return NULL;
@ -220,7 +220,7 @@ fmap_t *fmap_check_empty(int fd, off_t offset, size_t len, int *empty, const cha
m->unmap = unmap_win32;
if (NULL != name) {
m->name = cli_strdup(name);
m->name = cli_safer_strdup(name);
if (NULL == m->name) {
funmap(m);
return NULL;
@ -293,7 +293,7 @@ fmap_t *fmap_duplicate(cl_fmap_t *map, size_t offset, size_t length, const char
}
if (NULL != name) {
duplicate_map->name = cli_strdup(name);
duplicate_map->name = cli_safer_strdup(name);
if (NULL == duplicate_map->name) {
status = CL_EMEM;
goto done;
@ -862,7 +862,7 @@ fmap_t *fmap_open_memory(const void *start, size_t len, const char *name)
if (NULL != name) {
/* Copy the name, if one is given */
m->name = cli_strdup(name);
m->name = cli_safer_strdup(name);
if (NULL == m->name) {
cli_warnmsg("fmap: failed to duplicate map name\n");
goto done;

@ -360,7 +360,7 @@ static void html_tag_arg_set(tag_arguments_t *tags, const char *tag, const char
for (i = 0; i < tags->count; i++) {
if (strcmp((const char *)tags->tag[i], tag) == 0) {
free(tags->value[i]);
tags->value[i] = (unsigned char *)cli_strdup(value);
tags->value[i] = (unsigned char *)cli_safer_strdup(value);
return;
}
}
@ -371,34 +371,34 @@ void html_tag_arg_add(tag_arguments_t *tags,
{
int len, i;
tags->count++;
tags->tag = (unsigned char **)cli_max_realloc2(tags->tag,
tags->tag = (unsigned char **)cli_max_realloc_or_free(tags->tag,
tags->count * sizeof(char *));
if (!tags->tag) {
goto done;
}
tags->value = (unsigned char **)cli_max_realloc2(tags->value,
tags->value = (unsigned char **)cli_max_realloc_or_free(tags->value,
tags->count * sizeof(char *));
if (!tags->value) {
goto done;
}
if (tags->scanContents) {
tags->contents = (unsigned char **)cli_max_realloc2(tags->contents,
tags->contents = (unsigned char **)cli_max_realloc_or_free(tags->contents,
tags->count * sizeof(*tags->contents));
if (!tags->contents) {
goto done;
}
tags->contents[tags->count - 1] = NULL;
}
tags->tag[tags->count - 1] = (unsigned char *)cli_strdup(tag);
tags->tag[tags->count - 1] = (unsigned char *)cli_safer_strdup(tag);
if (value) {
if (*value == '"') {
tags->value[tags->count - 1] = (unsigned char *)cli_strdup(value + 1);
tags->value[tags->count - 1] = (unsigned char *)cli_safer_strdup(value + 1);
len = strlen((const char *)value + 1);
if (len > 0) {
tags->value[tags->count - 1][len - 1] = '\0';
}
} else {
tags->value[tags->count - 1] = (unsigned char *)cli_strdup(value);
tags->value[tags->count - 1] = (unsigned char *)cli_safer_strdup(value);
}
} else {
tags->value[tags->count - 1] = NULL;
@ -1201,9 +1201,9 @@ static bool cli_html_normalise(cli_ctx *ctx, int fd, m_area_t *m_area, const cha
chunk_size = style_end - style_begin;
if (style_buff == NULL) {
CLI_MAX_MALLOC(style_buff, chunk_size + 1);
CLI_MAX_MALLOC_OR_GOTO_DONE(style_buff, chunk_size + 1);
} else {
CLI_MAX_REALLOC(style_buff, style_buff_size + chunk_size + 1);
CLI_MAX_REALLOC_OR_GOTO_DONE(style_buff, style_buff_size + chunk_size + 1);
}
memcpy(style_buff + style_buff_size, style_begin, chunk_size);
@ -1312,7 +1312,7 @@ static bool cli_html_normalise(cli_ctx *ctx, int fd, m_area_t *m_area, const cha
if (arg_action_value) {
if (in_form_action)
free(in_form_action);
in_form_action = (unsigned char *)cli_strdup(arg_action_value);
in_form_action = (unsigned char *)cli_safer_strdup(arg_action_value);
}
} else if (strcmp(tag, "img") == 0) {
arg_value = html_tag_arg_value(&tag_args, "src");
@ -1320,7 +1320,7 @@ static bool cli_html_normalise(cli_ctx *ctx, int fd, m_area_t *m_area, const cha
html_tag_arg_add(hrefs, "src", arg_value);
if (hrefs->scanContents && in_ahref)
/* "contents" of an img tag, is the URL of its parent <a> tag */
hrefs->contents[hrefs->count - 1] = (unsigned char *)cli_strdup((const char *)hrefs->value[in_ahref - 1]);
hrefs->contents[hrefs->count - 1] = (unsigned char *)cli_safer_strdup((const char *)hrefs->value[in_ahref - 1]);
if (in_form_action) {
/* form action is the real URL, and href is the 'displayed' */
html_tag_arg_add(hrefs, "form", arg_value);
@ -1335,7 +1335,7 @@ static bool cli_html_normalise(cli_ctx *ctx, int fd, m_area_t *m_area, const cha
html_tag_arg_add(hrefs, "dynsrc", arg_value);
if (hrefs->scanContents && in_ahref)
/* see above */
hrefs->contents[hrefs->count - 1] = (unsigned char *)cli_strdup((const char *)hrefs->value[in_ahref - 1]);
hrefs->contents[hrefs->count - 1] = (unsigned char *)cli_safer_strdup((const char *)hrefs->value[in_ahref - 1]);
if (in_form_action) {
/* form action is the real URL, and href is the 'displayed' */
html_tag_arg_add(hrefs, "form", arg_value);
@ -1351,7 +1351,7 @@ static bool cli_html_normalise(cli_ctx *ctx, int fd, m_area_t *m_area, const cha
html_tag_arg_add(hrefs, "iframe", arg_value);
if (hrefs->scanContents && in_ahref)
/* see above */
hrefs->contents[hrefs->count - 1] = (unsigned char *)cli_strdup((const char *)hrefs->value[in_ahref - 1]);
hrefs->contents[hrefs->count - 1] = (unsigned char *)cli_safer_strdup((const char *)hrefs->value[in_ahref - 1]);
if (in_form_action) {
/* form action is the real URL, and href is the 'displayed' */
html_tag_arg_add(hrefs, "form", arg_value);
@ -1367,7 +1367,7 @@ static bool cli_html_normalise(cli_ctx *ctx, int fd, m_area_t *m_area, const cha
html_tag_arg_add(hrefs, "area", arg_value);
if (hrefs->scanContents && in_ahref)
/* see above */
hrefs->contents[hrefs->count - 1] = (unsigned char *)cli_strdup((const char *)hrefs->value[in_ahref - 1]);
hrefs->contents[hrefs->count - 1] = (unsigned char *)cli_safer_strdup((const char *)hrefs->value[in_ahref - 1]);
if (in_form_action) {
/* form action is the real URL, and href is the 'displayed' */
html_tag_arg_add(hrefs, "form", arg_value);
@ -1837,9 +1837,9 @@ static bool cli_html_normalise(cli_ctx *ctx, int fd, m_area_t *m_area, const cha
size_t chunk_size = ptr - style_begin;
if (style_buff == NULL) {
CLI_MAX_MALLOC(style_buff, chunk_size + 1);
CLI_MAX_MALLOC_OR_GOTO_DONE(style_buff, chunk_size + 1);
} else {
CLI_MAX_REALLOC(style_buff, style_buff_size + chunk_size + 1);
CLI_MAX_REALLOC_OR_GOTO_DONE(style_buff, style_buff_size + chunk_size + 1);
}
memcpy(style_buff + style_buff_size, style_begin, chunk_size);

@ -246,7 +246,7 @@ cl_error_t cli_scanishield_msi(cli_ctx *ctx, off_t off)
return CL_SUCCESS;
}
filename = cli_strdup((const char *)key);
filename = cli_safer_strdup((const char *)key);
/* FIXMEISHIELD: cleanup the spam below */
cli_dbgmsg("ishield-msi: File %s (csize: %llx, unk1:%x unk2:%x unk3:%x unk4:%x unk5:%x unk6:%x unk7:%x unk8:%x unk9:%x unk10:%x unk11:%x)\n", key, (long long)csize, fb.unk1, fb.unk2, fb.unk3, fb.unk4, fb.unk5, fb.unk6, fb.unk7, fb.unk8, fb.unk9, fb.unk10, fb.unk11);
@ -432,7 +432,7 @@ cl_error_t cli_scanishield(cli_ctx *ctx, off_t off, size_t sz)
}
if (i == c.cabcnt) {
c.cabcnt++;
if (!(c.cabs = cli_max_realloc2(c.cabs, sizeof(struct CABARRAY) * c.cabcnt))) {
if (!(c.cabs = cli_max_realloc_or_free(c.cabs, sizeof(struct CABARRAY) * c.cabcnt))) {
ret = CL_EMEM;
break;
}

@ -881,7 +881,7 @@ static void run_decoders(struct parser_state *state)
cli_js_process_buffer(state, res.txtbuf.data, res.txtbuf.pos);
--state->rec;
}
FREE(res.txtbuf.data);
CLI_FREE_AND_SET_NULL(res.txtbuf.data);
/* state->tokens still refers to the embedded/nested context here */
if (!res.append) {
if (CL_EARG == replace_token_range(&parent_tokens, res.pos_begin, res.pos_end, &state->tokens)) {
@ -1044,7 +1044,7 @@ void cli_js_process_buffer(struct parser_state *state, const char *buf, size_t n
if (current->last_token == TOK_DOT) {
/* this is a member name, don't normalize
*/
TOKEN_SET(&val, string, cli_strdup(text));
TOKEN_SET(&val, string, cli_safer_strdup(text));
val.type = TOK_UNNORM_IDENTIFIER;
} else {
switch (current->fsm_state) {
@ -1197,7 +1197,7 @@ void cli_js_process_buffer(struct parser_state *state, const char *buf, size_t n
}
if (val.vtype == vtype_undefined) {
text = yyget_text(state->scanner);
TOKEN_SET(&val, string, cli_strdup(text));
TOKEN_SET(&val, string, cli_safer_strdup(text));
abort();
}
add_token(state, &val);

@ -127,7 +127,7 @@ CLAMAV_PRIVATE {
cli_utf16toascii;
cli_memstr;
cli_strdup;
cli_safer_strdup;
cli_max_calloc;
cli_max_malloc;
cli_max_realloc;

@ -375,7 +375,7 @@ cl_error_t cli_scanmacho(cli_ctx *ctx, struct cli_exe_info *fileinfo)
cli_dbgmsg("MACHO: ------------------\n");
continue;
}
sections = (struct cli_exe_section *)cli_max_realloc2(sections, (sect + nsects) * sizeof(struct cli_exe_section));
sections = (struct cli_exe_section *)cli_max_realloc_or_free(sections, (sect + nsects) * sizeof(struct cli_exe_section));
if (!sections) {
cli_errmsg("cli_scanmacho: Can't allocate memory for 'sections'\n");
return CL_EMEM;

@ -2519,7 +2519,7 @@ inline static int ac_special_altstr(const char *hexpr, uint8_t sigopts, struct c
char *hexprcpy, *h, *c;
int i, ret, num, fixed, slen;
if (!(hexprcpy = cli_strdup(hexpr))) {
if (!(hexprcpy = cli_safer_strdup(hexpr))) {
cli_errmsg("ac_special_altstr: Can't duplicate alternate expression\n");
return CL_EDUP;
}
@ -2646,7 +2646,7 @@ cl_error_t cli_ac_addsig(struct cli_matcher *root, const char *virname, const ch
}
if (strchr(hexsig, '[')) {
if (!(hexcpy = cli_strdup(hexsig))) {
if (!(hexcpy = cli_safer_strdup(hexsig))) {
MPOOL_FREE(root->mempool, new);
return CL_EMEM;
}
@ -2730,7 +2730,7 @@ cl_error_t cli_ac_addsig(struct cli_matcher *root, const char *virname, const ch
return error;
}
hex = cli_strdup(hex);
hex = cli_safer_strdup(hex);
free(hexcpy);
if (!hex) {
MPOOL_FREE(root->mempool, new);
@ -2745,7 +2745,7 @@ cl_error_t cli_ac_addsig(struct cli_matcher *root, const char *virname, const ch
if (hex) {
hexcpy = hex;
} else if (!(hexcpy = cli_strdup(hexsig))) {
} else if (!(hexcpy = cli_safer_strdup(hexsig))) {
MPOOL_FREE(root->mempool, new);
return CL_EMEM;
}

@ -317,7 +317,7 @@ cl_error_t cli_bcomp_addpatt(struct cli_matcher *root, const char *virname, cons
bcomp->byte_len = byte_length;
/* we can have up to two comparison eval statements, each separated by a comma, let's parse them in a separate string */
comp_buf = cli_strdup(tokens[2]);
comp_buf = cli_safer_strdup(tokens[2]);
if (!comp_buf) {
cli_errmsg("cli_bcomp_addpatt: Unable to allocate memory for comparison buffer\n");
cli_bcomp_freemeta(root, bcomp);

@ -624,7 +624,7 @@ appendReadStruct(ReadStruct *rs, const char *const buffer)
strncpy(&(rs->buffer[rs->bufferLen]), buffer, part);
rs->bufferLen += part;
CALLOC(next, 1, sizeof(ReadStruct));
CLI_CALLOC_OR_GOTO_DONE(next, 1, sizeof(ReadStruct));
rs->next = next;
strcpy(next->buffer, &(buffer[part]));
@ -654,7 +654,7 @@ getMallocedBufferFromList(const ReadStruct *head)
rs = rs->next;
}
CLI_MAX_MALLOC(working, bufferLen);
CLI_MAX_MALLOC_OR_GOTO_DONE(working, bufferLen);
rs = head;
bufferLen = 0;
@ -668,7 +668,7 @@ getMallocedBufferFromList(const ReadStruct *head)
ret = working;
done:
if (NULL == ret) {
FREE(working);
CLI_FREE_AND_SET_NULL(working);
}
return ret;
@ -679,7 +679,7 @@ freeList(ReadStruct *head)
{
while (head) {
ReadStruct *rs = head->next;
FREE(head);
CLI_FREE_AND_SET_NULL(head);
head = rs;
}
}
@ -844,7 +844,7 @@ parseEmailFile(fmap_t *map, size_t *at, const table_t *rfc821, const char *first
if (ret == NULL)
return NULL;
CALLOC(head, 1, sizeof(ReadStruct));
CLI_CALLOC_OR_GOTO_DONE(head, 1, sizeof(ReadStruct));
curr = head;
strncpy(buffer, firstLine, sizeof(buffer) - 1);
@ -906,20 +906,20 @@ parseEmailFile(fmap_t *map, size_t *at, const table_t *rfc821, const char *first
if (head->bufferLen) {
char *header = getMallocedBufferFromList(head);
int needContinue = 0;
VERIFY_POINTER(header);
CLI_VERIFY_POINTER_OR_GOTO_DONE(header);
totalHeaderCnt++;
if (haveTooManyEmailHeaders(totalHeaderCnt, ctx, heuristicFound)) {
FREE(header);
CLI_FREE_AND_SET_NULL(header);
break;
}
needContinue = (parseEmailHeader(ret, header, rfc821, ctx, heuristicFound) < 0);
if (*heuristicFound) {
FREE(header);
CLI_FREE_AND_SET_NULL(header);
break;
}
FREE(header);
CLI_FREE_AND_SET_NULL(header);
FREELIST_REALLOC(head, curr);
if (needContinue) {
@ -1023,7 +1023,7 @@ parseEmailFile(fmap_t *map, size_t *at, const table_t *rfc821, const char *first
{
char *header = getMallocedBufferFromList(head); /*This is the issue */
int needContinue = 0;
VERIFY_POINTER(header);
CLI_VERIFY_POINTER_OR_GOTO_DONE(header);
needContinue = (header[strlen(header) - 1] == ';');
if (0 == needContinue) {
@ -1033,18 +1033,18 @@ parseEmailFile(fmap_t *map, size_t *at, const table_t *rfc821, const char *first
if (0 == needContinue) {
totalHeaderCnt++;
if (haveTooManyEmailHeaders(totalHeaderCnt, ctx, heuristicFound)) {
FREE(header);
CLI_FREE_AND_SET_NULL(header);
break;
}
needContinue = (parseEmailHeader(ret, header, rfc821, ctx, heuristicFound) < 0);
if (*heuristicFound) {
FREE(header);
CLI_FREE_AND_SET_NULL(header);
break;
}
/*Check total headers here;*/
}
FREE(header);
CLI_FREE_AND_SET_NULL(header);
if (needContinue) {
continue;
}
@ -1100,7 +1100,7 @@ done:
ret->isTruncated = true;
}
FREE(boundary);
CLI_FREE_AND_SET_NULL(boundary);
freeList(head);
@ -1226,7 +1226,7 @@ parseEmailHeaders(message *m, const table_t *rfc821, bool *heuristicFound)
anyHeadersFound = usefulHeader(commandNumber, cmd);
continue;
}
fullline = cli_strdup(line);
fullline = cli_safer_strdup(line);
fulllinelength = strlen(line) + 1;
} else if (line) {
fulllinelength += strlen(line) + 1;
@ -1363,7 +1363,7 @@ parseEmailHeader(message *m, const char *line, const table_t *rfc821, cli_ctx *c
copy = rfc2047(line);
if (copy == NULL) {
/* an RFC checker would return -1 here */
copy = cli_strdup(line);
copy = cli_safer_strdup(line);
if (NULL == copy) {
goto done;
}
@ -1397,7 +1397,7 @@ parseEmailHeader(message *m, const char *line, const table_t *rfc821, cli_ctx *c
}
}
done:
FREE(copy);
CLI_FREE_AND_SET_NULL(copy);
return ret;
}
@ -2058,7 +2058,7 @@ parseEmailBody(message *messageIn, text *textIn, mbox_ctx *mctx, unsigned int re
fullline = rfc822comments(line, NULL);
if (fullline == NULL)
fullline = cli_strdup(line);
fullline = cli_safer_strdup(line);
/*quotes = count_quotes(fullline);*/
@ -3241,7 +3241,7 @@ parseMimeHeader(message *m, const char *cmd, const table_t *rfc821Table, const c
* Content-Type: multipart/mixed foo/bar
*/
if (s && *s) {
char *buf2 = cli_strdup(buf);
char *buf2 = cli_safer_strdup(buf);
if (buf2 == NULL) {
if (copy)
@ -3457,7 +3457,7 @@ rfc2047(const char *in)
size_t len;
if ((strstr(in, "=?") == NULL) || (strstr(in, "?=") == NULL))
return cli_strdup(in);
return cli_safer_strdup(in);
cli_dbgmsg("rfc2047 '%s'\n", in);
out = cli_max_malloc(strlen(in) + 1);
@ -3503,7 +3503,7 @@ rfc2047(const char *in)
if (*++in == '\0')
break;
enctext = cli_strdup(in);
enctext = cli_safer_strdup(in);
if (enctext == NULL) {
free(out);
out = NULL;

@ -343,7 +343,7 @@ void messageSetMimeSubtype(message *m, const char *subtype)
if (m->mimeSubtype)
free(m->mimeSubtype);
m->mimeSubtype = cli_strdup(subtype);
m->mimeSubtype = cli_safer_strdup(subtype);
}
const char *
@ -375,7 +375,7 @@ void messageSetDispositionType(message *m, const char *disptype)
while (*disptype && isspace((int)*disptype))
disptype++;
if (*disptype) {
m->mimeDispositionType = cli_strdup(disptype);
m->mimeDispositionType = cli_safer_strdup(disptype);
if (m->mimeDispositionType)
strstrip(m->mimeDispositionType);
} else
@ -558,7 +558,7 @@ void messageAddArguments(message *m, const char *s)
* The field is in quotes, so look for the
* closing quotes
*/
kcopy = cli_strdup(key);
kcopy = cli_safer_strdup(key);
if (kcopy == NULL)
return;
@ -588,7 +588,7 @@ void messageAddArguments(message *m, const char *s)
continue;
}
data = cli_strdup(cptr);
data = cli_safer_strdup(cptr);
if (!data) {
cli_dbgmsg("Can't parse header \"%s\" - if you believe this file contains a missed virus, report it to bugs@clamav.net\n", s);
@ -704,7 +704,7 @@ messageFindArgument(const message *m, const char *variable)
ptr++;
if ((strlen(ptr) > 1) && (*ptr == '"') && (strchr(&ptr[1], '"') != NULL)) {
/* Remove any quote characters */
char *ret = cli_strdup(++ptr);
char *ret = cli_safer_strdup(++ptr);
char *p;
if (ret == NULL)
@ -725,7 +725,7 @@ messageFindArgument(const message *m, const char *variable)
}
return ret;
}
return cli_strdup(ptr);
return cli_safer_strdup(ptr);
}
}
return NULL;
@ -1278,7 +1278,7 @@ messageExport(message *m, const char *dir, void *(*create)(void), void (*destroy
f = lineGetData(t_line->t_line);
if ((filename = strstr(f, " name=")) != NULL) {
filename = cli_strdup(&filename[6]);
filename = cli_safer_strdup(&filename[6]);
if (filename) {
cli_chomp(filename);
strstrip(filename);
@ -1903,7 +1903,7 @@ decodeLine(message *m, encoding_type et, const char *line, unsigned char *buf, s
strcpy(base64buf, line);
copy = base64buf;
} else {
copy = cli_strdup(line);
copy = cli_safer_strdup(line);
if (copy == NULL)
break;
}
@ -2386,7 +2386,7 @@ rfc2231(const char *in)
}
if (ptr == NULL) { /* quick return */
out = ret = cli_strdup(in);
out = ret = cli_safer_strdup(in);
while (*out)
*out++ &= 0x7F;
return ret;
@ -2461,7 +2461,7 @@ rfc2231(const char *in)
if (field != CONTENTS) {
free(ret);
cli_dbgmsg("Invalid RFC2231 header: '%s'\n", in);
return cli_strdup("");
return cli_safer_strdup("");
}
*out = '\0';
@ -2509,9 +2509,9 @@ simil(const char *str1, const char *str2)
if (strcasecmp(str1, str2) == 0)
return 100;
if ((s1 = cli_strdup(str1)) == NULL)
if ((s1 = cli_safer_strdup(str1)) == NULL)
return OUT_OF_MEMORY;
if ((s2 = cli_strdup(str2)) == NULL) {
if ((s2 = cli_safer_strdup(str2)) == NULL) {
free(s1);
return OUT_OF_MEMORY;
}
@ -2629,7 +2629,7 @@ push(LINK1 *top, const char *string)
if ((element = (LINK1)malloc(sizeof(ELEMENT1))) == NULL)
return OUT_OF_MEMORY;
if ((element->d1 = cli_strdup(string)) == NULL) {
if ((element->d1 = cli_safer_strdup(string)) == NULL) {
free(element);
return OUT_OF_MEMORY;
}

@ -70,10 +70,10 @@ typedef void mpool_t;
#define MPOOL_FREE(a, b) free(b)
#define MPOOL_CALLOC(a, b, c) calloc(b, c)
#define MPOOL_REALLOC(a, b, c) cli_safer_realloc(b, c)
#define MPOOL_REALLOC2(a, b, c) cli_safer_realloc2(b, c)
#define MPOOL_REALLOC2(a, b, c) cli_safer_realloc_or_free(b, c)
#define CLI_MPOOL_HEX2STR(mpool, src) cli_hex2str(src)
#define CLI_MPOOL_STRDUP(mpool, s) cli_strdup(s)
#define CLI_MPOOL_STRNDUP(mpool, s, n) cli_strdup(s, n)
#define CLI_MPOOL_STRDUP(mpool, s) cli_safer_strdup(s)
#define CLI_MPOOL_STRNDUP(mpool, s, n) cli_safer_strdup(s, n)
#define CLI_MPOOL_VIRNAME(mpool, a, b) cli_virname(a, b)
#define CLI_MPOOL_HEX2UI(mpool, hex) cli_hex2ui(hex)
#define MPOOL_FLUSH(val)

@ -196,7 +196,7 @@ int ole2_list_push(ole2_list_t *list, uint32_t val)
ole2_list_node_t *new_node = NULL;
int status = CL_EMEM;
MALLOC(new_node, sizeof(ole2_list_node_t),
CLI_MALLOC_OR_GOTO_DONE(new_node, sizeof(ole2_list_node_t),
cli_dbgmsg("OLE2: could not allocate new node for worklist!\n"));
new_node->Val = val;
@ -256,7 +256,7 @@ cli_ole2_get_property_name2(const char *name, int size)
if ((name[0] == 0 && name[1] == 0) || size <= 0 || size > 128) {
return NULL;
}
CLI_MAX_MALLOC(newname, size * 7,
CLI_MAX_MALLOC_OR_GOTO_DONE(newname, size * 7,
cli_errmsg("OLE2 [cli_ole2_get_property_name2]: Unable to allocate memory for newname: %u\n", size * 7));
j = 0;
@ -304,7 +304,7 @@ get_property_name(char *name, int size)
return NULL;
}
CLI_MAX_MALLOC(newname, size,
CLI_MAX_MALLOC_OR_GOTO_DONE(newname, size,
cli_errmsg("OLE2 [get_property_name]: Unable to allocate memory for newname %u\n", size));
cname = newname;
@ -313,7 +313,7 @@ get_property_name(char *name, int size)
oname += 2;
if (u > 0x1040) {
FREE(newname);
CLI_FREE_AND_SET_NULL(newname);
return cli_ole2_get_property_name2(name, size);
}
lo = u % 64;
@ -888,7 +888,7 @@ static cl_error_t handler_writefile(ole2_header_t *hdr, property_t *prop, const
current_block = prop->start_block;
len = prop->size;
CLI_MAX_MALLOC(buff, 1 << hdr->log2_big_block_size,
CLI_MAX_MALLOC_OR_GOTO_DONE(buff, 1 << hdr->log2_big_block_size,
cli_errmsg("OLE2 [handler_writefile]: Unable to allocate memory for buff: %u\n", 1 << hdr->log2_big_block_size);
ret = CL_EMEM);
@ -956,11 +956,11 @@ static cl_error_t handler_writefile(ole2_header_t *hdr, property_t *prop, const
ret = CL_SUCCESS;
done:
FREE(name);
CLI_FREE_AND_SET_NULL(name);
if (-1 != ofd) {
close(ofd);
}
FREE(buff);
CLI_FREE_AND_SET_NULL(buff);
if (NULL != blk_bitset) {
cli_bitset_free(blk_bitset);
}
@ -1157,7 +1157,7 @@ static cl_error_t scan_for_xlm_macros_and_images(ole2_header_t *hdr, property_t
current_block = prop->start_block;
len = prop->size;
CLI_MAX_MALLOC(buff, 1 << hdr->log2_big_block_size,
CLI_MAX_MALLOC_OR_GOTO_DONE(buff, 1 << hdr->log2_big_block_size,
cli_errmsg("OLE2 [scan_for_xlm_macros_and_images]: Unable to allocate memory for buff: %u\n", 1 << hdr->log2_big_block_size);
status = CL_EMEM);
@ -1207,7 +1207,7 @@ static cl_error_t scan_for_xlm_macros_and_images(ole2_header_t *hdr, property_t
status = CL_SUCCESS;
done:
FREE(buff);
CLI_FREE_AND_SET_NULL(buff);
if (blk_bitset) {
cli_bitset_free(blk_bitset);
@ -1282,7 +1282,7 @@ static cl_error_t handler_enum(ole2_header_t *hdr, property_t *prop, const char
}
if (name) {
if (!strcmp(name, "fileheader")) {
CLI_MAX_CALLOC(hwp_check, 1, 1 << hdr->log2_big_block_size, status = CL_EMEM);
CLI_MAX_CALLOC_OR_GOTO_DONE(hwp_check, 1, 1 << hdr->log2_big_block_size, status = CL_EMEM);
/* reading safety checks; do-while used for breaks */
do {
@ -1316,7 +1316,7 @@ static cl_error_t handler_enum(ole2_header_t *hdr, property_t *prop, const char
#if HAVE_JSON
cli_jsonstr(ctx->wrkproperty, "FileType", "CL_TYPE_HWP5");
#endif
CALLOC(hwp_new, 1, sizeof(hwp5_header_t), status = CL_EMEM);
CLI_CALLOC_OR_GOTO_DONE(hwp_new, 1, sizeof(hwp5_header_t), status = CL_EMEM);
/*
* Copy the header information into our header struct.
@ -1349,8 +1349,8 @@ static cl_error_t handler_enum(ole2_header_t *hdr, property_t *prop, const char
status = CL_SUCCESS;
done:
FREE(name);
FREE(hwp_check);
CLI_FREE_AND_SET_NULL(name);
CLI_FREE_AND_SET_NULL(hwp_check);
return status;
}
@ -1567,7 +1567,7 @@ static cl_error_t handler_otf(ole2_header_t *hdr, property_t *prop, const char *
cli_dbgmsg("OLE2 [handler_otf]: Dumping '%s' to '%s'\n", name, tempfile);
}
CLI_MAX_MALLOC(buff, 1 << hdr->log2_big_block_size, ret = CL_EMEM);
CLI_MAX_MALLOC_OR_GOTO_DONE(buff, 1 << hdr->log2_big_block_size, ret = CL_EMEM);
blk_bitset = cli_bitset_init();
if (!blk_bitset) {
@ -1677,11 +1677,11 @@ static cl_error_t handler_otf(ole2_header_t *hdr, property_t *prop, const char *
ret = ret == CL_VIRUS ? CL_VIRUS : CL_SUCCESS;
done:
FREE(name);
CLI_FREE_AND_SET_NULL(name);
if (-1 != ofd) {
close(ofd);
}
FREE(buff);
CLI_FREE_AND_SET_NULL(buff);
if (NULL != blk_bitset) {
cli_bitset_free(blk_bitset);
}
@ -1746,7 +1746,7 @@ static cl_error_t handler_otf_encrypted(ole2_header_t *hdr, property_t *prop, co
goto done;
}
CLI_MAX_MALLOC(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(uint64_t), ret = CL_EMEM);
print_ole2_property(prop);
@ -1774,8 +1774,8 @@ static cl_error_t handler_otf_encrypted(ole2_header_t *hdr, property_t *prop, co
}
uint32_t blockSize = 1 << hdr->log2_big_block_size;
CLI_MAX_MALLOC(buff, blockSize + sizeof(uint64_t), ret = CL_EMEM);
CLI_MAX_MALLOC(decryptDst, blockSize, ret = CL_EMEM);
CLI_MAX_MALLOC_OR_GOTO_DONE(buff, blockSize + sizeof(uint64_t), ret = CL_EMEM);
CLI_MAX_MALLOC_OR_GOTO_DONE(decryptDst, blockSize, ret = CL_EMEM);
blk_bitset = cli_bitset_init();
if (!blk_bitset) {
@ -1921,11 +1921,11 @@ static cl_error_t handler_otf_encrypted(ole2_header_t *hdr, property_t *prop, co
ret = ret == CL_VIRUS ? CL_VIRUS : CL_SUCCESS;
done:
FREE(name);
CLI_FREE_AND_SET_NULL(name);
if (-1 != ofd) {
close(ofd);
}
FREE(buff);
CLI_FREE_AND_SET_NULL(buff);
if (NULL != blk_bitset) {
cli_bitset_free(blk_bitset);
}
@ -1938,8 +1938,8 @@ done:
free(tempfile);
tempfile = NULL;
}
FREE(decryptDst);
FREE(rk);
CLI_FREE_AND_SET_NULL(decryptDst);
CLI_FREE_AND_SET_NULL(rk);
return ret;
}
@ -2136,7 +2136,7 @@ static cl_error_t generate_key_aes(const char *const password, encryption_key_t
memcpy(key->key, doubleSha, tmp);
ret = CL_SUCCESS;
done:
FREE(buffer);
CLI_FREE_AND_SET_NULL(buffer);
return ret;
}

@ -941,7 +941,7 @@ static inline int cli_getpagesize(void)
/**
* @brief Wrapper around malloc that limits how much may be allocated to CLI_MAX_ALLOCATION.
*
* Please use CLI_MAX_MALLOC() with `goto done;` error handling instead.
* Please use CLI_MAX_MALLOC_OR_GOTO_DONE() with `goto done;` error handling instead.
*
* @param ptr
* @param size
@ -950,9 +950,9 @@ static inline int cli_getpagesize(void)
void *cli_max_malloc(size_t nmemb);
/**
* @brief Wrapper around Calloc that limits how much may be allocated to CLI_MAX_ALLOCATION.
* @brief Wrapper around calloc that limits how much may be allocated to CLI_MAX_ALLOCATION.
*
* Please use CLI_MAX_CALLOC() with `goto done;` error handling instead.
* Please use CLI_MAX_CALLOC_OR_GOTO_DONE() with `goto done;` error handling instead.
*
* @param ptr
* @param size
@ -963,7 +963,9 @@ void *cli_max_calloc(size_t nmemb, size_t size);
/**
* @brief Wrapper around realloc that limits how much may be allocated to CLI_MAX_ALLOCATION.
*
* Please use CLI_MAX_REALLOC() with `goto done;` error handling instead.
* Please use CLI_MAX_REALLOC_OR_GOTO_DONE() with `goto done;` error handling instead.
*
* NOTE: cli_max_realloc() will NOT free ptr if size==0. It is safe to free ptr after `done:`.
*
* IMPORTANT: This differs from realloc() in that if size==0, it will NOT free the ptr.
*
@ -976,7 +978,7 @@ void *cli_max_realloc(void *ptr, size_t size);
/**
* @brief Wrapper around realloc that limits how much may be allocated to CLI_MAX_ALLOCATION.
*
* Please use CLI_MAX_REALLOC() with `goto done;` error handling instead.
* Please use CLI_MAX_REALLOC_OR_GOTO_DONE() with `goto done;` error handling instead.
*
* IMPORTANT: This differs from realloc() in that if size==0, it will NOT free the ptr.
*
@ -987,12 +989,12 @@ void *cli_max_realloc(void *ptr, size_t size);
* @param size
* @return void*
*/
void *cli_max_realloc2(void *ptr, size_t size);
void *cli_max_realloc_or_free(void *ptr, size_t size);
/**
* @brief Wrapper around realloc that, unlike some variants of realloc, will not free the ptr if size==0.
*
* Please use CLI_MAX_REALLOC() with `goto done;` error handling instead.
* Please use CLI_MAX_REALLOC_OR_GOTO_DONE() with `goto done;` error handling instead.
*
* IMPORTANT: This differs from realloc() in that if size==0, it will NOT free the ptr.
*
@ -1005,7 +1007,7 @@ void *cli_safer_realloc(void *ptr, size_t size);
/**
* @brief Wrapper around realloc that, unlike some variants of realloc, will not free the ptr if size==0.
*
* Please use CLI_SAFER_REALLOC() with `goto done;` error handling instead.
* Please use CLI_SAFER_REALLOC_OR_GOTO_DONE() with `goto done;` error handling instead.
*
* IMPORTANT: This differs from realloc() in that if size==0, it will NOT free the ptr.
*
@ -1016,15 +1018,17 @@ void *cli_safer_realloc(void *ptr, size_t size);
* @param size
* @return void*
*/
void *cli_safer_realloc2(void *ptr, size_t size);
void *cli_safer_realloc_or_free(void *ptr, size_t size);
/**
* @brief Wrapper around strdup that does a NULL check.
*
* Please use CLI_STRDUP_OR_GOTO_DONE() with `goto done;` error handling instead.
*
* @param s
* @return char* Returns the allocated string or NULL if allocation failed. This includes if allocation fails because s==NULL.
*/
char *cli_strdup(const char *s);
char *cli_safer_strdup(const char *s);
int cli_rmdirs(const char *dirname);
char *cli_hashstream(FILE *fs, unsigned char *digcpy, int type);
@ -1298,103 +1302,150 @@ uint8_t cli_get_debug_flag(void);
*/
uint8_t cli_set_debug_flag(uint8_t debug_flag);
#ifndef STRDUP
#define STRDUP(buf, var, ...) \
do { \
var = strdup(buf); \
if (NULL == var) { \
do { \
__VA_ARGS__; \
} while (0); \
goto done; \
} \
} while (0)
#endif
#ifndef CLI_STRDUP
#define CLI_STRDUP(buf, var, ...) \
do { \
var = cli_strdup(buf); \
if (NULL == var) { \
do { \
__VA_ARGS__; \
} while (0); \
goto done; \
} \
#ifndef CLI_SAFER_STRDUP_OR_GOTO_DONE
/**
* @brief Wrapper around strdup that does a NULL check.
*
* This macro requires `goto done;` error handling.
*
* @param buf The string to duplicate.
* @param var The variable to assign the allocated string to.
* @param ... The error handling code to execute if the allocation fails.
*/
#define CLI_SAFER_STRDUP_OR_GOTO_DONE(buf, var, ...) \
do { \
var = cli_safer_strdup(buf); \
if (NULL == var) { \
do { \
__VA_ARGS__; \
} while (0); \
goto done; \
} \
} while (0)
#endif
#ifndef FREE
#define FREE(var) \
do { \
if (NULL != var) { \
free(var); \
var = NULL; \
} \
#ifndef CLI_FREE_AND_SET_NULL
/**
* @brief Wrapper around `free()` to ensure you reset the variable to NULL so as to prevent a double-free.
*
* @param var The variable to free and set to NULL.
*/
#define CLI_FREE_AND_SET_NULL(var) \
do { \
if (NULL != var) { \
free(var); \
var = NULL; \
} \
} while (0)
#endif
#ifndef MALLOC
#define MALLOC(var, size, ...) \
do { \
var = malloc(size); \
if (NULL == var) { \
do { \
__VA_ARGS__; \
} while (0); \
goto done; \
} \
#ifndef CLI_MALLOC_OR_GOTO_DONE
/**
* @brief Wrapper around malloc that will `goto done;` if the allocation fails.
*
* This macro requires `goto done;` error handling.
*
* @param ptr The variable to assign the allocated memory to.
* @param size The size of the memory to allocate.
* @param ... The error handling code to execute if the allocation fails.
*/
#define CLI_MALLOC_OR_GOTO_DONE(var, size, ...) \
do { \
var = malloc(size); \
if (NULL == var) { \
do { \
__VA_ARGS__; \
} while (0); \
goto done; \
} \
} while (0)
#endif
#ifndef CLI_MAX_MALLOC
#define CLI_MAX_MALLOC(var, size, ...) \
do { \
var = cli_max_malloc(size); \
if (NULL == var) { \
do { \
__VA_ARGS__; \
} while (0); \
goto done; \
} \
#ifndef CLI_MAX_MALLOC_OR_GOTO_DONE
/**
* @brief Wrapper around malloc that limits how much may be allocated to CLI_MAX_ALLOCATION.
*
* This macro requires `goto done;` error handling.
*
* @param var The variable to assign the allocated memory to.
* @param size The size of the memory to allocate.
* @param ... The error handling code to execute if the allocation fails.
*/
#define CLI_MAX_MALLOC_OR_GOTO_DONE(var, size, ...) \
do { \
var = cli_max_malloc(size); \
if (NULL == var) { \
do { \
__VA_ARGS__; \
} while (0); \
goto done; \
} \
} while (0)
#endif
#ifndef CALLOC
#define CALLOC(var, nmemb, size, ...) \
do { \
(var) = calloc(nmemb, size); \
if (NULL == var) { \
do { \
__VA_ARGS__; \
} while (0); \
goto done; \
} \
#ifndef CLI_CALLOC_OR_GOTO_DONE
/**
* @brief Wrapper around calloc that will `goto done;` if the allocation fails.
*
* This macro requires `goto done;` error handling.
*
* @param var The variable to assign the allocated memory to.
* @param nmemb The number of elements to allocate.
* @param size The size of each element.
* @param ... The error handling code to execute if the allocation fails.
*/
#define CLI_CALLOC_OR_GOTO_DONE(var, nmemb, size, ...) \
do { \
(var) = calloc(nmemb, size); \
if (NULL == var) { \
do { \
__VA_ARGS__; \
} while (0); \
goto done; \
} \
} while (0)
#endif
#ifndef CLI_MAX_CALLOC
#define CLI_MAX_CALLOC(var, nmemb, size, ...) \
do { \
(var) = cli_max_calloc(nmemb, size); \
if (NULL == var) { \
do { \
__VA_ARGS__; \
} while (0); \
goto done; \
} \
#ifndef CLI_MAX_CALLOC_OR_GOTO_DONE
/**
* @brief Wrapper around calloc that limits how much may be allocated to CLI_MAX_ALLOCATION.
*
* This macro requires `goto done;` error handling.
*
* @param var The variable to assign the allocated memory to.
* @param nmemb The number of elements to allocate.
* @param size The size of each element.
* @param ... The error handling code to execute if the allocation fails.
*/
#define CLI_MAX_CALLOC_OR_GOTO_DONE(var, nmemb, size, ...) \
do { \
(var) = cli_max_calloc(nmemb, size); \
if (NULL == var) { \
do { \
__VA_ARGS__; \
} while (0); \
goto done; \
} \
} while (0)
#endif
#ifndef VERIFY_POINTER
#define VERIFY_POINTER(ptr, ...) \
do { \
if (NULL == ptr) { \
do { \
__VA_ARGS__; \
} while (0); \
goto done; \
} \
#ifndef CLI_VERIFY_POINTER_OR_GOTO_DONE
/**
* @brief Wrapper around a NULL-check that will `goto done;` if the pointer is NULL.
*
* This macro requires `goto done;` error handling.
*
* @param ptr The pointer to verify.
* @param ... The error handling code to execute if the pointer is NULL.
*/
#define CLI_VERIFY_POINTER_OR_GOTO_DONE(ptr, ...) \
do { \
if (NULL == ptr) { \
do { \
__VA_ARGS__; \
} while (0); \
goto done; \
} \
} while (0)
#endif
@ -1405,21 +1456,21 @@ uint8_t cli_set_debug_flag(uint8_t debug_flag);
*
* NOTE: cli_max_realloc() will NOT free ptr if size==0. It is safe to free ptr after `done:`.
*
* @param ptr
* @param size
* @return void*
* @param ptr The pointer to realloc.
* @param size The size of the memory to allocate.
* @param ... The error handling code to execute if the allocation fails.
*/
#ifndef CLI_MAX_REALLOC
#define CLI_MAX_REALLOC(ptr, size, ...) \
do { \
void *vTmp = cli_max_realloc(ptr, size); \
if (NULL == vTmp) { \
do { \
__VA_ARGS__; \
} while (0); \
goto done; \
} \
ptr = vTmp; \
#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; \
} while (0)
#endif
@ -1430,21 +1481,21 @@ uint8_t cli_set_debug_flag(uint8_t debug_flag);
*
* NOTE: cli_safer_realloc() will NOT free ptr if size==0. It is safe to free ptr after `done:`.
*
* @param ptr
* @param size
* @return void*
* @param ptr The pointer to realloc.
* @param size The size of the memory to allocate.
* @param ... The error handling code to execute if the allocation fails.
*/
#ifndef CLI_SAFER_REALLOC
#define CLI_SAFER_REALLOC(ptr, size, ...) \
do { \
void *vTmp = cli_safer_realloc(ptr, size); \
if (NULL == vTmp) { \
do { \
__VA_ARGS__; \
} while (0); \
goto done; \
} \
ptr = vTmp; \
#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; \
} while (0)
#endif

@ -278,12 +278,12 @@ void *cli_safer_realloc(void *ptr, size_t size)
}
}
void *cli_safer_realloc2(void *ptr, size_t size)
void *cli_safer_realloc_or_free(void *ptr, size_t size)
{
void *alloc;
if (0 == size) {
cli_errmsg("cli_max_realloc2(): Attempt to allocate 0 bytes. Please report to https://github.com/Cisco-Talos/clamav/issues\n");
cli_errmsg("cli_max_realloc_or_free(): Attempt to allocate 0 bytes. Please report to https://github.com/Cisco-Talos/clamav/issues\n");
return NULL;
}
@ -291,7 +291,7 @@ void *cli_safer_realloc2(void *ptr, size_t size)
if (!alloc) {
perror("realloc_problem");
cli_errmsg("cli_max_realloc2(): Can't re-allocate memory to %lu bytes.\n", (unsigned long int)size);
cli_errmsg("cli_max_realloc_or_free(): Can't re-allocate memory to %lu bytes.\n", (unsigned long int)size);
// free the original pointer
if (ptr) {
@ -325,12 +325,12 @@ void *cli_max_realloc(void *ptr, size_t size)
}
}
void *cli_max_realloc2(void *ptr, size_t size)
void *cli_max_realloc_or_free(void *ptr, size_t size)
{
void *alloc;
if (0 == size || size > CLI_MAX_ALLOCATION) {
cli_warnmsg("cli_max_realloc2(): File or section is too large to scan (%zu bytes). For your safety, ClamAV limits how much memory an operation can allocate to %d bytes\n",
cli_warnmsg("cli_max_realloc_or_free(): File or section is too large to scan (%zu bytes). For your safety, ClamAV limits how much memory an operation can allocate to %d bytes\n",
size, CLI_MAX_ALLOCATION);
return NULL;
}
@ -339,7 +339,7 @@ void *cli_max_realloc2(void *ptr, size_t size)
if (!alloc) {
perror("realloc_problem");
cli_errmsg("cli_max_realloc2(): Can't re-allocate memory to %zu bytes.\n", size);
cli_errmsg("cli_max_realloc_or_free(): Can't re-allocate memory to %zu bytes.\n", size);
// free the original pointer
if (ptr) {
@ -352,12 +352,12 @@ void *cli_max_realloc2(void *ptr, size_t size)
}
}
char *cli_strdup(const char *s)
char *cli_safer_strdup(const char *s)
{
char *alloc;
if (s == NULL) {
cli_errmsg("cli_strdup(): passed reference is NULL, nothing to duplicate\n");
cli_errmsg("cli_safer_strdup(): passed reference is NULL, nothing to duplicate\n");
return NULL;
}
@ -365,7 +365,7 @@ char *cli_strdup(const char *s)
if (!alloc) {
perror("strdup_problem");
cli_errmsg("cli_strdup(): Can't allocate memory (%u bytes).\n", (unsigned int)strlen(s));
cli_errmsg("cli_safer_strdup(): Can't allocate memory (%u bytes).\n", (unsigned int)strlen(s));
return NULL;
}
@ -687,7 +687,7 @@ static cl_error_t handle_filetype(const char *fname, int flags,
if (*stated == -1) {
/* we failed a stat() or lstat() */
char *fname_copy = cli_strdup(fname);
char *fname_copy = cli_safer_strdup(fname);
if (NULL == fname_copy) {
goto done;
}
@ -699,7 +699,7 @@ static cl_error_t handle_filetype(const char *fname, int flags,
*ft = ft_unknown;
} else if (*ft == ft_skipped_link || *ft == ft_skipped_special) {
/* skipped filetype */
char *fname_copy = cli_strdup(fname);
char *fname_copy = cli_safer_strdup(fname);
if (NULL == fname_copy) {
goto done;
}
@ -777,7 +777,7 @@ cl_error_t cli_ftw(char *path, int flags, int maxdepth, cli_ftw_cb callback, str
*/
if (entry.is_dir) {
/* Allocate the filename for the callback function. TODO: this FTW code is spaghetti, refactor. */
filename_for_callback = cli_strdup(path);
filename_for_callback = cli_safer_strdup(path);
if (NULL == filename_for_callback) {
goto done;
}
@ -800,7 +800,7 @@ cl_error_t cli_ftw(char *path, int flags, int maxdepth, cli_ftw_cb callback, str
entry.dirname = path;
} else {
/* Allocate the filename for the callback function within the handle_entry function. TODO: this FTW code is spaghetti, refactor. */
filename_for_handleentry = cli_strdup(path);
filename_for_handleentry = cli_safer_strdup(path);
if (NULL == filename_for_handleentry) {
goto done;
}

@ -480,7 +480,7 @@ int pdf_findobj_in_objstm(struct pdf_struct *pdf, struct objstm_struct *objstm,
/* Success! Add the object to the list of all objects found. */
pdf->nobjs++;
CLI_MAX_REALLOC(pdf->objs, sizeof(struct pdf_obj *) * pdf->nobjs,
CLI_MAX_REALLOC_OR_GOTO_DONE(pdf->objs, sizeof(struct pdf_obj *) * pdf->nobjs,
cli_warnmsg("pdf_findobj_in_objstm: out of memory finding objects in stream\n"),
status = CL_EMEM);
pdf->objs[pdf->nobjs - 1] = obj;
@ -545,7 +545,7 @@ cl_error_t pdf_findobj(struct pdf_struct *pdf)
goto done;
}
pdf->nobjs++;
CLI_MAX_REALLOC(pdf->objs, sizeof(struct pdf_obj *) * pdf->nobjs, status = CL_EMEM);
CLI_MAX_REALLOC_OR_GOTO_DONE(pdf->objs, sizeof(struct pdf_obj *) * pdf->nobjs, status = CL_EMEM);
obj = malloc(sizeof(struct pdf_obj));
if (!obj) {
@ -1627,7 +1627,7 @@ cl_error_t pdf_extract_obj(struct pdf_struct *pdf, struct pdf_obj *obj, uint32_t
} else {
/* Add objstm to pdf struct, so it can be freed eventually */
pdf->nobjstms++;
pdf->objstms = cli_max_realloc2(pdf->objstms, sizeof(struct objstm_struct *) * pdf->nobjstms);
pdf->objstms = cli_max_realloc_or_free(pdf->objstms, sizeof(struct objstm_struct *) * pdf->nobjstms);
if (!pdf->objstms) {
cli_warnmsg("pdf_extract_obj: out of memory parsing object stream (%u)\n", pdf->nobjstms);
pdf_free_dict(dparams);
@ -1692,7 +1692,7 @@ cl_error_t pdf_extract_obj(struct pdf_struct *pdf, struct pdf_obj *obj, uint32_t
free(pdf->objstms);
pdf->objstms = NULL;
} else {
pdf->objstms = cli_max_realloc2(pdf->objstms, sizeof(struct objstm_struct *) * pdf->nobjstms);
pdf->objstms = cli_max_realloc_or_free(pdf->objstms, sizeof(struct objstm_struct *) * pdf->nobjstms);
if (!pdf->objstms) {
cli_warnmsg("pdf_extract_obj: out of memory when shrinking down objstm array\n");

@ -2211,7 +2211,7 @@ static char *pe_ordinal(const char *dll, uint16_t ord)
if (name[0] == '\0')
sprintf(name, "ord%u", ord);
return cli_strdup(name);
return cli_safer_strdup(name);
}
static int validate_impname(const char *name, uint32_t length, int dll)
@ -3218,7 +3218,7 @@ int cli_scanpe(cli_ctx *ctx)
if (xsjs == 1280)
break;
if (!(jumps = (uint32_t *)cli_max_realloc2(jumps, (xsjs + 128) * sizeof(uint32_t)))) {
if (!(jumps = (uint32_t *)cli_max_realloc_or_free(jumps, (xsjs + 128) * sizeof(uint32_t)))) {
cli_exe_info_destroy(peinfo);
return CL_EMEM;
}

@ -1537,13 +1537,13 @@ static enum phish_status phishingCheck(cli_ctx* ctx, struct url_check* urls)
*/
/* Provide copies of the original URL's, because domain_list_match() may modify the buffer,
and we don't want that to happen in this case. */
realData = cli_strdup(urls->realLink.data);
realData = cli_safer_strdup(urls->realLink.data);
if (!realData) {
cli_errmsg("Phishcheck: Failed to allocate memory for temporary real link string.\n");
phishing_verdict = CL_PHISH_CLEAN;
goto done;
}
displayData = cli_strdup(urls->displayLink.data);
displayData = cli_safer_strdup(urls->displayLink.data);
if (!displayData) {
cli_errmsg("Phishcheck: Failed to allocate memory for temporary display link string.\n");
phishing_verdict = CL_PHISH_CLEAN;

@ -114,7 +114,7 @@ char *cli_virname(const char *virname, unsigned int official)
}
if (official)
return cli_strdup(virname);
return cli_safer_strdup(virname);
newname = (char *)malloc(strlen(virname) + 11 + 1);
if (!newname) {
@ -142,7 +142,7 @@ cl_error_t cli_sigopts_handler(struct cli_matcher *root, const char *virname, co
return CL_EPARSE;
}
hexcpy = cli_strdup(hexsig);
hexcpy = cli_safer_strdup(hexsig);
if (!hexcpy)
return CL_EMEM;
@ -373,7 +373,7 @@ static cl_error_t readdb_load_regex_subsignature(struct cli_matcher *root, const
}
/* get copied */
hexcpy = cli_strdup(sig);
hexcpy = cli_safer_strdup(sig);
if (!hexcpy) {
status = CL_EMEM;
goto done;
@ -420,7 +420,7 @@ static cl_error_t readdb_load_regex_subsignature(struct cli_matcher *root, const
done:
FREE(hexcpy);
CLI_FREE_AND_SET_NULL(hexcpy);
return status;
}
@ -634,7 +634,7 @@ done:
ffierror_free(fuzzy_hash_load_error);
}
FREE(hexcpy);
CLI_FREE_AND_SET_NULL(hexcpy);
return status;
}
@ -795,7 +795,7 @@ cl_error_t cli_add_content_match_pattern(struct cli_matcher *root, const char *v
// Make a copy of the whole pattern so that we can NULL-terminate the hexsig
// and pass it to cli_ac_addsig() without having to pass the part-length.
if (!(hexcpy = cli_strdup(hexsig)))
if (!(hexcpy = cli_safer_strdup(hexsig)))
return CL_EMEM;
start = pt = hexcpy;
@ -2330,7 +2330,7 @@ static int cli_loadcbc(FILE *fs, struct cl_engine *engine, unsigned int *signo,
return CL_SUCCESS;
}
bcs->all_bcs = cli_safer_realloc2(bcs->all_bcs, sizeof(*bcs->all_bcs) * (bcs->count + 1));
bcs->all_bcs = cli_safer_realloc_or_free(bcs->all_bcs, sizeof(*bcs->all_bcs) * (bcs->count + 1));
if (!bcs->all_bcs) {
cli_errmsg("cli_loadcbc: Can't allocate memory for bytecode entry\n");
return CL_EMEM;
@ -2401,7 +2401,7 @@ static int cli_loadcbc(FILE *fs, struct cl_engine *engine, unsigned int *signo,
if (bc->kind >= _BC_START_HOOKS && bc->kind < _BC_LAST_HOOK) {
unsigned hook = bc->kind - _BC_START_HOOKS;
unsigned cnt = ++engine->hooks_cnt[hook];
engine->hooks[hook] = cli_safer_realloc2(engine->hooks[hook],
engine->hooks[hook] = cli_safer_realloc_or_free(engine->hooks[hook],
sizeof(*engine->hooks[0]) * cnt);
if (!engine->hooks[hook]) {
cli_errmsg("Out of memory allocating memory for hook %u", hook);
@ -3720,7 +3720,7 @@ static cl_error_t ytable_add_attrib(struct cli_ytable *ytable, const char *hexsi
if (ytable->table[lookup]->offset)
free(ytable->table[lookup]->offset);
ytable->table[lookup]->offset = cli_strdup(value);
ytable->table[lookup]->offset = cli_safer_strdup(value);
if (!ytable->table[lookup]->offset) {
cli_yaramsg("ytable_add_attrib: ran out of memory for offset\n");
@ -3747,7 +3747,7 @@ static int ytable_add_string(struct cli_ytable *ytable, const char *hexsig)
return CL_EMEM;
}
new->hexstr = cli_strdup(hexsig);
new->hexstr = cli_safer_strdup(hexsig);
if (!new->hexstr) {
cli_yaramsg("ytable_add_string: out of memory for hexsig copy\n");
free(new);
@ -4221,10 +4221,10 @@ static int load_oneyara(YR_RULE *rule, int chkpua, struct cl_engine *engine, uns
/* TDB */
if (rule->cl_flags & RULE_EP && ytable.tbl_cnt == 1)
target_str = cli_strdup(YARATARGET1);
target_str = cli_safer_strdup(YARATARGET1);
else
#endif
target_str = cli_strdup(YARATARGET0);
target_str = cli_safer_strdup(YARATARGET0);
memset(&tdb, 0, sizeof(tdb));
if (CL_SUCCESS != (ret = init_tdb(&tdb, engine, target_str, newident))) {
@ -4592,7 +4592,7 @@ static int cli_loadpwdb(FILE *fs, struct cl_engine *engine, unsigned int options
/* append target type 0 to tdb string if needed */
if ((tokens[1][0] == '\0') || (strstr(tokens[1], "Target:") != NULL)) {
attribs = cli_strdup(tokens[1]);
attribs = cli_safer_strdup(tokens[1]);
if (!attribs) {
cli_errmsg("cli_loadpwdb: Can't allocate memory for attributes\n");
ret = CL_EMEM;
@ -5397,7 +5397,7 @@ cl_error_t cl_statinidir(const char *dirname, struct cl_stat *dbstat)
dbstat->entries = 0;
dbstat->stattab = NULL;
dbstat->statdname = NULL;
dbstat->dir = cli_strdup(dirname);
dbstat->dir = cli_safer_strdup(dirname);
} else {
cli_errmsg("cl_statdbdir(): Null argument passed.\n");
return CL_ENULLARG;
@ -5415,7 +5415,7 @@ cl_error_t cl_statinidir(const char *dirname, struct cl_stat *dbstat)
if (dent->d_ino) {
if (strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..") && CLI_DBEXT(dent->d_name)) {
dbstat->entries++;
dbstat->stattab = (STATBUF *)cli_safer_realloc2(dbstat->stattab, dbstat->entries * sizeof(STATBUF));
dbstat->stattab = (STATBUF *)cli_safer_realloc_or_free(dbstat->stattab, dbstat->entries * sizeof(STATBUF));
if (!dbstat->stattab) {
cl_statfree(dbstat);
closedir(dd);
@ -5423,7 +5423,7 @@ cl_error_t cl_statinidir(const char *dirname, struct cl_stat *dbstat)
}
#ifdef _WIN32
dbstat->statdname = (char **)cli_safer_realloc2(dbstat->statdname, dbstat->entries * sizeof(char *));
dbstat->statdname = (char **)cli_safer_realloc_or_free(dbstat->statdname, dbstat->entries * sizeof(char *));
if (!dbstat->statdname) {
cli_errmsg("cl_statinidir: Can't allocate memory for dbstat->statdname\n");
cl_statfree(dbstat);

@ -224,7 +224,7 @@ cl_error_t regex_list_match(struct regex_matcher *matcher, char *real_url, const
if (CL_SUCCESS != (rc = cli_ac_initdata(&mdata, 0, 0, 0, CLI_DEFAULT_AC_TRACKLEN)))
return rc;
bufrev = cli_strdup(buffer);
bufrev = cli_safer_strdup(buffer);
if (!bufrev)
return CL_EMEM;
@ -774,14 +774,14 @@ static cl_error_t add_pattern_suffix(void *cbdata, const char *suffix, size_t su
goto done;
}
MALLOC(regex, sizeof(*regex),
CLI_MALLOC_OR_GOTO_DONE(regex, sizeof(*regex),
cli_errmsg("add_pattern_suffix: Unable to allocate memory for regex\n");
ret = CL_EMEM);
if (NULL == iregex->pattern) {
regex->pattern = NULL;
} else {
CLI_STRDUP(iregex->pattern, regex->pattern,
CLI_SAFER_STRDUP_OR_GOTO_DONE(iregex->pattern, regex->pattern,
cli_errmsg("add_pattern_suffix: unable to strdup iregex->pattern");
ret = CL_EMEM);
}
@ -802,7 +802,7 @@ static cl_error_t add_pattern_suffix(void *cbdata, const char *suffix, size_t su
/* new suffix */
size_t n = matcher->suffix_cnt;
el = cli_hashtab_insert(&matcher->suffix_hash, suffix, suffix_len, (cli_element_data)n);
CLI_MAX_REALLOC(matcher->suffix_regexes,
CLI_MAX_REALLOC_OR_GOTO_DONE(matcher->suffix_regexes,
(n + 1) * sizeof(*matcher->suffix_regexes),
cli_errmsg("add_pattern_suffix: Unable to reallocate memory for matcher->suffix_regexes\n");
ret = CL_EMEM);
@ -817,7 +817,7 @@ static cl_error_t add_pattern_suffix(void *cbdata, const char *suffix, size_t su
if (CL_SUCCESS != ret) {
cli_hashtab_delete(&matcher->suffix_hash, suffix, suffix_len);
/*shrink the size back to what it was.*/
CLI_MAX_REALLOC(matcher->suffix_regexes, n * sizeof(*matcher->suffix_regexes));
CLI_MAX_REALLOC_OR_GOTO_DONE(matcher->suffix_regexes, n * sizeof(*matcher->suffix_regexes));
} else {
matcher->suffix_cnt++;
}
@ -825,8 +825,8 @@ static cl_error_t add_pattern_suffix(void *cbdata, const char *suffix, size_t su
done:
if (CL_SUCCESS != ret) {
FREE(regex->pattern);
FREE(regex);
CLI_FREE_AND_SET_NULL(regex->pattern);
CLI_FREE_AND_SET_NULL(regex);
}
return ret;
@ -869,13 +869,13 @@ static cl_error_t add_static_pattern(struct regex_matcher *matcher, char *patter
len = reverse_string(pattern);
regex.nxt = NULL;
CLI_STRDUP(pattern, regex.pattern,
CLI_SAFER_STRDUP_OR_GOTO_DONE(pattern, regex.pattern,
cli_errmsg("add_static_pattern: Cannot allocate memory for regex.pattern\n");
rc = CL_EMEM);
regex.preg = NULL;
rc = add_pattern_suffix(matcher, pattern, len, &regex);
done:
FREE(regex.pattern);
CLI_FREE_AND_SET_NULL(regex.pattern);
return rc;
}

@ -175,13 +175,13 @@ static void destroy_tree(struct node *n)
break;
case leaf_class:
if (n->u.leaf_class_bitmap != dot_bitmap)
FREE(n->u.leaf_class_bitmap);
CLI_FREE_AND_SET_NULL(n->u.leaf_class_bitmap);
break;
case root:
case leaf:
break;
}
FREE(n);
CLI_FREE_AND_SET_NULL(n);
}
static uint8_t *parse_char_class(const uint8_t *pat, size_t patSize, size_t *pos)
@ -193,7 +193,7 @@ static uint8_t *parse_char_class(const uint8_t *pat, size_t patSize, size_t *pos
do { \
if (((*posPtr) + incVal) >= posMax) { \
cli_warnmsg("parse_char_class: Invalid char class\n"); \
FREE(bitmap); \
CLI_FREE_AND_SET_NULL(bitmap); \
goto done; \
} \
(*posPtr)++; \
@ -207,7 +207,7 @@ static uint8_t *parse_char_class(const uint8_t *pat, size_t patSize, size_t *pos
int hasprev = 0;
uint8_t *bitmap = NULL;
MALLOC(bitmap, 32,
CLI_MALLOC_OR_GOTO_DONE(bitmap, 32,
cli_errmsg("parse_char_class: Unable to allocate memory for bitmap\n"));
if (pat[*pos] == '^') {
@ -222,7 +222,7 @@ static uint8_t *parse_char_class(const uint8_t *pat, size_t patSize, size_t *pos
unsigned char range_end;
unsigned int c;
if (0 == range_start) {
FREE(bitmap);
CLI_FREE_AND_SET_NULL(bitmap);
cli_errmsg("parse_char_class: range_start not initialized\n");
goto done;
}
@ -230,7 +230,7 @@ static uint8_t *parse_char_class(const uint8_t *pat, size_t patSize, size_t *pos
if (pat[*pos] == '[')
if (pat[*pos + 1] == '.') {
/* collating sequence not handled */
FREE(bitmap);
CLI_FREE_AND_SET_NULL(bitmap);
/* we are parsing the regex for a
* filter, be conservative and
* tell the filter that anything could
@ -248,7 +248,7 @@ static uint8_t *parse_char_class(const uint8_t *pat, size_t patSize, size_t *pos
hasprev = 0;
} else if (pat[*pos] == '[' && pat[*pos] == ':') {
/* char class */
FREE(bitmap);
CLI_FREE_AND_SET_NULL(bitmap);
while (pat[*pos] != ']') INC_POS(pos, patSize);
INC_POS(pos, patSize);
while (pat[*pos] != ']') INC_POS(pos, patSize);
@ -501,7 +501,7 @@ cl_error_t cli_regex2suffix(const char *pattern, regex_t *preg, suffix_callback
return rc;
}
regex.nxt = NULL;
CLI_STRDUP(pattern, regex.pattern,
CLI_SAFER_STRDUP_OR_GOTO_DONE(pattern, regex.pattern,
cli_errmsg("cli_regex2suffix: unable to strdup regex.pattern\n");
rc = REG_ESPACE);
@ -517,8 +517,8 @@ cl_error_t cli_regex2suffix(const char *pattern, regex_t *preg, suffix_callback
rc = build_suffixtree_descend(n, &buf, cb, cbdata, &regex);
done:
FREE(regex.pattern);
FREE(buf.data);
CLI_FREE_AND_SET_NULL(regex.pattern);
CLI_FREE_AND_SET_NULL(buf.data);
destroy_tree(n);
return rc;
}

@ -4014,7 +4014,7 @@ static cl_error_t dispatch_file_inspection_callback(clcb_file_inspection cb, cli
fmap = ctx->recursion_stack[fmap_index].fmap;
fd = fmap_fd(fmap);
CLI_MAX_CALLOC(ancestors, ctx->recursion_level + 1, sizeof(char *), status = CL_EMEM);
CLI_MAX_CALLOC_OR_GOTO_DONE(ancestors, ctx->recursion_level + 1, sizeof(char *), status = CL_EMEM);
file_name = fmap->name;
file_buffer = fmap_need_off_once_len(fmap, 0, fmap->len, &file_size);
@ -4056,7 +4056,7 @@ static cl_error_t dispatch_file_inspection_callback(clcb_file_inspection cb, cli
done:
FREE(ancestors);
CLI_FREE_AND_SET_NULL(ancestors);
return status;
}
@ -5406,7 +5406,7 @@ static cl_error_t scan_common(cl_fmap_t *map, const char *filepath, const char *
ctx.engine = engine;
ctx.scanned = scanned;
MALLOC(ctx.options, sizeof(struct cl_scan_options), status = CL_EMEM);
CLI_MALLOC_OR_GOTO_DONE(ctx.options, sizeof(struct cl_scan_options), status = CL_EMEM);
memcpy(ctx.options, scanoptions, sizeof(struct cl_scan_options));

@ -428,7 +428,7 @@ static cl_error_t real_scansis(cli_ctx *ctx, const char *tmpd)
}
if ((install_filepath = getsistring(map, pdname, sdname))) {
cli_dbgmsg("\tInstalled to: %s\n", install_filepath);
FREE(install_filepath);
CLI_FREE_AND_SET_NULL(install_filepath);
}
if (!(ptrs = cli_max_malloc(fcount * sizeof(uint32_t) * 3))) {
@ -492,7 +492,7 @@ static cl_error_t real_scansis(cli_ctx *ctx, const char *tmpd)
}
if (uncompress(decomp, &olen, comp, lens[j]) != Z_OK) {
cli_dbgmsg("\tUnpacking failure\n");
FREE(decomp);
CLI_FREE_AND_SET_NULL(decomp);
continue;
}
decompp = decomp;
@ -515,7 +515,7 @@ static cl_error_t real_scansis(cli_ctx *ctx, const char *tmpd)
goto done;
}
FREE(decomp);
CLI_FREE_AND_SET_NULL(decomp);
status = cli_magic_scan_desc(fd, ofn, ctx, original_filepath, LAYER_ATTRIBUTES_NONE);
if (CL_SUCCESS != status) {
@ -529,8 +529,8 @@ static cl_error_t real_scansis(cli_ctx *ctx, const char *tmpd)
}
}
FREE(original_filepath);
FREE(ptrs);
CLI_FREE_AND_SET_NULL(original_filepath);
CLI_FREE_AND_SET_NULL(ptrs);
fcount = 2 * sizeof(uint32_t);
break;
@ -569,10 +569,10 @@ done:
if (-1 != fd) {
close(fd);
}
FREE(original_filepath);
FREE(decomp);
FREE(ptrs);
FREE(alangs);
CLI_FREE_AND_SET_NULL(original_filepath);
CLI_FREE_AND_SET_NULL(decomp);
CLI_FREE_AND_SET_NULL(ptrs);
CLI_FREE_AND_SET_NULL(alangs);
return status;
}

@ -974,7 +974,7 @@ char *cli_unescape(const char *str)
R[i++] = c;
}
R[i++] = '\0';
R = cli_max_realloc2(R, i);
R = cli_max_realloc_or_free(R, i);
return R;
}

@ -91,7 +91,7 @@ int tableInsert(table_t *table, const char *key, int value)
for (tableItem = table->tableHead; tableItem; tableItem = tableItem->next)
if (tableItem->key == NULL) {
/* This item has been deleted */
tableItem->key = cli_strdup(key);
tableItem->key = cli_safer_strdup(key);
tableItem->value = value;
return value;
}
@ -109,7 +109,7 @@ int tableInsert(table_t *table, const char *key, int value)
}
table->tableLast->next = NULL;
table->tableLast->key = cli_strdup(key);
table->tableLast->key = cli_safer_strdup(key);
table->tableLast->value = value;
return value;

@ -100,7 +100,7 @@ done:
}
}
FREE(tmpf);
CLI_FREE_AND_SET_NULL(tmpf);
return status;
}
@ -623,7 +623,7 @@ typedef struct {
static void freePointerList(PointerList *pl)
{
FREE(pl->idxs);
CLI_FREE_AND_SET_NULL(pl->idxs);
memset(pl, 0, sizeof(PointerList));
}
@ -633,7 +633,7 @@ static cl_error_t initPointerList(PointerList *pl)
uint32_t capacity = POINTER_LIST_INCREMENT;
freePointerList(pl);
CALLOC(pl->idxs, capacity, sizeof(uint8_t *),
CLI_CALLOC_OR_GOTO_DONE(pl->idxs, capacity, sizeof(uint8_t *),
cli_errmsg("initPointerList: Can't allocate memory\n");
ret = CL_EMEM);
@ -648,7 +648,7 @@ static cl_error_t insertPointer(PointerList *pl, const uint8_t *pointer)
if (pl->cnt == (pl->capacity - 1)) {
uint32_t newCapacity = pl->capacity + POINTER_LIST_INCREMENT;
CLI_SAFER_REALLOC(pl->idxs, newCapacity * sizeof(uint8_t *),
CLI_SAFER_REALLOC_OR_GOTO_DONE(pl->idxs, newCapacity * sizeof(uint8_t *),
cli_errmsg("insertPointer: Can't allocate memory\n");
ret = CL_EMEM);

@ -2434,7 +2434,7 @@ create_vba_project(int record_count, const char *dir, struct uniq *U)
ret->name = (char **)cli_max_malloc(sizeof(char *) * record_count);
ret->colls = (uint32_t *)cli_max_malloc(sizeof(uint32_t) * record_count);
ret->dir = cli_strdup(dir);
ret->dir = cli_safer_strdup(dir);
ret->offset = (uint32_t *)cli_max_malloc(sizeof(uint32_t) * record_count);
if ((ret->colls == NULL) || (ret->name == NULL) || (ret->dir == NULL) || (ret->offset == NULL)) {

@ -4790,7 +4790,7 @@ cl_error_t cli_extract_xlm_macros_and_images(const char *dir, cli_ctx *ctx, char
} else {
/* already found the beginning of a drawing group, extract the remaining chunks */
drawinggroup_len += biff_header.length;
CLI_MAX_REALLOC(drawinggroup, drawinggroup_len, status = CL_EMEM);
CLI_MAX_REALLOC_OR_GOTO_DONE(drawinggroup, drawinggroup_len, status = CL_EMEM);
memcpy(drawinggroup + (drawinggroup_len - biff_header.length), data, biff_header.length);
// cli_dbgmsg("Collected %d drawing group bytes\n", biff_header.length);
}
@ -4801,7 +4801,7 @@ cl_error_t cli_extract_xlm_macros_and_images(const char *dir, cli_ctx *ctx, char
(NULL != drawinggroup)) {
/* already found the beginning of an image, extract the remaining chunks */
drawinggroup_len += biff_header.length;
CLI_MAX_REALLOC(drawinggroup, drawinggroup_len, status = CL_EMEM);
CLI_MAX_REALLOC_OR_GOTO_DONE(drawinggroup, drawinggroup_len, status = CL_EMEM);
memcpy(drawinggroup + (drawinggroup_len - biff_header.length), data, biff_header.length);
// cli_dbgmsg("Collected %d image bytes\n", biff_header.length);
}
@ -4977,7 +4977,7 @@ cl_error_t cli_extract_xlm_macros_and_images(const char *dir, cli_ctx *ctx, char
status = CL_SUCCESS;
done:
FREE(drawinggroup);
CLI_FREE_AND_SET_NULL(drawinggroup);
if (in_fd != -1) {
close(in_fd);
@ -4992,12 +4992,12 @@ done:
out_fd = -1;
}
FREE(data);
CLI_FREE_AND_SET_NULL(data);
if (tempfile && !ctx->engine->keeptmp) {
remove(tempfile);
}
FREE(tempfile);
CLI_FREE_AND_SET_NULL(tempfile);
return status;
}

@ -452,7 +452,7 @@ struct RE {
/*
* YARA to ClamAV function mappings
*/
#define yr_strdup cli_strdup
#define yr_strdup cli_safer_strdup
#define yr_malloc cli_max_malloc
#define yr_realloc cli_max_realloc
#define yr_free free

@ -882,7 +882,7 @@ YR_STRING* yr_parser_reduce_string_declaration(
}
if (identifier != NULL) {
meta->identifier = cli_strdup(identifier);
meta->identifier = cli_safer_strdup(identifier);
if (meta->identifier == NULL) {
cli_errmsg("yara_parser: no mem for meta->identifier.\n");
compiler->last_result = CL_EMEM;
@ -890,7 +890,7 @@ YR_STRING* yr_parser_reduce_string_declaration(
}
}
if (string != NULL) {
meta->string = cli_strdup(string);
meta->string = cli_safer_strdup(string);
if (meta->string == NULL) {
cli_errmsg("yara_parser: no mem for meta->string.\n");
compiler->last_result = CL_EMEM;

@ -158,7 +158,7 @@ fc_error_t fc_initialize(fc_config *fcConfig)
logg_size = fcConfig->maxLogSize;
/* Set a log file if requested, and is not already set */
if ((NULL == logg_file) && (NULL != fcConfig->logFile)) {
logg_file = cli_strdup(fcConfig->logFile);
logg_file = cli_safer_strdup(fcConfig->logFile);
if (0 != logg(LOGG_INFO_NF, "--------------------------------------\n")) {
mprintf(LOGG_ERROR, "Problem with internal logger (UpdateLogFile = %s).\n", logg_file);
status = FC_ELOGGING;
@ -188,14 +188,14 @@ fc_error_t fc_initialize(fc_config *fcConfig)
mprintf(LOGG_ERROR, "Your installation was built with libcurl version %u.%u.%u.\n", LIBCURL_VERSION_MAJOR, LIBCURL_VERSION_MINOR, LIBCURL_VERSION_PATCH);
mprintf(LOGG_ERROR, "LocalIP requires libcurl version 7.33.0 or higher and must include the c-ares optional dependency.\n");
#else
g_localIP = cli_strdup(fcConfig->localIP);
g_localIP = cli_safer_strdup(fcConfig->localIP);
#endif
}
if (NULL != fcConfig->userAgent) {
g_userAgent = cli_strdup(fcConfig->userAgent);
g_userAgent = cli_safer_strdup(fcConfig->userAgent);
}
if (NULL != fcConfig->proxyServer) {
g_proxyServer = cli_strdup(fcConfig->proxyServer);
g_proxyServer = cli_safer_strdup(fcConfig->proxyServer);
if (0 != fcConfig->proxyPort) {
g_proxyPort = fcConfig->proxyPort;
} else {
@ -215,10 +215,10 @@ fc_error_t fc_initialize(fc_config *fcConfig)
}
}
if (NULL != fcConfig->proxyUsername) {
g_proxyUsername = cli_strdup(fcConfig->proxyUsername);
g_proxyUsername = cli_safer_strdup(fcConfig->proxyUsername);
}
if (NULL != fcConfig->proxyPassword) {
g_proxyPassword = cli_strdup(fcConfig->proxyPassword);
g_proxyPassword = cli_safer_strdup(fcConfig->proxyPassword);
}
#ifdef _WIN32
@ -234,7 +234,7 @@ fc_error_t fc_initialize(fc_config *fcConfig)
"%s" PATHSEP,
fcConfig->databaseDirectory);
} else {
g_databaseDirectory = cli_strdup(fcConfig->databaseDirectory);
g_databaseDirectory = cli_safer_strdup(fcConfig->databaseDirectory);
}
/* Validate that the database directory exists, and store it. */
@ -249,7 +249,7 @@ fc_error_t fc_initialize(fc_config *fcConfig)
goto done;
}
g_tempDirectory = cli_strdup(fcConfig->tempDirectory);
g_tempDirectory = cli_safer_strdup(fcConfig->tempDirectory);
g_maxAttempts = fcConfig->maxAttempts;
g_connectTimeout = fcConfig->connectTimeout;
@ -573,7 +573,7 @@ fc_error_t fc_dns_query_update_info(
logg(LOGG_WARNING, "Your ClamAV installation is OUTDATED!\n");
logg(LOGG_WARNING, "Local version: %s Recommended version: %s\n", version_string, reply_token);
logg(LOGG_INFO, "DON'T PANIC! Read https://docs.clamav.net/manual/Installing.html\n");
*newVersion = cli_strdup(reply_token);
*newVersion = cli_safer_strdup(reply_token);
}
}
}

@ -1722,7 +1722,7 @@ static struct cl_cvd *currentdb(const char *database, char **localname)
}
if (localname) {
*localname = cli_strdup(filename);
*localname = cli_safer_strdup(filename);
}
done:
@ -2045,9 +2045,9 @@ static fc_error_t query_remote_database_version(
}
if (remote_is_cld) {
*remoteFilename = cli_strdup(cldfile);
*remoteFilename = cli_safer_strdup(cldfile);
} else {
*remoteFilename = cli_strdup(cvdfile);
*remoteFilename = cli_safer_strdup(cvdfile);
}
*remoteVersion = newVersion;
@ -2174,7 +2174,7 @@ static fc_error_t check_for_new_database_version(
*remoteVersion = remotever;
if (NULL != remotename) {
*remoteFilename = cli_strdup(remotename);
*remoteFilename = cli_safer_strdup(remotename);
if (NULL == *remoteFilename) {
logg(LOGG_ERROR, "check_for_new_database_version: Failed to allocate memory for remote filename.\n");
status = FC_EMEM;
@ -2183,7 +2183,7 @@ static fc_error_t check_for_new_database_version(
}
if (NULL != localname) {
*localVersion = localver;
*localFilename = cli_strdup(localname);
*localFilename = cli_safer_strdup(localname);
if (NULL == *localFilename) {
logg(LOGG_ERROR, "check_for_new_database_version: Failed to allocate memory for local filename.\n");
status = FC_EMEM;
@ -2268,7 +2268,7 @@ fc_error_t updatedb(
}
if ((localVersion >= remoteVersion) && (NULL != localFilename)) {
*dbFilename = cli_strdup(localFilename);
*dbFilename = cli_safer_strdup(localFilename);
goto up_to_date;
}
@ -2288,7 +2288,7 @@ fc_error_t updatedb(
logg(LOGG_WARNING, "Expected newer version of %s database but the server's copy is not newer than our local file (version %d).\n", database, localVersion);
if (NULL != localFilename) {
/* Received a 304 (not modified), must be up-to-date after all */
*dbFilename = cli_strdup(localFilename);
*dbFilename = cli_safer_strdup(localFilename);
}
goto up_to_date;
} else if (FC_EMIRRORNOTSYNC == ret) {
@ -2302,7 +2302,7 @@ fc_error_t updatedb(
goto done;
}
newLocalFilename = cli_strdup(remoteFilename);
newLocalFilename = cli_safer_strdup(remoteFilename);
} else {
/*
* Attempt scripted/CDIFF incremental update.
@ -2387,10 +2387,10 @@ fc_error_t updatedb(
}
}
newLocalFilename = cli_strdup(remoteFilename);
newLocalFilename = cli_safer_strdup(remoteFilename);
} else if (0 == numPatchesReceived) {
logg(LOGG_INFO, "The database server doesn't have the latest patch for the %s database (version %u). The server will likely have updated if you check again in a few hours.\n", database, remoteVersion);
*dbFilename = cli_strdup(localFilename);
*dbFilename = cli_safer_strdup(localFilename);
goto up_to_date;
} else {
/*
@ -2488,7 +2488,7 @@ fc_error_t updatedb(
*signo = cvd->sigs;
*bUpdated = 1;
*dbFilename = cli_strdup(newLocalFilename);
*dbFilename = cli_safer_strdup(newLocalFilename);
if (NULL == *dbFilename) {
logg(LOGG_ERROR, "updatedb: Failed to allocate memory for database filename.\n");
status = FC_EMEM;
@ -2709,7 +2709,7 @@ fc_error_t updatecustomdb(
up_to_date:
*dbFilename = cli_strdup(databaseName);
*dbFilename = cli_safer_strdup(databaseName);
if (NULL == *dbFilename) {
logg(LOGG_ERROR, "Failed to allocate memory for database filename.\n");
status = FC_EMEM;

@ -273,8 +273,8 @@ START_TEST(regex_list_match_test)
ck_assert_msg(rtest->result == RTR_PHISH || rtest->result == RTR_ALLOWED || rtest->result == RTR_INVALID_REGEX,
"Allow list test result must be either RTR_PHISH or RTR_ALLOWED or RTR_INVALID_REGEX");
pattern = cli_strdup(rtest->pattern);
ck_assert_msg(!!pattern, "cli_strdup");
pattern = cli_safer_strdup(rtest->pattern);
ck_assert_msg(!!pattern, "cli_safer_strdup");
rc = regex_list_add_pattern(&matcher, pattern);
if (rtest->result == RTR_INVALID_REGEX) {
@ -292,7 +292,7 @@ START_TEST(regex_list_match_test)
ck_assert_msg(is_regex_ok(&matcher), "is_regex_ok");
realurl = cli_strdup(rtest->realurl);
realurl = cli_safer_strdup(rtest->realurl);
rc = regex_list_match(&matcher, realurl, rtest->displayurl, NULL, 1, &info, 1);
ck_assert_msg(rc == rtest->result, "regex_list_match");
/* regex_list_match is not supposed to modify realurl in this case */
@ -393,8 +393,8 @@ static void do_phishing_test(const struct rtest *rtest)
memset(&options, 0, sizeof(struct cl_scan_options));
ctx.options = &options;
realurl = cli_strdup(rtest->realurl);
ck_assert_msg(!!realurl, "cli_strdup");
realurl = cli_safer_strdup(rtest->realurl);
ck_assert_msg(!!realurl, "cli_safer_strdup");
hrefs.count = 1;
hrefs.value = malloc(sizeof(*hrefs.value));
@ -404,8 +404,8 @@ static void do_phishing_test(const struct rtest *rtest)
ck_assert_msg(!!hrefs.contents, "malloc");
hrefs.tag = malloc(sizeof(*hrefs.tag));
ck_assert_msg(!!hrefs.tag, "malloc");
hrefs.tag[0] = (unsigned char *)cli_strdup("href");
hrefs.contents[0] = (unsigned char *)cli_strdup(rtest->displayurl);
hrefs.tag[0] = (unsigned char *)cli_safer_strdup("href");
hrefs.contents[0] = (unsigned char *)cli_safer_strdup(rtest->displayurl);
ctx.engine = engine;
ctx.evidence = evidence_new();
@ -480,8 +480,8 @@ static void do_phishing_test_allscan(const struct rtest *rtest)
memset(&options, 0, sizeof(struct cl_scan_options));
ctx.options = &options;
realurl = cli_strdup(rtest->realurl);
ck_assert_msg(!!realurl, "cli_strdup");
realurl = cli_safer_strdup(rtest->realurl);
ck_assert_msg(!!realurl, "cli_safer_strdup");
hrefs.count = 1;
hrefs.value = malloc(sizeof(*hrefs.value));
@ -491,8 +491,8 @@ static void do_phishing_test_allscan(const struct rtest *rtest)
ck_assert_msg(!!hrefs.contents, "malloc");
hrefs.tag = malloc(sizeof(*hrefs.tag));
ck_assert_msg(!!hrefs.tag, "malloc");
hrefs.tag[0] = (unsigned char *)cli_strdup("href");
hrefs.contents[0] = (unsigned char *)cli_strdup(rtest->displayurl);
hrefs.tag[0] = (unsigned char *)cli_safer_strdup("href");
hrefs.contents[0] = (unsigned char *)cli_safer_strdup(rtest->displayurl);
ctx.engine = engine;
ctx.evidence = evidence_new();

Loading…
Cancel
Save