Browse code

fix segfault on OOM (bb#515)

git-svn: trunk@3067

Tomasz Kojm authored on 2007/05/26 23:08:18
Showing 2 changed files
... ...
@@ -1,3 +1,7 @@
1
+Sat May 26 14:05:44 CEST 2007 (tk)
2
+----------------------------------
3
+  * libclamav/matcher-ac.c: fix segfault on OOM (bb#515)
4
+
1 5
 Sat May 26 11:34:19 BST 2007 (njh)
2 6
 ----------------------------------
3 7
   * libclamav/pdf.c:	Fix OOM, pointed out by TK
... ...
@@ -38,7 +38,7 @@
38 38
 
39 39
 int cli_ac_addpatt(struct cli_matcher *root, struct cli_ac_patt *pattern)
40 40
 {
41
-	struct cli_ac_node *pt, *next;
41
+	struct cli_ac_node *pt, *next, **newtable;
42 42
 	uint8_t i;
43 43
 	uint16_t len = MIN(root->ac_maxdepth, pattern->length);
44 44
 
... ...
@@ -85,15 +85,17 @@ int cli_ac_addpatt(struct cli_matcher *root, struct cli_ac_patt *pattern)
85 85
 	    }
86 86
 
87 87
 	    root->ac_nodes++;
88
-	    root->ac_nodetable = (struct cli_ac_node **) cli_realloc2(root->ac_nodetable, root->ac_nodes * sizeof(struct cli_ac_node *));
89
-	    if(!root->ac_nodetable) {
88
+	    newtable = (struct cli_ac_node **) cli_realloc(root->ac_nodetable, root->ac_nodes * sizeof(struct cli_ac_node *));
89
+	    if(!newtable) {
90
+		root->ac_nodes--;
90 91
 		cli_errmsg("cli_ac_addpatt: Can't realloc ac_nodetable\n");
91 92
 		if(next->trans)
92 93
 		    free(next->trans);
93 94
 		free(next);
94 95
 		return CL_EMEM;
95 96
 	    }
96
-	    root->ac_nodetable[root->ac_nodes - 1] = next;
97
+	    newtable[root->ac_nodes - 1] = next;
98
+	    root->ac_nodetable = newtable;
97 99
 
98 100
 	    pt->trans[(unsigned char) (pattern->pattern[i] & 0xff)] = next;
99 101
 	    pt->leaf = 0;