Skip to content

Commit

Permalink
bb6258 - Add warnings when allocations fail
Browse files Browse the repository at this point in the history
  • Loading branch information
Shawn Webb committed Mar 1, 2013
1 parent 63c6b0e commit 241e7eb
Show file tree
Hide file tree
Showing 33 changed files with 250 additions and 78 deletions.
12 changes: 9 additions & 3 deletions libclamav/bytecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -1390,8 +1390,10 @@ static int parseBB(struct cli_bc *bc, unsigned func, unsigned bb, unsigned char
return CL_EMALFDB;
}
bcfunc->dbgnodes = cli_malloc(num*sizeof(*bcfunc->dbgnodes));
if (!bcfunc->dbgnodes)
if (!bcfunc->dbgnodes) {
cli_errmsg("Unable to allocate memory for dbg nodes: %s\n", num*sizeof(*bcfunc->dbgnodes));
return CL_EMEM;
}
for (i=0;i<num;i++) {
bcfunc->dbgnodes[i] = readNumber(buffer, &offset, len, &ok);
if (!ok)
Expand Down Expand Up @@ -2042,8 +2044,10 @@ static int cli_bytecode_prepare_interpreter(struct cli_bc *bc)
int ret=CL_SUCCESS;
bc->numGlobalBytes = 0;
gmap = cli_malloc(bc->num_globals*sizeof(*gmap));
if (!gmap)
return CL_EMEM;
if (!gmap) {
cli_errmsg("interpreter: Unable to allocate memory for global map: %u\n", bc->num_globals*sizeof(*gmap));
return CL_EMEM;
}
for (j=0;j<bc->num_globals;j++) {
uint16_t ty = bc->globaltys[j];
unsigned align = typealign(bc, ty);
Expand All @@ -2055,6 +2059,7 @@ static int cli_bytecode_prepare_interpreter(struct cli_bc *bc)
if (bc->numGlobalBytes) {
bc->globalBytes = cli_calloc(1, bc->numGlobalBytes);
if (!bc->globalBytes) {
cli_errmsg("interpreter: Unable to allocate memory for globalBytes: %u\n", bc->numGlobalBytes);
free(gmap);
return CL_EMEM;
}
Expand Down Expand Up @@ -2122,6 +2127,7 @@ static int cli_bytecode_prepare_interpreter(struct cli_bc *bc)
unsigned totValues = bcfunc->numValues + bcfunc->numConstants + bc->num_globals;
unsigned *map = cli_malloc(sizeof(*map)*totValues);
if (!map) {
cli_errmsg("interpreter: Unable to allocate memory for map: %u\n", sizeof(*map)*totValues);
free(gmap);
return CL_EMEM;
}
Expand Down
6 changes: 4 additions & 2 deletions libclamav/bytecode_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,10 @@ static always_inline void* cli_stack_alloc(struct stack *stack, unsigned bytes)
}
/* not enough room here, allocate new chunk */
chunk = cli_malloc(sizeof(*stack->chunk));
if (!chunk)
return NULL;
if (!chunk) {
cli_warnmsg("cli_stack_alloc: Unable to allocate memory for stack-chunk: bytes: %u!\n", sizeof(*stack->chunk));
return NULL;
}

*(uint16_t*)&chunk->u.data[last_size_off] = stack->last_size;
stack->last_size = bytes/sizeof(align_t);
Expand Down
15 changes: 12 additions & 3 deletions libclamav/hashtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,10 @@ const struct cli_element* cli_hashtab_insert(struct cli_hashtable *s, const char
PROFILE_INSERT(s, tries);
}
thekey = cli_malloc(len+1);
if(!thekey)
if(!thekey) {
cli_errmsg("hashtab.c: Unable to allocate memory for thekey\n");
return NULL;
}
strncpy(thekey, key, len+1);
thekey[len]='\0';
element->key = thekey;
Expand Down Expand Up @@ -662,11 +664,13 @@ int cli_hashset_init(struct cli_hashset* hs, size_t initial_capacity, uint8_t lo
hs->keys = cli_malloc(initial_capacity * sizeof(*hs->keys));
hs->mempool = NULL;
if(!hs->keys) {
cli_errmsg("hashtab.c: Uable to allocate memory for hs->keys\n");
return CL_EMEM;
}
hs->bitmap = cli_calloc(initial_capacity >> 5, sizeof(*hs->bitmap));
if(!hs->bitmap) {
free(hs->keys);
cli_errmsg("hashtab.c: Unable to allocate memory for hs->bitmap\n");
return CL_EMEM;
}
return 0;
Expand All @@ -685,11 +689,13 @@ int cli_hashset_init_pool(struct cli_hashset* hs, size_t initial_capacity, uint8
hs->mempool = mempool;
hs->keys = mpool_malloc(mempool, initial_capacity * sizeof(*hs->keys));
if(!hs->keys) {
cli_errmsg("hashtab.c: Unable to allocate memory pool for hs->keys\n");
return CL_EMEM;
}
hs->bitmap = mpool_calloc(mempool, initial_capacity >> 5, sizeof(*hs->bitmap));
if(!hs->bitmap) {
mpool_free(mempool, hs->keys);
cli_errmsg("hashtab.c: Unable to allocate/initialize memory for hs->keys\n");
return CL_EMEM;
}
return 0;
Expand Down Expand Up @@ -820,6 +826,7 @@ ssize_t cli_hashset_toarray(const struct cli_hashset* hs, uint32_t** array)
}
*array = arr = cli_malloc(hs->count * sizeof(*arr));
if(!arr) {
cli_errmsg("hashtab.c: Unable to allocate memory for array\n");
return CL_EMEM;
}

Expand Down Expand Up @@ -927,8 +934,10 @@ int cli_map_setvalue(struct cli_map *m, const void* value, int32_t valuesize)
if (v->value)
free(v->value);
v->value = cli_malloc(valuesize);
if (!v->value)
return -CL_EMEM;
if (!v->value) {
cli_errmsg("hashtab.c: Unable to allocate memory for v->value\n");
return -CL_EMEM;
}
memcpy(v->value, value, valuesize);
v->valuesize = valuesize;
}
Expand Down
8 changes: 7 additions & 1 deletion libclamav/htmlnorm.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ static unsigned char *cli_readchunk(FILE *stream, m_area_t *m_area, unsigned int

chunk = (unsigned char *) cli_malloc(max_len);
if (!chunk) {
cli_errmsg("readchunk: Unable to allocate memory for chunk\n");
return NULL;
}

Expand Down Expand Up @@ -520,8 +521,10 @@ static inline void html_tag_contents_done(tag_arguments_t *tags,int idx, struct
unsigned char *p;
cont->contents[cont->pos++] = '\0';
p = cli_malloc(cont->pos);
if(!p)
if(!p) {
cli_errmsg("html_tag_contents_done: Unable to allocate memory for p\n");
return;
}
memcpy(p, cont->contents, cont->pos);
tags->contents[idx-1] = p;
cont->pos = 0;
Expand Down Expand Up @@ -702,6 +705,7 @@ static int cli_html_normalise(int fd, m_area_t *m_area, const char *dirname, tag

file_buff_o2 = (file_buff_t *) cli_malloc(sizeof(file_buff_t));
if (!file_buff_o2) {
cli_errmsg("cli_html_normalise: Unable to allocate memory for file_buff_o2\n");
file_buff_o2 = file_buff_text = NULL;
goto abort;
}
Expand All @@ -721,6 +725,7 @@ static int cli_html_normalise(int fd, m_area_t *m_area, const char *dirname, tag
close(file_buff_o2->fd);
free(file_buff_o2);
file_buff_o2 = file_buff_text = NULL;
cli_errmsg("cli_html_normalise: Unable to allocate memory for file_buff_text\n");
goto abort;
}

Expand Down Expand Up @@ -1596,6 +1601,7 @@ static int cli_html_normalise(int fd, m_area_t *m_area, const char *dirname, tag
if (dirname) {
file_tmp_o1 = (file_buff_t *) cli_malloc(sizeof(file_buff_t));
if (!file_tmp_o1) {
cli_errmsg("cli_html_normalise: Unable to allocate memory for file_tmp_o1\n");
goto abort;
}
snprintf(filename, 1024, "%s"PATHSEP"rfc2397", dirname);
Expand Down
6 changes: 4 additions & 2 deletions libclamav/ishield.c
Original file line number Diff line number Diff line change
Expand Up @@ -692,8 +692,10 @@ static int is_extract_cab(cli_ctx *ctx, uint64_t off, uint64_t size, uint64_t cs
int success = 0;
fmap_t *map = *ctx->fmap;

if(!(outbuf = cli_malloc(IS_CABBUFSZ)))
return CL_EMEM;
if(!(outbuf = cli_malloc(IS_CABBUFSZ))) {
cli_errmsg("is_extract_cab: Unable to allocate memory for outbuf\n");
return CL_EMEM;
}

if(!(tempfile = cli_gentemp(ctx->engine->tmpdir))) {
free(outbuf);
Expand Down
6 changes: 4 additions & 2 deletions libclamav/line.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ lineCreate(const char *data)
const size_t size = strlen(data);
line_t *ret = (line_t *)cli_malloc(size + 2);

if(ret == NULL)
return (line_t *)NULL;
if(ret == NULL) {
cli_errmsg("lineCreate: Unable to allocate memory for ret\n");
return (line_t *)NULL;
}

ret[0] = (char)1;
/*strcpy(&ret[1], data);*/
Expand Down
10 changes: 7 additions & 3 deletions libclamav/matcher-ac.c
Original file line number Diff line number Diff line change
Expand Up @@ -1353,8 +1353,10 @@ int cli_ac_scanbuff(const unsigned char *buffer, uint32_t length, const char **v

if(res) {
newres = (struct cli_ac_result *) malloc(sizeof(struct cli_ac_result));
if(!newres)
if(!newres) {
cli_errmsg("cli_ac_scanbuff: Can't allocate memory for newres %u\n", sizeof(struct cli_ac_result));
return CL_EMEM;
}
newres->virname = pt->virname;
newres->customdata = pt->customdata;
newres->next = *res;
Expand Down Expand Up @@ -1404,8 +1406,10 @@ int cli_ac_scanbuff(const unsigned char *buffer, uint32_t length, const char **v

if(res) {
newres = (struct cli_ac_result *) malloc(sizeof(struct cli_ac_result));
if(!newres)
return CL_EMEM;
if(!newres) {
cli_errmsg("cli_ac_scanbuff: Can't allocate memory for newres %u\n", sizeof(struct cli_ac_result));
return CL_EMEM;
}
newres->virname = pt->virname;
newres->customdata = pt->customdata;
newres->offset = realoff;
Expand Down
10 changes: 8 additions & 2 deletions libclamav/mbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -2437,6 +2437,7 @@ parseMimeHeader(message *m, const char *cmd, const table_t *rfc821Table, const c

buf = cli_malloc(strlen(ptr) + 1);
if(buf == NULL) {
cli_errmsg("parseMimeHeader: Unable to allocate memory for buf %u\n", strlen(ptr) + 1);
if(copy)
free(copy);
return -1;
Expand Down Expand Up @@ -2545,6 +2546,7 @@ parseMimeHeader(message *m, const char *cmd, const table_t *rfc821Table, const c
case CONTENT_DISPOSITION:
buf = cli_malloc(strlen(ptr) + 1);
if(buf == NULL) {
cli_errmsg("parseMimeHeader: Unable to allocate memory for buf %u\n", strlen(ptr) + 1);
if(copy)
free(copy);
return -1;
Expand Down Expand Up @@ -2621,8 +2623,10 @@ rfc822comments(const char *in, char *out)

if(out == NULL) {
out = cli_malloc(strlen(in) + 1);
if(out == NULL)
if(out == NULL) {
cli_errmsg("rfc822comments: Unable to allocate memory for out %u\n", strlen(in) + 1);
return NULL;
}
}

backslash = commentlevel = inquote = 0;
Expand Down Expand Up @@ -2687,8 +2691,10 @@ rfc2047(const char *in)
cli_dbgmsg("rfc2047 '%s'\n", in);
out = cli_malloc(strlen(in) + 1);

if(out == NULL)
if(out == NULL) {
cli_errmsg("rfc2047: Unable to allocate memory for out %u\n", strlen(in) + 1);
return NULL;
}

pout = out;

Expand Down
12 changes: 9 additions & 3 deletions libclamav/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -889,8 +889,10 @@ messageAddLine(message *m, line_t *line)
m->body_last = m->body_last->t_next;
}

if(m->body_last == NULL)
if(m->body_last == NULL) {
cli_errmsg("messageAddLine: out of memory for m->body_last\n");
return -1;
}

m->body_last->t_next = NULL;

Expand Down Expand Up @@ -2293,8 +2295,10 @@ rfc2231(const char *in)

/* Don't handle continuations, decode what we can */
p = ret = cli_malloc(strlen(in) + 16);
if(ret == NULL)
if(ret == NULL) {
cli_errmsg("rfc2331: out of memory, unable to proceed\n");
return NULL;
}

do {
switch(*in) {
Expand Down Expand Up @@ -2347,8 +2351,10 @@ rfc2231(const char *in)

ret = cli_malloc(strlen(in) + 1);

if(ret == NULL)
if(ret == NULL) {
cli_errmsg("rfc2331: out of memory for ret\n");
return NULL;
}

/*
* memcpy(out, in, (ptr - in));
Expand Down
4 changes: 4 additions & 0 deletions libclamav/mspack.c
Original file line number Diff line number Diff line change
Expand Up @@ -603,12 +603,14 @@ struct mszip_stream *mszip_init(int ofd,

/* allocate decompression state */
if (!(zip = cli_calloc(1, sizeof(struct mszip_stream)))) {
cli_errmsg("mszip_stream: Unable to allocate zip buffer\n");
return NULL;
}

/* allocate input buffer */
zip->inbuf = cli_malloc((size_t) input_buffer_size);
if (!zip->inbuf) {
cli_errmsg("mszip_stream: Unable to allocate input buffer\n");
free(zip);
return NULL;
}
Expand Down Expand Up @@ -1783,12 +1785,14 @@ struct qtm_stream *qtm_init(int ofd,
/* allocate decompression window and input buffer */
qtm->window = cli_malloc((size_t) window_size);
if (!qtm->window) {
cli_errmsg("qtm_init: Unable to allocate decompression window\n");
free(qtm);
return NULL;
}

qtm->inbuf = cli_malloc((size_t) input_buffer_size);
if (!qtm->inbuf) {
cli_errmsg("qtm_init: Unable to allocate input buffer\n");
free(qtm->window);
free(qtm);
return NULL;
Expand Down
7 changes: 6 additions & 1 deletion libclamav/ole2_extract.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ static char *get_property_name2(char *name, int size)

newname = (char *) cli_malloc(size*7);
if (!newname) {
cli_errmsg("OLE2 [get_property_name2]: Unable to allocate memory for newname: %u\n", size*7);
return NULL;
}
j=0;
Expand Down Expand Up @@ -179,7 +180,10 @@ static char *get_property_name(char *name, int size) {
if (csize<=0) return NULL;

newname = cname = (char *)cli_malloc(size);
if (!newname) return NULL;
if (!newname) {
cli_errmsg("OLE2 [get_property_name]: Unable to allocate memory for newname %u\n", size);
return NULL;
}

while(--csize) {
uint16_t lo, hi, u=cli_readint16(oname)-0x3800;
Expand Down Expand Up @@ -582,6 +586,7 @@ static int handler_writefile(ole2_header_t *hdr, property_t *prop, const char *d

buff = (unsigned char *) cli_malloc(1 << hdr->log2_big_block_size);
if (!buff) {
cli_errmsg("OLE2 [handler_writefile]: Unable to allocate memory for buff: %u\n", 1 << hdr->log2_big_block_size);
close(ofd);
return CL_BREAK;
}
Expand Down
10 changes: 8 additions & 2 deletions libclamav/others.c
Original file line number Diff line number Diff line change
Expand Up @@ -604,8 +604,10 @@ struct cl_settings *cl_engine_settings_copy(const struct cl_engine *engine)
struct cl_settings *settings;

settings = (struct cl_settings *) malloc(sizeof(struct cl_settings));
if(!settings)
return NULL;
if(!settings) {
cli_errmsg("cl_engine_settings_copy: Unable to allocate memory for settings %u\n", sizeof(struct cl_settings));
return NULL;
}

settings->ac_only = engine->ac_only;
settings->ac_mindepth = engine->ac_mindepth;
Expand Down Expand Up @@ -929,6 +931,7 @@ cli_rmdirs(const char *name)
path = cli_malloc(strlen(name) + strlen(dent->d_name) + 2);

if(path == NULL) {
cli_errmsg("cli_rmdirs: Unable to allocate memory for path %u\n", strlen(name) + strlen(dent->d_name) + 2);
closedir(dd);
return -1;
}
Expand Down Expand Up @@ -987,6 +990,7 @@ int cli_rmdirs(const char *dirname)
if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
path = cli_malloc(strlen(dirname) + strlen(dent->d_name) + 2);
if(!path) {
cli_errmsg("cli_rmdirs: Unable to allocate memory for path %u\n", strlen(dirname) + strlen(dent->d_name) + 2);
closedir(dd);
return -1;
}
Expand Down Expand Up @@ -1058,11 +1062,13 @@ bitset_t *cli_bitset_init(void)

bs = cli_malloc(sizeof(bitset_t));
if (!bs) {
cli_errmsg("cli_bitset_init: Unable to allocate memory for bs %u\n", sizeof(bitset_t));
return NULL;
}
bs->length = BITSET_DEFAULT_SIZE;
bs->bitset = cli_calloc(BITSET_DEFAULT_SIZE, 1);
if (!bs->bitset) {
cli_errmsg("cli_bitset_init: Unable to allocate memory for bs->bitset %u\n", BITSET_DEFAULT_SIZE);
free(bs);
return NULL;
}
Expand Down
Loading

0 comments on commit 241e7eb

Please sign in to comment.