Browse code

trie API Fixed dtrie_insert OOM handling, where assert() was wrongfully used

Now the function behaves as expected, freeing previous allocated resources, logging the OOM condition , and returning -1
(cherry picked from commit 49837c37aea72c1e4f0cc3290382c0adbf72e3f8)

Marius Zbihlei authored on 02/11/2010 09:45:15
Showing 1 changed files
... ...
@@ -39,8 +39,6 @@
39 39
 
40 40
 #include "dtrie.h"
41 41
 
42
-#include <assert.h>
43
-
44 42
 #include "../../dprint.h"
45 43
 #include "../../mem/shm_mem.h"
46 44
 #include "../../mem/mem.h"
... ...
@@ -142,12 +140,20 @@ int dtrie_insert(struct dtrie_node_t *root, const char *number, const unsigned i
142 140
 
143 141
 		if (node->child[digit] == NULL) {
144 142
 			node->child[digit] = shm_malloc(sizeof(struct dtrie_node_t));
145
-			assert(node->child[digit] != NULL);
143
+			if(node->child[digit] == NULL ){
144
+				SHM_MEM_ERROR;
145
+				return -1;
146
+			}
147
+
146 148
 			LM_DBG("allocate %lu bytes for node at %p\n", (long unsigned)sizeof(struct dtrie_node_t), node->child[digit]);
147 149
 			memset(node->child[digit], 0, sizeof(struct dtrie_node_t));
148 150
 
149 151
 			node->child[digit]->child = shm_malloc(sizeof(struct dtrie_node_t *) * branches);
150
-			assert(node->child[digit]->child != NULL);
152
+			if(node->child[digit]->child == NULL){
153
+				SHM_MEM_ERROR;
154
+				shm_free(node->child[digit]);
155
+				return -1;
156
+			}
151 157
 			LM_DBG("allocate %lu bytes for %d root children pointer at %p\n",
152 158
 					(long unsigned)sizeof(struct dtrie_node_t *) * branches,
153 159
 					branches, node->child[digit]->child);