Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary memory allocation limit checks and rename max-allocation related functions #1137

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clambc/bcrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ int main(int argc, char *argv[])
cctx.evidence = evidence_new();

cctx.recursion_stack_size = cctx.engine->max_recursion_level;
cctx.recursion_stack = cli_calloc(sizeof(recursion_level_t), cctx.recursion_stack_size);
cctx.recursion_stack = calloc(sizeof(recursion_level_t), cctx.recursion_stack_size);
if (!cctx.recursion_stack) {
fprintf(stderr, "Out of memory\n");
exit(3);
Expand Down
2 changes: 1 addition & 1 deletion clamdscan/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion clamdscan/proto.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ struct client_parallel_data {
unsigned int id;
const char *file;
struct SCANID *next;
} * ids;
} *ids;
};

/* Sends a proper scan request to clamd and parses its replies
Expand Down
2 changes: 1 addition & 1 deletion clamonacc/clamonacc.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ static void onas_handle_signals(void)

struct onas_context *onas_init_context(void)
{
struct onas_context *ctx = (struct onas_context *)cli_malloc(sizeof(struct onas_context));
struct onas_context *ctx = (struct onas_context *)malloc(sizeof(struct onas_context));
if (NULL == ctx) {
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion clamonacc/client/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
6 changes: 3 additions & 3 deletions clamonacc/fanotif/fanotif.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ int onas_fan_eloop(struct onas_context **ctx)
if (scan) {
struct onas_scan_event *event_data;

event_data = cli_calloc(1, sizeof(struct onas_scan_event));
event_data = calloc(1, sizeof(struct onas_scan_event));
if (NULL == event_data) {
close(fmd->fd);
logg(LOGG_ERROR, "ClamFanotif: could not allocate memory for event data struct\n");
Expand All @@ -247,15 +247,15 @@ int onas_fan_eloop(struct onas_context **ctx)

/* fanotify specific stuffs */
event_data->bool_opts |= ONAS_SCTH_B_FANOTIFY;
event_data->fmd = cli_malloc(sizeof(struct fanotify_event_metadata));
event_data->fmd = malloc(sizeof(struct fanotify_event_metadata));
if (NULL == event_data->fmd) {
close(fmd->fd);
free(event_data);
logg(LOGG_ERROR, "ClamFanotif: could not allocate memory for event data struct fmd\n");
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);
Expand Down
14 changes: 7 additions & 7 deletions clamonacc/inotif/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ int onas_ht_init(struct onas_ht **ht, uint32_t size)

if (size == 0 || (size & (~size + 1)) != size) return CL_EARG;

*ht = (struct onas_ht *)cli_malloc(sizeof(struct onas_ht));
*ht = (struct onas_ht *)malloc(sizeof(struct onas_ht));
if (!(*ht)) return CL_EMEM;

**ht = (struct onas_ht){
Expand All @@ -145,7 +145,7 @@ int onas_ht_init(struct onas_ht **ht, uint32_t size)
.nbckts = 0,
};

