Browse code

bb6258 - Add warnings when allocations fail

Shawn Webb authored on 2013/03/02 03:51:15
Showing 33 changed files
... ...
@@ -1390,8 +1390,10 @@ static int parseBB(struct cli_bc *bc, unsigned func, unsigned bb, unsigned char
1390 1390
 	    return CL_EMALFDB;
1391 1391
 	}
1392 1392
 	bcfunc->dbgnodes = cli_malloc(num*sizeof(*bcfunc->dbgnodes));
1393
-	if (!bcfunc->dbgnodes)
1393
+	if (!bcfunc->dbgnodes) {
1394
+        cli_errmsg("Unable to allocate memory for dbg nodes: %s\n", num*sizeof(*bcfunc->dbgnodes));
1394 1395
 	    return CL_EMEM;
1396
+    }
1395 1397
 	for (i=0;i<num;i++) {
1396 1398
 	    bcfunc->dbgnodes[i] = readNumber(buffer, &offset, len, &ok);
1397 1399
 	    if (!ok)
... ...
@@ -2042,8 +2044,10 @@ static int cli_bytecode_prepare_interpreter(struct cli_bc *bc)
2042 2042
     int ret=CL_SUCCESS;
2043 2043
     bc->numGlobalBytes = 0;
2044 2044
     gmap = cli_malloc(bc->num_globals*sizeof(*gmap));
2045
-    if (!gmap)
2046
-	return CL_EMEM;
2045
+    if (!gmap) {
2046
+        cli_errmsg("interpreter: Unable to allocate memory for global map: %u\n", bc->num_globals*sizeof(*gmap));
2047
+        return CL_EMEM;
2048
+    }
2047 2049
     for (j=0;j<bc->num_globals;j++) {
2048 2050
 	uint16_t ty = bc->globaltys[j];
2049 2051
 	unsigned align = typealign(bc, ty);
... ...
@@ -2055,6 +2059,7 @@ static int cli_bytecode_prepare_interpreter(struct cli_bc *bc)
2055 2055
     if (bc->numGlobalBytes) {
2056 2056
 	bc->globalBytes = cli_calloc(1, bc->numGlobalBytes);
2057 2057
 	if (!bc->globalBytes) {
2058
+        cli_errmsg("interpreter: Unable to allocate memory for globalBytes: %u\n", bc->numGlobalBytes);
2058 2059
         free(gmap);
2059 2060
 	    return CL_EMEM;
2060 2061
     }
... ...
@@ -2122,6 +2127,7 @@ static int cli_bytecode_prepare_interpreter(struct cli_bc *bc)
2122 2122
 	unsigned totValues = bcfunc->numValues + bcfunc->numConstants + bc->num_globals;
2123 2123
 	unsigned *map = cli_malloc(sizeof(*map)*totValues);
2124 2124
 	if (!map) {
2125
+        cli_errmsg("interpreter: Unable to allocate memory for map: %u\n", sizeof(*map)*totValues);
2125 2126
         free(gmap);
2126 2127
 	    return CL_EMEM;
2127 2128
     }
... ...
@@ -150,8 +150,10 @@ static always_inline void* cli_stack_alloc(struct stack *stack, unsigned bytes)
150 150
     }
151 151
     /* not enough room here, allocate new chunk */
152 152
     chunk = cli_malloc(sizeof(*stack->chunk));
153
-    if (!chunk)
154
-	return NULL;
153
+    if (!chunk) {
154
+        cli_warnmsg("cli_stack_alloc: Unable to allocate memory for stack-chunk: bytes: %u!\n", sizeof(*stack->chunk));
155
+        return NULL;
156
+    }
155 157
 
156 158
     *(uint16_t*)&chunk->u.data[last_size_off] = stack->last_size;
157 159
     stack->last_size = bytes/sizeof(align_t);
... ...
@@ -451,8 +451,10 @@ const struct cli_element* cli_hashtab_insert(struct cli_hashtable *s, const char
451 451
 					PROFILE_INSERT(s, tries);
452 452
 				}
453 453
 				thekey = cli_malloc(len+1);
454
-				if(!thekey)
454
+				if(!thekey) {
455
+                    cli_errmsg("hashtab.c: Unable to allocate memory for thekey\n");
455 456
 					return NULL;
457
+                }
456 458
 				strncpy(thekey, key, len+1);
457 459
 				thekey[len]='\0';
458 460
 				element->key = thekey;
... ...
@@ -662,11 +664,13 @@ int cli_hashset_init(struct cli_hashset* hs, size_t initial_capacity, uint8_t lo
662 662
 	hs->keys = cli_malloc(initial_capacity * sizeof(*hs->keys));
663 663
 	hs->mempool = NULL;
664 664
 	if(!hs->keys) {
665
+        cli_errmsg("hashtab.c: Uable to allocate memory for hs->keys\n");
665 666
 		return CL_EMEM;
666 667
 	}
667 668
 	hs->bitmap = cli_calloc(initial_capacity >> 5, sizeof(*hs->bitmap));
668 669
 	if(!hs->bitmap) {
669 670
 		free(hs->keys);
671
+        cli_errmsg("hashtab.c: Unable to allocate memory for hs->bitmap\n");
670 672
 		return CL_EMEM;
671 673
 	}
672 674
 	return 0;
... ...
@@ -685,11 +689,13 @@ int cli_hashset_init_pool(struct cli_hashset* hs, size_t initial_capacity, uint8
685 685
 	hs->mempool = mempool;
686 686
 	hs->keys = mpool_malloc(mempool, initial_capacity * sizeof(*hs->keys));
687 687
 	if(!hs->keys) {
688
+        cli_errmsg("hashtab.c: Unable to allocate memory pool for hs->keys\n");
688 689
 		return CL_EMEM;
689 690
 	}
690 691
 	hs->bitmap = mpool_calloc(mempool, initial_capacity >> 5, sizeof(*hs->bitmap));
691 692
 	if(!hs->bitmap) {
692 693
 		mpool_free(mempool, hs->keys);
694
+        cli_errmsg("hashtab.c: Unable to allocate/initialize memory for hs->keys\n");
693 695
 		return CL_EMEM;
694 696
 	}
695 697
 	return 0;
... ...
@@ -820,6 +826,7 @@ ssize_t cli_hashset_toarray(const struct cli_hashset* hs, uint32_t** array)
820 820
 	}
821 821
 	*array = arr = cli_malloc(hs->count * sizeof(*arr));
822 822
 	if(!arr) {
823
+        cli_errmsg("hashtab.c: Unable to allocate memory for array\n");
823 824
 		return CL_EMEM;
824 825
 	}
825 826
 
... ...
@@ -927,8 +934,10 @@ int  cli_map_setvalue(struct cli_map *m, const void* value, int32_t valuesize)
927 927
 	if (v->value)
928 928
 	    free(v->value);
929 929
 	v->value = cli_malloc(valuesize);
930
-	if (!v->value)
931
-	    return -CL_EMEM;
930
+	if (!v->value) {
931
+        cli_errmsg("hashtab.c: Unable to allocate  memory for v->value\n");
932
+        return -CL_EMEM;
933
+    }
932 934
 	memcpy(v->value, value, valuesize);
933 935
 	v->valuesize = valuesize;
934 936
     }
... ...
@@ -174,6 +174,7 @@ static unsigned char *cli_readchunk(FILE *stream, m_area_t *m_area, unsigned int
174 174
 
175 175
 	chunk = (unsigned char *) cli_malloc(max_len);
176 176
 	if (!chunk) {
177
+        cli_errmsg("readchunk: Unable to allocate memory for chunk\n");
177 178
 		return NULL;
178 179
 	}
179 180
 
... ...
@@ -520,8 +521,10 @@ static inline void html_tag_contents_done(tag_arguments_t *tags,int idx, struct
520 520
 	unsigned char *p;
521 521
 	cont->contents[cont->pos++] = '\0';
522 522
 	p = cli_malloc(cont->pos);
523
-	if(!p)
523
+	if(!p) {
524
+        cli_errmsg("html_tag_contents_done: Unable to allocate memory for p\n");
524 525
 		return;
526
+    }
525 527
 	memcpy(p, cont->contents, cont->pos);
526 528
 	tags->contents[idx-1] = p;
527 529
 	cont->pos = 0;
... ...
@@ -702,6 +705,7 @@ static int cli_html_normalise(int fd, m_area_t *m_area, const char *dirname, tag
702 702
 
703 703
 		file_buff_o2 = (file_buff_t *) cli_malloc(sizeof(file_buff_t));
704 704
 		if (!file_buff_o2) {
705
+            cli_errmsg("cli_html_normalise: Unable to allocate memory for file_buff_o2\n");
705 706
 			file_buff_o2 = file_buff_text = NULL;
706 707
 			goto abort;
707 708
 		}
... ...
@@ -721,6 +725,7 @@ static int cli_html_normalise(int fd, m_area_t *m_area, const char *dirname, tag
721 721
 			close(file_buff_o2->fd);
722 722
 			free(file_buff_o2);
723 723
 			file_buff_o2 = file_buff_text = NULL;
724
+            cli_errmsg("cli_html_normalise: Unable to allocate memory for file_buff_text\n");
724 725
 			goto abort;
725 726
 		}
726 727
 
... ...
@@ -1596,6 +1601,7 @@ static int cli_html_normalise(int fd, m_area_t *m_area, const char *dirname, tag
1596 1596
 				if (dirname) {
1597 1597
 					file_tmp_o1 = (file_buff_t *) cli_malloc(sizeof(file_buff_t));
1598 1598
 					if (!file_tmp_o1) {
1599
+                        cli_errmsg("cli_html_normalise: Unable to allocate memory for file_tmp_o1\n");
1599 1600
 						goto abort;
1600 1601
 					}
1601 1602
 					snprintf(filename, 1024, "%s"PATHSEP"rfc2397", dirname);
... ...
@@ -692,8 +692,10 @@ static int is_extract_cab(cli_ctx *ctx, uint64_t off, uint64_t size, uint64_t cs
692 692
     int success = 0;
693 693
     fmap_t *map = *ctx->fmap;
694 694
 
695
-    if(!(outbuf = cli_malloc(IS_CABBUFSZ)))
696
-	return CL_EMEM;
695
+    if(!(outbuf = cli_malloc(IS_CABBUFSZ))) {
696
+        cli_errmsg("is_extract_cab: Unable to allocate memory for outbuf\n");
697
+        return CL_EMEM;
698
+    }
697 699
 
698 700
     if(!(tempfile = cli_gentemp(ctx->engine->tmpdir))) {
699 701
 	free(outbuf);
... ...
@@ -72,8 +72,10 @@ lineCreate(const char *data)
72 72
 	const size_t size = strlen(data);
73 73
 	line_t *ret = (line_t *)cli_malloc(size + 2);
74 74
 
75
-	if(ret == NULL)
76
-		return (line_t *)NULL;
75
+    if(ret == NULL) {
76
+        cli_errmsg("lineCreate: Unable to allocate memory for ret\n");
77
+        return (line_t *)NULL;
78
+    }
77 79
 
78 80
 	ret[0] = (char)1;
79 81
 	/*strcpy(&ret[1], data);*/
... ...
@@ -1353,8 +1353,10 @@ int cli_ac_scanbuff(const unsigned char *buffer, uint32_t length, const char **v
1353 1353
 
1354 1354
 				    if(res) {
1355 1355
 					newres = (struct cli_ac_result *) malloc(sizeof(struct cli_ac_result));
1356
-					if(!newres)
1356
+					if(!newres) {
1357
+                        cli_errmsg("cli_ac_scanbuff: Can't allocate memory for newres %u\n", sizeof(struct cli_ac_result));
1357 1358
 					    return CL_EMEM;
1359
+                    }
1358 1360
 					newres->virname = pt->virname;
1359 1361
 					newres->customdata = pt->customdata;
1360 1362
 					newres->next = *res;
... ...
@@ -1404,8 +1406,10 @@ int cli_ac_scanbuff(const unsigned char *buffer, uint32_t length, const char **v
1404 1404
 
1405 1405
 				if(res) {
1406 1406
 				    newres = (struct cli_ac_result *) malloc(sizeof(struct cli_ac_result));
1407
-				    if(!newres)
1408
-					return CL_EMEM;
1407
+				    if(!newres) {
1408
+                        cli_errmsg("cli_ac_scanbuff: Can't allocate memory for newres %u\n", sizeof(struct cli_ac_result));
1409
+                        return CL_EMEM;
1410
+                    }
1409 1411
 				    newres->virname = pt->virname;
1410 1412
 				    newres->customdata = pt->customdata;
1411 1413
 				    newres->offset = realoff;
... ...
@@ -2437,6 +2437,7 @@ parseMimeHeader(message *m, const char *cmd, const table_t *rfc821Table, const c
2437 2437
 
2438 2438
 				buf = cli_malloc(strlen(ptr) + 1);
2439 2439
 				if(buf == NULL) {
2440
+                    cli_errmsg("parseMimeHeader: Unable to allocate memory for buf %u\n", strlen(ptr) + 1);
2440 2441
 					if(copy)
2441 2442
 						free(copy);
2442 2443
 					return -1;
... ...
@@ -2545,6 +2546,7 @@ parseMimeHeader(message *m, const char *cmd, const table_t *rfc821Table, const c
2545 2545
 		case CONTENT_DISPOSITION:
2546 2546
 			buf = cli_malloc(strlen(ptr) + 1);
2547 2547
 			if(buf == NULL) {
2548
+                cli_errmsg("parseMimeHeader: Unable to allocate memory for buf %u\n", strlen(ptr) + 1);
2548 2549
 				if(copy)
2549 2550
 					free(copy);
2550 2551
 				return -1;
... ...
@@ -2621,8 +2623,10 @@ rfc822comments(const char *in, char *out)
2621 2621
 
2622 2622
 	if(out == NULL) {
2623 2623
 		out = cli_malloc(strlen(in) + 1);
2624
-		if(out == NULL)
2624
+		if(out == NULL) {
2625
+            cli_errmsg("rfc822comments: Unable to allocate memory for out %u\n", strlen(in) + 1);
2625 2626
 			return NULL;
2627
+        }
2626 2628
 	}
2627 2629
 
2628 2630
 	backslash = commentlevel = inquote = 0;
... ...
@@ -2687,8 +2691,10 @@ rfc2047(const char *in)
2687 2687
 	cli_dbgmsg("rfc2047 '%s'\n", in);
2688 2688
 	out = cli_malloc(strlen(in) + 1);
2689 2689
 
2690
-	if(out == NULL)
2690
+	if(out == NULL) {
2691
+        cli_errmsg("rfc2047: Unable to allocate memory for out %u\n", strlen(in) + 1);
2691 2692
 		return NULL;
2693
+    }
2692 2694
 
2693 2695
 	pout = out;
2694 2696
 
... ...
@@ -889,8 +889,10 @@ messageAddLine(message *m, line_t *line)
889 889
 		m->body_last = m->body_last->t_next;
890 890
 	}
891 891
 
892
-	if(m->body_last == NULL)
892
+	if(m->body_last == NULL) {
893
+        cli_errmsg("messageAddLine: out of memory for m->body_last\n");
893 894
 		return -1;
895
+    }
894 896
 
895 897
 	m->body_last->t_next = NULL;
896 898
 
... ...
@@ -2293,8 +2295,10 @@ rfc2231(const char *in)
2293 2293
 
2294 2294
 		/* Don't handle continuations, decode what we can */
2295 2295
 		p = ret = cli_malloc(strlen(in) + 16);
2296
-		if(ret == NULL)
2296
+		if(ret == NULL) {
2297
+            cli_errmsg("rfc2331: out of memory, unable to proceed\n");
2297 2298
 			return NULL;
2299
+        }
2298 2300
 
2299 2301
 		do {
2300 2302
 			switch(*in) {
... ...
@@ -2347,8 +2351,10 @@ rfc2231(const char *in)
2347 2347
 
2348 2348
 	ret = cli_malloc(strlen(in) + 1);
2349 2349
 
2350
-	if(ret == NULL)
2350
+	if(ret == NULL) {
2351
+        cli_errmsg("rfc2331: out of memory for ret\n");
2351 2352
 		return NULL;
2353
+    }
2352 2354
 
2353 2355
 	/*
2354 2356
 	 * memcpy(out, in, (ptr - in));
... ...
@@ -603,12 +603,14 @@ struct mszip_stream *mszip_init(int ofd,
603 603
 
604 604
   /* allocate decompression state */
605 605
   if (!(zip = cli_calloc(1, sizeof(struct mszip_stream)))) {
606
+      cli_errmsg("mszip_stream: Unable to allocate zip buffer\n");
606 607
     return NULL;
607 608
   }
608 609
 
609 610
   /* allocate input buffer */
610 611
   zip->inbuf  = cli_malloc((size_t) input_buffer_size);
611 612
   if (!zip->inbuf) {
613
+      cli_errmsg("mszip_stream: Unable to allocate input buffer\n");
612 614
     free(zip);
613 615
     return NULL;
614 616
   }
... ...
@@ -1783,12 +1785,14 @@ struct qtm_stream *qtm_init(int ofd,
1783 1783
   /* allocate decompression window and input buffer */
1784 1784
   qtm->window = cli_malloc((size_t) window_size);
1785 1785
   if (!qtm->window) {
1786
+      cli_errmsg("qtm_init: Unable to allocate decompression window\n");
1786 1787
     free(qtm);
1787 1788
     return NULL;
1788 1789
   }
1789 1790
 
1790 1791
   qtm->inbuf  = cli_malloc((size_t) input_buffer_size);
1791 1792
   if (!qtm->inbuf) {
1793
+      cli_errmsg("qtm_init: Unable to allocate input buffer\n");
1792 1794
     free(qtm->window);
1793 1795
     free(qtm);
1794 1796
     return NULL;
... ...
@@ -138,6 +138,7 @@ static char *get_property_name2(char *name, int size)
138 138
 
139 139
 	newname = (char *) cli_malloc(size*7);
140 140
 	if (!newname) {
141
+        cli_errmsg("OLE2 [get_property_name2]: Unable to allocate memory for newname: %u\n", size*7);
141 142
 		return NULL;
142 143
 	}
143 144
 	j=0;
... ...
@@ -179,7 +180,10 @@ static char *get_property_name(char *name, int size) {
179 179
   if (csize<=0) return NULL;
180 180
 
181 181
   newname = cname = (char *)cli_malloc(size);
182
-  if (!newname) return NULL;
182
+  if (!newname) {
183
+      cli_errmsg("OLE2 [get_property_name]: Unable to allocate memory for newname %u\n", size);
184
+      return NULL;
185
+  }
183 186
 
184 187
   while(--csize) {
185 188
     uint16_t lo, hi, u=cli_readint16(oname)-0x3800;
... ...
@@ -582,6 +586,7 @@ static int handler_writefile(ole2_header_t *hdr, property_t *prop, const char *d
582 582
 
583 583
 	buff = (unsigned char *) cli_malloc(1 << hdr->log2_big_block_size);
584 584
 	if (!buff) {
585
+        cli_errmsg("OLE2 [handler_writefile]: Unable to allocate memory for buff: %u\n", 1 << hdr->log2_big_block_size);
585 586
 		close(ofd);
586 587
 		return CL_BREAK;
587 588
 	}
... ...
@@ -604,8 +604,10 @@ struct cl_settings *cl_engine_settings_copy(const struct cl_engine *engine)
604 604
 	struct cl_settings *settings;
605 605
 
606 606
     settings = (struct cl_settings *) malloc(sizeof(struct cl_settings));
607
-    if(!settings)
608
-	return NULL;
607
+    if(!settings) {
608
+        cli_errmsg("cl_engine_settings_copy: Unable to allocate memory for settings %u\n", sizeof(struct cl_settings));
609
+        return NULL;
610
+    }
609 611
 
610 612
     settings->ac_only = engine->ac_only;
611 613
     settings->ac_mindepth = engine->ac_mindepth;
... ...
@@ -929,6 +931,7 @@ cli_rmdirs(const char *name)
929 929
 	path = cli_malloc(strlen(name) + strlen(dent->d_name) + 2);
930 930
 
931 931
 	if(path == NULL) {
932
+        cli_errmsg("cli_rmdirs: Unable to allocate memory for path %u\n", strlen(name) + strlen(dent->d_name) + 2);
932 933
 	    closedir(dd);
933 934
 	    return -1;
934 935
 	}
... ...
@@ -987,6 +990,7 @@ int cli_rmdirs(const char *dirname)
987 987
 		    if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
988 988
 			path = cli_malloc(strlen(dirname) + strlen(dent->d_name) + 2);
989 989
 			if(!path) {
990
+                cli_errmsg("cli_rmdirs: Unable to allocate memory for path %u\n", strlen(dirname) + strlen(dent->d_name) + 2);
990 991
 			    closedir(dd);
991 992
 			    return -1;
992 993
 			}
... ...
@@ -1058,11 +1062,13 @@ bitset_t *cli_bitset_init(void)
1058 1058
 	
1059 1059
 	bs = cli_malloc(sizeof(bitset_t));
1060 1060
 	if (!bs) {
1061
+        cli_errmsg("cli_bitset_init: Unable to allocate memory for bs %u\n", sizeof(bitset_t));
1061 1062
 		return NULL;
1062 1063
 	}
1063 1064
 	bs->length = BITSET_DEFAULT_SIZE;
1064 1065
 	bs->bitset = cli_calloc(BITSET_DEFAULT_SIZE, 1);
1065 1066
 	if (!bs->bitset) {
1067
+        cli_errmsg("cli_bitset_init: Unable to allocate memory for bs->bitset %u\n", BITSET_DEFAULT_SIZE);
1066 1068
 	    free(bs);
1067 1069
 	    return NULL;
1068 1070
 	}
... ...
@@ -955,7 +955,7 @@ static int pdf_extract_obj(struct pdf_struct *pdf, struct pdf_obj *obj)
955 955
 	    if (obj->flags & (1 << OBJ_FILTER_AH)) {
956 956
 		ascii_decoded = cli_malloc(length/2 + 1);
957 957
 		if (!ascii_decoded) {
958
-		    cli_errmsg("Cannot allocate memory for asciidecode\n");
958
+		    cli_errmsg("Cannot allocate memory for ascii_decoded\n");
959 959
 		    rc = CL_EMEM;
960 960
 		    break;
961 961
 		}
... ...
@@ -965,7 +965,7 @@ static int pdf_extract_obj(struct pdf_struct *pdf, struct pdf_obj *obj)
965 965
 	    } else if (obj->flags & (1 << OBJ_FILTER_A85)) {
966 966
 		ascii_decoded = cli_malloc(length*5);
967 967
 		if (!ascii_decoded) {
968
-		    cli_errmsg("Cannot allocate memory for asciidecode\n");
968
+		    cli_errmsg("Cannot allocate memory for ascii_decoded\n");
969 969
 		    rc = CL_EMEM;
970 970
 		    break;
971 971
 		}
... ...
@@ -1535,8 +1535,10 @@ static char *pdf_readstring(const char *q0, int len, const char *key, unsigned *
1535 1535
 	q--;
1536 1536
 	len  = q - start;
1537 1537
 	s0 = s = cli_malloc(len + 1);
1538
-	if (!s)
1539
-	    return NULL;
1538
+	if (!s) {
1539
+        cli_errmsg("pdf_readstring: Unable to allocate buffer\n");
1540
+        return NULL;
1541
+    }
1540 1542
 	end = start + len;
1541 1543
         if (noescape) {
1542 1544
             memcpy(s0, start, len);
... ...
@@ -1729,8 +1731,10 @@ static void check_user_password(struct pdf_struct *pdf, int R, const char *O,
1729 1729
 	    } else {
1730 1730
 		pdf->keylen = 32;
1731 1731
 		pdf->key = cli_malloc(32);
1732
-		if (!pdf->key)
1733
-		    return;
1732
+		if (!pdf->key) {
1733
+            cli_errmsg("check_user_password: Cannot allocate memory for pdf->key\n");
1734
+            return;
1735
+        }
1734 1736
 		aes_decrypt(UE, &n, pdf->key, result2, 32, 0);
1735 1737
 		dbg_printhex("cli_pdf: Candidate encryption key", pdf->key, pdf->keylen);
1736 1738
 	    }
... ...
@@ -1694,6 +1694,7 @@ int cli_scanpe(cli_ctx *ctx)
1694 1694
 	}
1695 1695
 
1696 1696
 	if((sections = (struct cli_exe_section *) cli_malloc((sectcnt + 1) * sizeof(struct cli_exe_section))) == NULL) {
1697
+        cli_errmsg("FSG: Unable to allocate memory for sections %u\n", (sectcnt + 1) * sizeof(struct cli_exe_section));
1697 1698
 	    free(exe_sections);
1698 1699
 	    return CL_EMEM;
1699 1700
 	}
... ...
@@ -1793,6 +1794,7 @@ int cli_scanpe(cli_ctx *ctx)
1793 1793
 	}
1794 1794
 
1795 1795
 	if((sections = (struct cli_exe_section *) cli_malloc((sectcnt + 1) * sizeof(struct cli_exe_section))) == NULL) {
1796
+        cli_errmsg("FSG: Unable to allocate memory for sections %u\n", (sectcnt + 1) * sizeof(struct cli_exe_section));
1796 1797
 	    free(exe_sections);
1797 1798
 	    return CL_EMEM;
1798 1799
 	}
... ...
@@ -2040,6 +2042,7 @@ int cli_scanpe(cli_ctx *ctx)
2040 2040
 	CLI_UNPSIZELIMITS("PEspin", fsize);
2041 2041
 
2042 2042
 	if((spinned = (char *) cli_malloc(fsize)) == NULL) {
2043
+        cli_errmsg("PESping: Unable to allocate memory for spinned %u\n", fsize);
2043 2044
 	    free(exe_sections);
2044 2045
 	    return CL_EMEM;
2045 2046
 	}
... ...
@@ -2103,6 +2106,7 @@ int cli_scanpe(cli_ctx *ctx)
2103 2103
 	    char *spinned;
2104 2104
 
2105 2105
 	    if((spinned = (char *) cli_malloc(fsize)) == NULL) {
2106
+            cli_errmsg("yc: Unable to allocate memory for spinned %u\n", fsize);
2106 2107
 	      free(exe_sections);
2107 2108
 	      return CL_EMEM;
2108 2109
 	    }
... ...
@@ -2250,7 +2254,10 @@ int cli_scanpe(cli_ctx *ctx)
2250 2250
 	CLI_UNPSIZELIMITS("NsPack", MAX(ssize,dsize));
2251 2251
 
2252 2252
 	if (!ssize || !dsize || dsize != exe_sections[0].vsz) break;
2253
-	if (!(dest=cli_malloc(dsize))) break;
2253
+	if (!(dest=cli_malloc(dsize))) {
2254
+        cli_errmsg("NsPack: Unable to allocate memory for dest %u\n", dsize);
2255
+        break;
2256
+    }
2254 2257
 	/* memset(dest, 0xfc, dsize); */
2255 2258
 
2256 2259
 	if(!(src = fmap_need_off(map, start_of_stuff, ssize))) {
... ...
@@ -776,8 +776,10 @@ static int getmetrics(unsigned int side, unsigned int *imagedata, struct icomtr
776 776
     unsigned int edge_avg[6], edge_x[6]={0,0,0,0,0,0}, edge_y[6]={0,0,0,0,0,0}, noedge_avg[6], noedge_x[6]={0,0,0,0,0,0}, noedge_y[6]={0,0,0,0,0,0};
777 777
     double *sobel;
778 778
 
779
-    if(!(tmp = cli_malloc(side*side*4*2)))
780
-	return CL_EMEM;
779
+    if(!(tmp = cli_malloc(side*side*4*2))) {
780
+        cli_errmsg("getmetrics: Unable to allocate memory for tmp %u\n", (side*side*4*2));
781
+        return CL_EMEM;
782
+    }
781 783
 
782 784
     memset(res, 0, sizeof(*res));
783 785
 
... ...
@@ -939,6 +941,7 @@ static int getmetrics(unsigned int side, unsigned int *imagedata, struct icomtr
939 939
 #ifdef USE_FLOATS
940 940
     sobel = cli_malloc(side * side * sizeof(double));
941 941
     if(!sobel) {
942
+        cli_errmsg("getmetrics: Unable to allocate memory for edge detection %u\n", (side * side * sizeof(double)));
942 943
 	free(tmp);
943 944
 	return CL_EMEM;
944 945
     }
... ...
@@ -51,8 +51,10 @@ int init_domainlist(struct cl_engine* engine)
51 51
 {
52 52
 	if(engine) {
53 53
 		engine->domainlist_matcher = (struct regex_matcher *) cli_malloc(sizeof(struct regex_matcher));
54
-		if(!engine->domainlist_matcher)
54
+		if(!engine->domainlist_matcher) {
55
+            cli_errmsg("Phishcheck: Unable to allocate memory for init_domainlist\n");
55 56
 			return CL_EMEM;
57
+        }
56 58
 #ifdef USE_MPOOL
57 59
 		((struct regex_matcher*)engine->domainlist_matcher)->mempool = engine->mempool;
58 60
 #endif
... ...
@@ -52,8 +52,10 @@ int init_whitelist(struct cl_engine* engine)
52 52
 {
53 53
 	if(engine) {
54 54
 		engine->whitelist_matcher = (struct regex_matcher *) mpool_malloc(engine->mempool, sizeof(struct regex_matcher));
55
-		if(!engine->whitelist_matcher)
55
+		if(!engine->whitelist_matcher) {
56
+            cli_errmsg("Phish_whitelist: Unable to allocate memory for whitelist_match\n");
56 57
 			return CL_EMEM;
58
+        }
57 59
 #ifdef USE_MPOOL
58 60
 		((struct regex_matcher *)(engine->whitelist_matcher))->mempool = engine->mempool;
59 61
 #endif
... ...
@@ -264,8 +264,10 @@ static int string_assign_concatenated(struct string* dest, const char* prefix, c
264 264
 {
265 265
 	const size_t prefix_len = strlen(prefix);
266 266
 	char* ret = cli_malloc(prefix_len + end - begin + 1);
267
-	if(!ret)
267
+	if(!ret) {
268
+        cli_errmsg("Phishcheck: Unable to allocate memory for string_assign_concatonated\n");
268 269
 		return CL_EMEM;
270
+    }
269 271
 	strncpy(ret, prefix, prefix_len);
270 272
 	strncpy(ret+prefix_len, begin, end-begin);
271 273
 	ret[prefix_len+end-begin]='\0';
... ...
@@ -278,8 +280,10 @@ static int string_assign_concatenated(struct string* dest, const char* prefix, c
278 278
 static int string_assign_dup(struct string* dest,const char* start,const char* end)
279 279
 {
280 280
 	char* ret  = cli_malloc(end-start+1);
281
-	if(!ret)
281
+	if(!ret) {
282
+        cli_errmsg("Phishcheck: Unable to allocate memory for string_assign_dup\n");
282 283
 		return CL_EMEM;
284
+    }
283 285
 	strncpy(ret,start,end-start);
284 286
 	ret[end-start]='\0';
285 287
 
... ...
@@ -860,8 +864,10 @@ int phishing_init(struct cl_engine* engine)
860 860
 	struct phishcheck* pchk;
861 861
 	if(!engine->phishcheck) {
862 862
 		pchk = engine->phishcheck = mpool_malloc(engine->mempool, sizeof(struct phishcheck));
863
-		if(!pchk)
863
+		if(!pchk) {
864
+            cli_errmsg("Phishcheck: Unable to allocate memory for initialization\n");
864 865
 			return CL_EMEM;
866
+        }
865 867
 		pchk->is_disabled=1;
866 868
 	}
867 869
 	else {
... ...
@@ -554,8 +554,10 @@ static int cli_loaddb(FILE *fs, struct cl_engine *engine, unsigned int *signo, u
554 554
     root = engine->root[0];
555 555
 
556 556
     if(engine->ignored)
557
-	if(!(buffer_cpy = cli_malloc(FILEBUFF)))
557
+	if(!(buffer_cpy = cli_malloc(FILEBUFF))) {
558
+        cli_errmsg("cli_loaddb: Can't allocate memory for buffer_cpy\n");
558 559
 	    return CL_EMEM;
560
+    }
559 561
 
560 562
     while(cli_dbgets(buffer, FILEBUFF, fs, dbio)) {
561 563
 	line++;
... ...
@@ -629,6 +631,7 @@ static int cli_loadidb(FILE *fs, struct cl_engine *engine, unsigned int *signo,
629 629
     
630 630
     if(engine->ignored)
631 631
 	if(!(buffer_cpy = cli_malloc(FILEBUFF))) {
632
+        cli_errmsg("cli_loadidb: CAn't allocate memory for buffer_cpy\n");
632 633
 	    mpool_free(engine->mempool, matcher);
633 634
 	    return CL_EMEM;
634 635
 	}
... ...
@@ -903,8 +906,10 @@ static int cli_loadndb(FILE *fs, struct cl_engine *engine, unsigned int *signo,
903 903
 	return ret;
904 904
 
905 905
     if(engine->ignored)
906
-	if(!(buffer_cpy = cli_malloc(FILEBUFF)))
906
+	if(!(buffer_cpy = cli_malloc(FILEBUFF))) {
907
+        cli_errmsg("cli_loadndb: Can't allocate memory for buffer_cpy\n");
907 908
 	    return CL_EMEM;
909
+    }
908 910
 
909 911
     while(cli_dbgets(buffer, FILEBUFF, fs, dbio)) {
910 912
 	line++;
... ...
@@ -1405,8 +1410,10 @@ static int cli_loadldb(FILE *fs, struct cl_engine *engine, unsigned int *signo,
1405 1405
 	return ret;
1406 1406
 
1407 1407
     if(engine->ignored)
1408
-	if(!(buffer_cpy = cli_malloc(sizeof(buffer))))
1408
+	if(!(buffer_cpy = cli_malloc(sizeof(buffer)))) {
1409
+        cli_errmsg("cli_loadldb: Can't allocate memory for buffer_cpy\n");
1409 1410
 	    return CL_EMEM;
1411
+    }
1410 1412
     while(cli_dbgets(buffer, sizeof(buffer), fs, dbio)) {
1411 1413
 	line++;
1412 1414
 	if(buffer[0] == '#')
... ...
@@ -1940,8 +1947,10 @@ static int cli_loadhash(FILE *fs, struct cl_engine *engine, unsigned int *signo,
1940 1940
     }
1941 1941
 
1942 1942
     if(engine->ignored)
1943
-	if(!(buffer_cpy = cli_malloc(FILEBUFF)))
1943
+	if(!(buffer_cpy = cli_malloc(FILEBUFF))) {
1944
+        cli_errmsg("cli_loadhash: Can't allocate memory for buffer_cpy\n");
1944 1945
 	    return CL_EMEM;
1946
+    }
1945 1947
 
1946 1948
     while(cli_dbgets(buffer, FILEBUFF, fs, dbio)) {
1947 1949
 	line++;
... ...
@@ -2044,8 +2053,10 @@ static int cli_loadmd(FILE *fs, struct cl_engine *engine, unsigned int *signo, i
2044 2044
 
2045 2045
 
2046 2046
     if(engine->ignored)
2047
-	if(!(buffer_cpy = cli_malloc(FILEBUFF)))
2047
+	if(!(buffer_cpy = cli_malloc(FILEBUFF))) {
2048
+        cli_errmsg("cli_loadmd: Can't allocate memory for buffer_cpy\n");
2048 2049
 	    return CL_EMEM;
2050
+    }
2049 2051
 
2050 2052
     while(cli_dbgets(buffer, FILEBUFF, fs, dbio)) {
2051 2053
 	line++;
... ...
@@ -2197,8 +2208,10 @@ static int cli_loadcdb(FILE *fs, struct cl_engine *engine, unsigned int *signo,
2197 2197
 
2198 2198
 
2199 2199
     if(engine->ignored)
2200
-	if(!(buffer_cpy = cli_malloc(FILEBUFF)))
2200
+	if(!(buffer_cpy = cli_malloc(FILEBUFF))) {
2201
+        cli_errmsg("cli_loadcdb: Can't allocate memory for buffer_cpy\n");
2201 2202
 	    return CL_EMEM;
2203
+    }
2202 2204
 
2203 2205
     while(cli_dbgets(buffer, FILEBUFF, fs, dbio)) {
2204 2206
 	line++;
... ...
@@ -2734,14 +2747,14 @@ static int cli_loaddbdir(const char *dirname, struct cl_engine *engine, unsigned
2734 2734
 	    if(cli_strbcasestr(dent->d_name, ".ign") || cli_strbcasestr(dent->d_name, ".ign2")) {
2735 2735
 		dbfile = (char *) cli_malloc(strlen(dent->d_name) + strlen(dirname) + 2);
2736 2736
 		if(!dbfile) {
2737
-		    cli_dbgmsg("cli_loaddbdir(): dbfile == NULL\n");
2737
+		    cli_errmsg("cli_loaddbdir(): dbfile == NULL\n");
2738 2738
 		    closedir(dd);
2739 2739
 		    return CL_EMEM;
2740 2740
 		}
2741 2741
 		sprintf(dbfile, "%s"PATHSEP"%s", dirname, dent->d_name);
2742 2742
 		ret = cli_load(dbfile, engine, signo, options, NULL);
2743 2743
 		if(ret) {
2744
-		    cli_dbgmsg("cli_loaddbdir(): error loading database %s\n", dbfile);
2744
+		    cli_errmsg("cli_loaddbdir(): error loading database %s\n", dbfile);
2745 2745
 		    free(dbfile);
2746 2746
 		    closedir(dd);
2747 2747
 		    return ret;
... ...
@@ -2755,6 +2768,7 @@ static int cli_loaddbdir(const char *dirname, struct cl_engine *engine, unsigned
2755 2755
     dbfile = (char *) cli_malloc(strlen(dirname) + 20);
2756 2756
     if(!dbfile) {
2757 2757
 	closedir(dd);
2758
+    cli_errmsg("cli_loaddbdir: Can't allocate memory for dbfile\n");
2758 2759
 	return CL_EMEM;
2759 2760
     }
2760 2761
 
... ...
@@ -2832,14 +2846,14 @@ static int cli_loaddbdir(const char *dirname, struct cl_engine *engine, unsigned
2832 2832
 
2833 2833
 		dbfile = (char *) cli_malloc(strlen(dent->d_name) + strlen(dirname) + 2);
2834 2834
 		if(!dbfile) {
2835
-		    cli_dbgmsg("cli_loaddbdir(): dbfile == NULL\n");
2835
+		    cli_errmsg("cli_loaddbdir(): dbfile == NULL\n");
2836 2836
 		    closedir(dd);
2837 2837
 		    return CL_EMEM;
2838 2838
 		}
2839 2839
 		sprintf(dbfile, "%s"PATHSEP"%s", dirname, dent->d_name);
2840 2840
 		ret = cli_load(dbfile, engine, signo, options, NULL);
2841 2841
 		if(ret) {
2842
-		    cli_dbgmsg("cli_loaddbdir(): error loading database %s\n", dbfile);
2842
+		    cli_errmsg("cli_loaddbdir(): error loading database %s\n", dbfile);
2843 2843
 		    free(dbfile);
2844 2844
 		    closedir(dd);
2845 2845
 		    return ret;
... ...
@@ -2964,6 +2978,7 @@ int cl_statinidir(const char *dirname, struct cl_stat *dbstat)
2964 2964
 #ifdef _WIN32
2965 2965
 		dbstat->statdname = (char **) cli_realloc2(dbstat->statdname, dbstat->entries * sizeof(char *));
2966 2966
 		if(!dbstat->statdname) {
2967
+            cli_errmsg("cl_statinidir: Can't allocate memory for dbstat->statdname\n");
2967 2968
 		    cl_statfree(dbstat);
2968 2969
 		    closedir(dd);
2969 2970
 		    return CL_EMEM;
... ...
@@ -2972,6 +2987,7 @@ int cl_statinidir(const char *dirname, struct cl_stat *dbstat)
2972 2972
 
2973 2973
                 fname = cli_malloc(strlen(dirname) + strlen(dent->d_name) + 32);
2974 2974
 		if(!fname) {
2975
+            cli_errmsg("cl_statinidir: Cant' allocate memory for fname\n");
2975 2976
 		    cl_statfree(dbstat);
2976 2977
 		    closedir(dd);
2977 2978
 		    return CL_EMEM;
... ...
@@ -2980,6 +2996,7 @@ int cl_statinidir(const char *dirname, struct cl_stat *dbstat)
2980 2980
 #ifdef _WIN32
2981 2981
 		dbstat->statdname[dbstat->entries - 1] = (char *) cli_malloc(strlen(dent->d_name) + 1);
2982 2982
 		if(!dbstat->statdname[dbstat->entries - 1]) {
2983
+            cli_errmsg("cli_statinidir: Can't allocate memory for dbstat->statdname\n");
2983 2984
 		    cl_statfree(dbstat);
2984 2985
 		    closedir(dd);
2985 2986
 		    return CL_EMEM;
... ...
@@ -3036,6 +3053,7 @@ int cl_statchkdir(const struct cl_stat *dbstat)
3036 3036
 	    if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..") && CLI_DBEXT(dent->d_name)) {
3037 3037
                 fname = cli_malloc(strlen(dbstat->dir) + strlen(dent->d_name) + 32);
3038 3038
 		if(!fname) {
3039
+            cli_errmsg("cl_statchkdir: can't allocate memory for fname\n");
3039 3040
 		    closedir(dd);
3040 3041
 		    return CL_EMEM;
3041 3042
 		}
... ...
@@ -179,8 +179,10 @@ int regex_list_match(struct regex_matcher* matcher,char* real_url,const char* di
179 179
 		struct cli_ac_data mdata;
180 180
 		struct cli_ac_result *res = NULL;
181 181
 
182
-		if(!buffer)
182
+		if(!buffer) {
183
+            cli_errmsg("regex_list_match: Unable to allocate memory for buffer\n");
183 184
 			return CL_EMEM;
185
+        }
184 186
 
185 187
 		strncpy(buffer,real_url,real_len);
186 188
 		buffer[real_len]= (!is_whitelist && hostOnly) ? '/' : ':';
... ...
@@ -369,6 +371,7 @@ static int add_hash(struct regex_matcher *matcher, char* pattern, const char fl,
369 369
 	pat->virname = mpool_malloc(matcher->mempool, 1);
370 370
 	if(!pat->virname) {
371 371
 		free(pat);
372
+        cli_errmsg("add_hash: Unable to allocate memory for path->virname\n");
372 373
 		return CL_EMEM;
373 374
 	}
374 375
 	*pat->virname = fl;
... ...
@@ -592,6 +595,7 @@ static int add_newsuffix(struct regex_matcher *matcher, struct regex_list *info,
592 592
 	new->pattern = mpool_malloc(matcher->mempool, sizeof(new->pattern[0])*len);
593 593
 	if(!new->pattern) {
594 594
 		mpool_free(matcher->mempool, new);
595
+        cli_errmsg("add_newsuffix: Unable to allocate memory for new->pattern\n");
595 596
 		return CL_EMEM;
596 597
 	}
597 598
 	for(i=0;i<len;i++)
... ...
@@ -629,8 +633,10 @@ static int add_pattern_suffix(void *cbdata, const char *suffix, size_t suffix_le
629 629
 	const struct cli_element *el;
630 630
 
631 631
 	assert(matcher);
632
-	if(!regex)
632
+	if(!regex) {
633
+        cli_errmsg("add_pattern_suffix: Unable to allocate memory for regex\n");
633 634
 		return CL_EMEM;
635
+    }
634 636
 	regex->pattern = iregex->pattern ? cli_strdup(iregex->pattern) : NULL;
635 637
 	regex->preg = iregex->preg;
636 638
 	regex->nxt = NULL;
... ...
@@ -675,11 +681,15 @@ static regex_t *new_preg(struct regex_matcher *matcher)
675 675
 {
676 676
 	regex_t *r;
677 677
 	matcher->all_pregs = mpool_realloc(matcher->mempool, matcher->all_pregs, ++matcher->regex_cnt * sizeof(*matcher->all_pregs));
678
-	if(!matcher->all_pregs)
678
+	if(!matcher->all_pregs) {
679
+        cli_errmsg("new_preg: Unable to reallocate memory\n");
679 680
 		return NULL;
681
+    }
680 682
 	r = mpool_malloc(matcher->mempool, sizeof(*r));
681
-	if(!r)
683
+	if(!r) {
684
+        cli_errmsg("new_preg: Unable to allocate memory\n");
682 685
 		return NULL;
686
+    }
683 687
 	matcher->all_pregs[matcher->regex_cnt-1] = r;
684 688
 	return r;
685 689
 }
... ...
@@ -78,8 +78,10 @@ static struct node* make_node(enum node_type type, struct node *left, struct nod
78 78
 			return left;
79 79
 	}
80 80
 	n = cli_malloc(sizeof(*n));
81
-	if(!n)
81
+	if(!n) {
82
+        cli_errmsg("make_node: Unable to allocate memory for new node\n");
82 83
 		return NULL;
84
+    }
83 85
 	n->type = type;
84 86
 	n->parent = NULL;
85 87
 	n->u.children.left = left;
... ...
@@ -99,8 +101,10 @@ static struct node *dup_node(struct node *p)
99 99
 	if(!p)
100 100
 		return NULL;
101 101
 	d = cli_malloc(sizeof(*d));
102
-	if(!d)
102
+	if(!d) {
103
+        cli_errmsg("dup_node: Unable to allocate memory for duplicate node\n");
103 104
 		return NULL;
105
+    }
104 106
 	d->type = p->type;
105 107
 	d->parent = NULL;
106 108
 	switch(p->type) {
... ...
@@ -110,6 +114,7 @@ static struct node *dup_node(struct node *p)
110 110
 		case leaf_class:
111 111
 			d->u.leaf_class_bitmap = cli_malloc(32);
112 112
 			if(!d->u.leaf_class_bitmap) {
113
+                cli_errmsg("make_node: Unable to allocate memory for leaf class\n");
113 114
 				free(d);
114 115
 				return NULL;
115 116
 			}
... ...
@@ -132,8 +137,10 @@ static struct node *dup_node(struct node *p)
132 132
 static struct node *make_charclass(uint8_t *bitmap)
133 133
 {
134 134
 	struct node *v = cli_malloc(sizeof(*v));
135
-	if(!v)
135
+	if(!v) {
136
+        cli_errmsg("make_charclass: Unable to allocate memory for character class\n");
136 137
 		return NULL;
138
+    }
137 139
 	v->type = leaf_class;
138 140
 	v->parent = NULL;
139 141
 	v->u.leaf_class_bitmap = bitmap;
... ...
@@ -178,8 +185,10 @@ static uint8_t* parse_char_class(const char *pat, size_t *pos)
178 178
 	unsigned char range_start=0;
179 179
 	int hasprev = 0;
180 180
 	uint8_t* bitmap = cli_malloc(32);
181
-	if(!bitmap)
181
+	if(!bitmap) {
182
+        cli_errmsg("parse_char_class: Unable to allocate memory for bitmap\n");
182 183
 		return NULL;
184
+    }
183 185
 	if (pat[*pos]=='^') {
184 186
 		memset(bitmap,0xFF,32);/*match chars not in brackets*/
185 187
 		++*pos;
... ...
@@ -214,8 +214,10 @@ static int load_actions(table_t* t)
214 214
 static int rtf_object_begin(struct rtf_state* state,cli_ctx* ctx,const char* tmpdir)
215 215
 {
216 216
 	struct rtf_object_data* data = cli_malloc(sizeof(*data));
217
-	if(!data)
217
+	if(!data) {
218
+        cli_errmsg("rtf_object_begin: Unable to allocate memory for object data\n");
218 219
 		return CL_EMEM;
220
+    }
219 221
 	data->fd = -1;
220 222
 	data->partial = 0;
221 223
 	data->has_partial = 0;
... ...
@@ -329,6 +331,7 @@ static int rtf_object_process(struct rtf_state* state, const unsigned char* inpu
329 329
 							    else
330 330
 								    data->desc_name = cli_malloc(data->desc_len+1);
331 331
 							    if(!data->desc_name) {
332
+                                    cli_errmsg("rtf_object_process: Unable to allocate memory for data->desc_name\n");
332 333
 								    return CL_EMEM;
333 334
 							    }
334 335
 							    data->internal_state = WAIT_DESC;
... ...
@@ -524,8 +527,10 @@ int cli_scanrtf(cli_ctx *ctx)
524 524
 	stack.warned = 0;
525 525
 	stack.states = cli_malloc(stack.stack_size*sizeof(*stack.states));
526 526
 
527
-	if(!stack.states)
527
+	if(!stack.states) {
528
+        cli_errmsg("ScanRTF: Unable to allocate memory for stack states\n");
528 529
 		return CL_EMEM;
530
+    }
529 531
 
530 532
 	if(!(tempname = cli_gentemp(ctx->engine->tmpdir)))
531 533
 	    return CL_EMEM;
... ...
@@ -135,6 +135,7 @@ static int cli_scandir(const char *dirname, cli_ctx *ctx)
135 135
 		    fname = cli_malloc(strlen(dirname) + strlen(dent->d_name) + 2);
136 136
 		    if(!fname) {
137 137
 			closedir(dd);
138
+            cli_dbgmsg("cli_scandir: Unable to allocate memory for filename\n");
138 139
 			return CL_EMEM;
139 140
 		    }
140 141
 
... ...
@@ -1016,6 +1017,7 @@ static int cli_vba_scandir(const char *dirname, cli_ctx *ctx, struct uniq *U)
1016 1016
 		    /* build the full name */
1017 1017
 		    fullname = cli_malloc(strlen(dirname) + strlen(dent->d_name) + 2);
1018 1018
 		    if(!fullname) {
1019
+                cli_dbgmsg("cli_vba_scandir: Unable to allocate memory for fullname\n");
1019 1020
 			ret = CL_EMEM;
1020 1021
 			break;
1021 1022
 		    }
... ...
@@ -714,6 +714,7 @@ static int real_scansis9x(cli_ctx *ctx, const char *tmpd) {
714 714
 		}
715 715
 
716 716
 		if (!(dst=cli_malloc(uusize))) {
717
+            cli_dbgmsg("SIS: OOM\n");
717 718
 		  free(src);
718 719
 		  break;
719 720
 		}
... ...
@@ -167,8 +167,10 @@ int unspin(char *src, int ssize, struct cli_exe_section *sections, int sectcnt,
167 167
 
168 168
   cli_dbgmsg("in unspin\n");
169 169
 
170
-  if ((spinned = (char *) cli_malloc(sections[sectcnt].rsz)) == NULL )
170
+  if ((spinned = (char *) cli_malloc(sections[sectcnt].rsz)) == NULL ) {
171
+      cli_dbgmsg("spin: Unable to allocate memory for spinned\n");
171 172
     return 1;
173
+  }
172 174
 
173 175
   memcpy(spinned, src + sections[sectcnt].raw, sections[sectcnt].rsz); 
174 176
   ep = spinned + nep - sections[sectcnt].rva;
... ...
@@ -390,8 +392,10 @@ int unspin(char *src, int ssize, struct cli_exe_section *sections, int sectcnt,
390 390
   }
391 391
 
392 392
   cli_dbgmsg("spin: Compression bitmap is %x\n", bitmap);
393
-  if ( (sects= (char **) cli_malloc(sectcnt*sizeof(char *))) == NULL )
393
+  if ( (sects= (char **) cli_malloc(sectcnt*sizeof(char *))) == NULL ) {
394
+      cli_dbgmsg("spin: malloc(%d) failed\n", sectcnt*sizeof(char *));
394 395
     return 1;
396
+  }
395 397
 
396 398
   len = 0;
397 399
   for (j=0; j<sectcnt; j++) {
... ...
@@ -458,6 +462,7 @@ int unspin(char *src, int ssize, struct cli_exe_section *sections, int sectcnt,
458 458
 	}
459 459
       } else {
460 460
 	/* malloc failed but i'm too deep into this crap to quit without leaking more :( */
461
+          cli_dbgmsg("spin: memory allocation failed, continuing anyway\n");
461 462
 	blobsz+=sections[j].rsz;
462 463
       }
463 464
     } else {
... ...
@@ -323,8 +323,10 @@ char *cli_strtok(const char *line, int fieldno, const char *delim)
323 323
 	return NULL;
324 324
     }
325 325
     buffer = cli_malloc(j-i+1);
326
-    if(!buffer)
327
-	return NULL;
326
+    if(!buffer) {
327
+        cli_errmsg("cli_strtok: Unable to allocate memory for buffer\n");
328
+        return NULL;
329
+    }
328 330
     strncpy(buffer, line+i, j-i);
329 331
     buffer[j-i] = '\0';
330 332
 
... ...
@@ -497,8 +499,10 @@ char *cli_unescape(const char *str)
497 497
 	/* unescaped string is at most as long as original,
498 498
 	 * it will usually be shorter */
499 499
 	R = cli_malloc(len + 1);
500
-	if(!R)
500
+	if(!R) {
501
+        cli_errmsg("cli_unescape: Unable to allocate memory for string\n");
501 502
 		return NULL;
503
+    }
502 504
 	for(k=0;k < len;k++) {
503 505
 		unsigned char c = str[k];
504 506
 		if (str[k] == '%') {
... ...
@@ -103,8 +103,10 @@ tableInsert(table_t *table, const char *key, int value)
103 103
 			(tableEntry *)cli_malloc(sizeof(tableEntry));
104 104
 	}
105 105
 
106
-	if(table->tableLast == NULL)
106
+	if(table->tableLast == NULL) {
107
+        cli_dbgmsg("tableInsert: Unable to allocate memory for table\n");
107 108
 		return -1;
109
+    }
108 110
 
109 111
 	table->tableLast->next = NULL;
110 112
 	table->tableLast->key = cli_strdup(key);
... ...
@@ -146,6 +146,7 @@ textCopy(const text *t_head)
146 146
 		}
147 147
 
148 148
 		if(last == NULL) {
149
+            cli_errmsg("textCopy: Unable to allocate memory to clone object\n");
149 150
 			if(first)
150 151
 				textDestroy(first);
151 152
 			return NULL;
... ...
@@ -247,8 +248,10 @@ textMove(text *t_head, text *t)
247 247
 			return NULL;
248 248
 		}
249 249
 		t_head = (text *)cli_malloc(sizeof(text));
250
-		if(t_head == NULL)
250
+		if(t_head == NULL) {
251
+            cli_errmsg("textMove: Unable to allocate memory for head\n");
251 252
 			return NULL;
253
+        }
252 254
 		t_head->t_line = t->t_line;
253 255
 		t_head->t_next = t->t_next;
254 256
 		t->t_line = NULL;
... ...
@@ -269,8 +272,10 @@ textMove(text *t_head, text *t)
269 269
 	 * empty, the rest is moved by a simple pointer reassignment
270 270
 	 */
271 271
 	t_head->t_next = (text *)cli_malloc(sizeof(text));
272
-	if(t_head->t_next == NULL)
272
+	if(t_head->t_next == NULL) {
273
+        cli_errmsg("textMove: Unable to allocate memory for head->next\n");
273 274
 		return NULL;
275
+    }
274 276
 	t_head = t_head->t_next;
275 277
 
276 278
 	assert(t_head != NULL);
... ...
@@ -248,8 +248,10 @@ tnef_message(fmap_t *map, off_t *pos, uint16_t type, uint16_t tag, int32_t lengt
248 248
 			if(length <= 0)
249 249
 				return -1;
250 250
 			string = cli_malloc(length + 1);
251
-			if(string == NULL)
251
+			if(string == NULL) {
252
+                cli_errmsg("tnef_message: Unable to allocate memory for string\n");
252 253
 				return -1;
254
+            }
253 255
 			if(fmap_readn(map, string, *pos, (uint32_t)length) != (uint32_t)length) {
254 256
 				free(string);
255 257
 				return -1;
... ...
@@ -297,8 +299,10 @@ tnef_attachment(fmap_t *map, off_t *pos, uint16_t type, uint16_t tag, int32_t le
297 297
 			if(length <= 0)
298 298
 				return -1;
299 299
 			string = cli_malloc(length + 1);
300
-			if(string == NULL)
300
+			if(string == NULL) {
301
+                cli_errmsg("tnef_attachment: Unable to allocate memory for string\n");
301 302
 				return -1;
303
+            }
302 304
 			if(fmap_readn(map, string, *pos, (uint32_t)length) != (uint32_t)length) {
303 305
 				free(string);
304 306
 				return -1;
... ...
@@ -849,13 +849,17 @@ static int arj_read_main_header(arj_metadata_t *metadata)
849 849
 	}
850 850
 
851 851
 	filename = fmap_need_offstr(metadata->map, metadata->offset, header_size);
852
-	if (!filename)
852
+	if (!filename) {
853
+        cli_dbgmsg("UNARJ: Unable to allocate memory for filename\n");
853 854
 		return FALSE;
855
+    }
854 856
 	metadata->offset += strlen(filename) + 1;
855 857
 
856 858
 	comment = fmap_need_offstr(metadata->map, metadata->offset, header_size);
857
-	if (!comment)
859
+	if (!comment) {
860
+        cli_dbgmsg("UNARJ: Unable to allocate memory for comment\n");
858 861
 		return FALSE;
862
+    }
859 863
 	metadata->offset += strlen(comment) + 1;
860 864
 	cli_dbgmsg("Filename: %s\n", filename);
861 865
 	cli_dbgmsg("Comment: %s\n", comment);
... ...
@@ -929,13 +933,17 @@ static int arj_read_file_header(arj_metadata_t *metadata)
929 929
 	}
930 930
 
931 931
 	filename = fmap_need_offstr(metadata->map, metadata->offset, header_size);
932
-	if (!filename)
932
+	if (!filename) {
933
+        cli_dbgmsg("UNARJ: Unable to allocate memory for filename\n");
933 934
 		return FALSE;
935
+    }
934 936
 	metadata->offset += strlen(filename) + 1;
935 937
 
936 938
 	comment = fmap_need_offstr(metadata->map, metadata->offset, header_size);
937
-	if (!comment)
939
+	if (!comment) {
940
+        cli_dbgmsg("UNARJ: Unable to allocate memory for comment\n");
938 941
 		return FALSE;
942
+    }
939 943
 	metadata->offset += strlen(comment) + 1;
940 944
 	cli_dbgmsg("Filename: %s\n", filename);
941 945
 	cli_dbgmsg("Comment: %s\n", comment);
... ...
@@ -149,7 +149,10 @@ uint32_t unspack(const char *start_of_stuff, char *dest, cli_ctx *ctx, uint32_t
149 149
     return 1; /* Should be ~15KB, if it's so big it's prolly just not nspacked */
150 150
     
151 151
   cli_dbgmsg("unsp: table size = %d\n", tablesz);
152
-  if (!(table = cli_malloc(tablesz))) return 1;
152
+  if (!(table = cli_malloc(tablesz))) {
153
+      cli_dbgmsg("unspack: Unable to allocate memory for table\n");
154
+      return 1;
155
+  }
153 156
   
154 157
   dsize = cli_readint32(start_of_stuff+9);
155 158
   ssize = cli_readint32(start_of_stuff+5);
... ...
@@ -104,8 +104,10 @@ get_unicode_name(const char *name, int size, int big_endian)
104 104
 		return NULL;
105 105
 
106 106
 	newname = (char *)cli_malloc(size * 7 + 1);
107
-	if(newname == NULL)
107
+	if(newname == NULL) {
108
+        cli_errmsg("get_unicode_name: Unable to allocate memory for newname\n");
108 109
 		return NULL;
110
+    }
109 111
 
110 112
 	if((!big_endian) && (size & 0x1)) {
111 113
 		cli_dbgmsg("get_unicode_name: odd number of bytes %d\n", size);
... ...
@@ -897,8 +899,10 @@ word_read_macro_entry(int fd, macro_info_t *macro_info)
897 897
 
898 898
 	msize = count * sizeof(struct macro);
899 899
 	m = cli_malloc(msize);
900
-	if(m == NULL)
900
+	if(m == NULL) {
901
+        cli_errmsg("word_read_macro_entry: Unable to allocate memory for 'm'\n");
901 902
 		return FALSE;
903
+    }
902 904
 
903 905
 	if(cli_readn(fd, m, msize) != msize) {
904 906
 		free(m);
... ...
@@ -932,6 +936,7 @@ word_read_macro_info(int fd, macro_info_t *macro_info)
932 932
 	macro_info->entries = (macro_entry_t *)cli_malloc(sizeof(macro_entry_t) * macro_info->count);
933 933
 	if(macro_info->entries == NULL) {
934 934
 		macro_info->count = 0;
935
+        cli_errmsg("word_read_macro_info: Unable to allocate memory for macro_info->entries\n");
935 936
 		return NULL;
936 937
 	}
937 938
 	if(!word_read_macro_entry(fd, macro_info)) {
... ...
@@ -1166,6 +1171,7 @@ cli_wm_readdir(int fd)
1166 1166
 				m++;
1167 1167
 			}
1168 1168
 		} else {
1169
+            cli_errmsg("cli_wm_readdir: Unable to allocate memory for vba_project\n");
1169 1170
 			free(vba_project->name);
1170 1171
 			free(vba_project->colls);
1171 1172
 			free(vba_project->dir);
... ...
@@ -1195,8 +1201,10 @@ cli_wm_decrypt_macro(int fd, off_t offset, uint32_t len, unsigned char key)
1195 1195
 		return NULL;
1196 1196
 
1197 1197
 	buff = (unsigned char *)cli_malloc(len);
1198
-	if(buff == NULL)
1198
+	if(buff == NULL) {
1199
+        cli_errmsg("cli_wm_decrypt_macro: Unable to allocate memory for buff\n");
1199 1200
 		return NULL;
1201
+    }
1200 1202
 
1201 1203
 	if(!seekandread(fd, offset, SEEK_SET, buff, len)) {
1202 1204
 		free(buff);
... ...
@@ -1284,8 +1292,10 @@ create_vba_project(int record_count, const char *dir, struct uniq *U)
1284 1284
 
1285 1285
 	ret = (vba_project_t *) cli_malloc(sizeof(struct vba_project_tag));
1286 1286
 
1287
-	if(ret == NULL)
1287
+	if(ret == NULL) {
1288
+        cli_errmsg("create_vba_project: Unable to allocate memory for vba project structure\n");
1288 1289
 		return NULL;
1290
+    }
1289 1291
 
1290 1292
 	ret->name = (char **)cli_malloc(sizeof(char *) * record_count);
1291 1293
 	ret->colls = (uint32_t *)cli_malloc(sizeof(uint32_t) * record_count);
... ...
@@ -1302,6 +1312,7 @@ create_vba_project(int record_count, const char *dir, struct uniq *U)
1302 1302
 		if(ret->offset)
1303 1303
 			free(ret->offset);
1304 1304
 		free(ret);
1305
+        cli_errmsg("create_vba_project: Unable to allocate memory for vba project elements\n");
1305 1306
 		return NULL;
1306 1307
 	}
1307 1308
 	ret->count = record_count;
... ...
@@ -94,7 +94,10 @@ int wwunpack(uint8_t *exe, uint32_t exesz, uint8_t *wwsect, struct cli_exe_secti
94 94
       break;
95 95
     }
96 96
     cli_dbgmsg("WWP: src: %x, szd: %x, srcend: %x - %x\n", src, szd, srcend, srcend+4-szd);
97
-    if (!(compd = cli_malloc(szd))) break;
97
+    if (!(compd = cli_malloc(szd))) {
98
+        cli_dbgmsg("WWPack: Unable to allocate memory for compd\n");
99
+        break;
100
+    }
98 101
     memcpy(compd, unpd, szd);
99 102
     memset(unpd, -1, szd); /*FIXME*/
100 103
     ccur=compd;