if (!((*ht)->htable = (struct onas_bucket **)cli_calloc(size, sizeof(struct onas_bucket *)))) {
if (!((*ht)->htable = (struct onas_bucket **)calloc(size, sizeof(struct onas_bucket *)))) {
onas_free_ht(*ht);
return CL_EMEM;
}
Expand Down Expand Up @@ -180,7 +180,7 @@ void onas_free_ht(struct onas_ht *ht)
static struct onas_bucket *onas_bucket_init()
{

struct onas_bucket *bckt = (struct onas_bucket *)cli_malloc(sizeof(struct onas_bucket));
struct onas_bucket *bckt = (struct onas_bucket *)malloc(sizeof(struct onas_bucket));
if (!bckt) return NULL;

*bckt = (struct onas_bucket){
Expand Down Expand Up @@ -216,7 +216,7 @@ static void onas_free_bucket(struct onas_bucket *bckt)
struct onas_element *onas_element_init(struct onas_hnode *value, const char *key, size_t klen)
{

struct onas_element *elem = (struct onas_element *)cli_malloc(sizeof(struct onas_element));
struct onas_element *elem = (struct onas_element *)malloc(sizeof(struct onas_element));
if (!elem) return NULL;

*elem = (struct onas_element){
Expand Down Expand Up @@ -392,7 +392,7 @@ static int onas_bucket_remove(struct onas_bucket *bckt, struct onas_element *ele
static struct onas_hnode *onas_hashnode_init(void)
{
struct onas_hnode *hnode = NULL;
if (!(hnode = (struct onas_hnode *)cli_malloc(sizeof(struct onas_hnode)))) {
if (!(hnode = (struct onas_hnode *)malloc(sizeof(struct onas_hnode)))) {
return NULL;
}

Expand Down Expand Up @@ -428,7 +428,7 @@ static struct onas_hnode *onas_hashnode_init(void)
static struct onas_lnode *onas_listnode_init(void)
{
struct onas_lnode *lnode = NULL;
if (!(lnode = (struct onas_lnode *)cli_malloc(sizeof(struct onas_lnode)))) {
if (!(lnode = (struct onas_lnode *)malloc(sizeof(struct onas_lnode)))) {
return NULL;
}

Expand Down Expand Up @@ -794,7 +794,7 @@ int onas_ht_rm_hierarchy(struct onas_ht *ht, const char *pathname, size_t len, i
curr = curr->next;

size_t size = len + strlen(curr->dirname) + 2;
char *child_path = (char *)cli_malloc(size);
char *child_path = (char *)malloc(size);
if (child_path == NULL)
return CL_EMEM;
if (hnode->pathname[len - 1] == '/')
Expand Down
14 changes: 7 additions & 7 deletions clamonacc/inotif/inotif.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ static int onas_ddd_init_wdlt(uint64_t nwatches)

if (nwatches <= 0) return CL_EARG;

wdlt = (char **)cli_calloc(nwatches << 1, sizeof(char *));
wdlt = (char **)calloc(nwatches << 1, sizeof(char *));
micahsnyder marked this conversation as resolved.
Show resolved Hide resolved
if (!wdlt) return CL_EMEM;

wdlt_len = nwatches << 1;
Expand All @@ -121,7 +121,7 @@ static int onas_ddd_grow_wdlt(void)

char **ptr = NULL;

ptr = (char **)cli_realloc(wdlt, wdlt_len << 1);
ptr = (char **)cli_safer_realloc(wdlt, wdlt_len << 1);
if (ptr) {
wdlt = ptr;
memset(&ptr[wdlt_len], 0, sizeof(char *) * (wdlt_len - 1));
Expand Down Expand Up @@ -243,7 +243,7 @@ static int onas_ddd_watch_hierarchy(const char *pathname, size_t len, int fd, ui
curr = curr->next;

size_t size = len + strlen(curr->dirname) + 2;
char *child_path = (char *)cli_malloc(size);
char *child_path = (char *)malloc(size);
micahsnyder marked this conversation as resolved.
Show resolved Hide resolved
if (child_path == NULL) {
logg(LOGG_ERROR, "ClamInotif: out of memory when adding child for %s\n", hnode->pathname);
return CL_EMEM;
Expand Down Expand Up @@ -330,7 +330,7 @@ static int onas_ddd_unwatch_hierarchy(const char *pathname, size_t len, int fd,
curr = curr->next;

size_t size = len + strlen(curr->dirname) + 2;
char *child_path = (char *)cli_malloc(size);
char *child_path = (char *)malloc(size);
micahsnyder marked this conversation as resolved.
Show resolved Hide resolved
if (child_path == NULL)
return CL_EMEM;
if (hnode->pathname[len - 1] == '/')
Expand Down Expand Up @@ -679,7 +679,7 @@ void *onas_ddd_th(void *arg)
} else {
len = strlen(path);
size_t size = strlen(child) + len + 2;
char *child_path = (char *)cli_malloc(size);
char *child_path = (char *)malloc(size);
if (child_path == NULL) {
logg(LOGG_DEBUG, "ClamInotif: could not allocate space for child path ... aborting\n");
return NULL;
Expand Down Expand Up @@ -814,14 +814,14 @@ static void onas_ddd_handle_extra_scanning(struct onas_context *ctx, const char

struct onas_scan_event *event_data;

event_data = (struct onas_scan_event *)cli_calloc(1, sizeof(struct onas_scan_event));
event_data = (struct onas_scan_event *)calloc(1, sizeof(struct onas_scan_event));
if (NULL == event_data) {
logg(LOGG_ERROR, "ClamInotif: could not allocate memory for event data struct\n");
}

/* 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 */
Expand Down
4 changes: 2 additions & 2 deletions clamonacc/misc/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ char **onas_get_opt_list(const char *fname, int *num_entries, cl_error_t *err)

*num_entries = 0;

opt_list = cli_malloc(sizeof(char *));
opt_list = malloc(sizeof(char *));
if (NULL == opt_list) {
*err = CL_EMEM;
return NULL;
Expand Down Expand Up @@ -206,7 +206,7 @@ char **onas_get_opt_list(const char *fname, int *num_entries, cl_error_t *err)
}

(*num_entries)++;
rlc_ptr = cli_realloc(opt_list, sizeof(char *) * (*num_entries + 1));
rlc_ptr = cli_safer_realloc(opt_list, sizeof(char *) * (*num_entries + 1));
if (rlc_ptr) {
opt_list = rlc_ptr;
opt_list[*num_entries] = NULL;
Expand Down
2 changes: 1 addition & 1 deletion common/scanmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static inline char *wc2mb(const wchar_t *wc, DWORD flags)
return NULL;
}

mb = cli_malloc(len + 1);
mb = malloc(len + 1);
if (!mb) return NULL;

res = WideCharToMultiByte(CP_ACP, flags, wc, -1, mb, len, NULL, &invalid);
Expand Down
14 changes: 7 additions & 7 deletions freshclam/freshclam.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -535,7 +535,7 @@ static fc_error_t string_list_add(const char *item, char ***stringList, uint32_t
}

nItems = *nListItems + 1;
newList = (char **)cli_realloc(*stringList, nItems * sizeof(char *));
newList = (char **)cli_safer_realloc(*stringList, nItems * sizeof(char *));
if (newList == NULL) {
mprintf(LOGG_ERROR, "string_list_add: Failed to allocate memory for optional database list entry.\n");
status = FC_EMEM;
Expand All @@ -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;
Expand Down Expand Up @@ -1142,7 +1142,7 @@ fc_error_t select_from_official_databases(
goto done;
}

selectedDatabases = cli_calloc(nStandardDatabases + nOptionalDatabases, sizeof(char *));
selectedDatabases = calloc(nStandardDatabases + nOptionalDatabases, sizeof(char *));

/*
* Select desired standard databases.
Expand Down Expand Up @@ -1261,7 +1261,7 @@ fc_error_t select_specific_databases(
*databaseList = NULL;
*nDatabases = 0;

selectedDatabases = cli_calloc(nSpecificDatabases, sizeof(char *));
selectedDatabases = calloc(nSpecificDatabases, sizeof(char *));

/*
* Get lists of available databases.
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion libclamav/7z_iface.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ int cli_7unz(cli_ctx *ctx, size_t offset)
if (newnamelen > namelen) {
if (namelen > UTFBUFSZ)
free(utf16name);
utf16name = cli_malloc(newnamelen * 2);
utf16name = cli_max_malloc(newnamelen * 2);
if (!utf16name) {
found = CL_EMEM;
break;
Expand Down
2 changes: 1 addition & 1 deletion libclamav/apm.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include "scanners.h"
#include "dconf.h"

//#define DEBUG_APM_PARSE
// #define DEBUG_APM_PARSE

#ifdef DEBUG_APM_PARSE
#define apm_parsemsg(...) cli_dbgmsg(__VA_ARGS__)
Expand Down
6 changes: 3 additions & 3 deletions libclamav/aspack.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ int unaspack(uint8_t *image, unsigned int size, struct cli_exe_section *sections

blocks = image + ep + blocks_offset;

if (!(wrkbuf = cli_calloc(0x1800, sizeof(uint8_t)))) {
if (!(wrkbuf = calloc(0x1800, sizeof(uint8_t)))) {
cli_dbgmsg("Aspack: Unable to allocate dictionary\n");
return 0;
}
Expand All @@ -426,7 +426,7 @@ int unaspack(uint8_t *image, unsigned int size, struct cli_exe_section *sections
while (CLI_ISCONTAINED(image, size, blocks, 8) && (block_rva = cli_readint32(blocks)) && (block_size = cli_readint32(blocks + 4)) && CLI_ISCONTAINED(image, size, image + block_rva, block_size)) {

cli_dbgmsg("Aspack: unpacking block rva:%x - sz:%x\n", block_rva, block_size);
wrkbuf = (uint8_t *)cli_calloc(block_size + 0x10e, sizeof(uint8_t));
wrkbuf = (uint8_t *)cli_max_calloc(block_size + 0x10e, sizeof(uint8_t));

if (!wrkbuf) {
cli_dbgmsg("Aspack: Null work buff\n");
Expand Down Expand Up @@ -484,7 +484,7 @@ int unaspack(uint8_t *image, unsigned int size, struct cli_exe_section *sections
if (sectcount > 2 && ep == sections[sectcount - 2].rva && !sections[sectcount - 1].rsz) {
sectcount -= 2;
}
if (!(outsects = cli_malloc(sizeof(struct cli_exe_section) * sectcount))) {
if (!(outsects = cli_max_malloc(sizeof(struct cli_exe_section) * sectcount))) {
cli_dbgmsg("Aspack: OOM - rebuild failed\n");
cli_writen(f, image, size);
return 1; /* No whatsoheader - won't infloop in pe.c */
Expand Down
Loading
Loading