Browse code

dtrie: sanity checks

Segfaults reported by Igor, on sr-Users mailing list.

(cherry picked from commit 825b4fabb9bb4b2cf58dc1205423b8ce5d5e08cc)

Stefan Mititelu authored on 22/01/2016 12:11:25
Showing 1 changed files
... ...
@@ -76,7 +76,9 @@ void dtrie_delete(struct dtrie_node_t *root, struct dtrie_node_t *node,
76 76
 		dt_delete_func_t delete_payload, const unsigned int branches)
77 77
 {
78 78
 	unsigned int i;
79
-	if (node==NULL) return;
79
+
80
+	if (node == NULL) return;
81
+	if (root == NULL) return;
80 82
 
81 83
 	for (i=0; i<branches; i++) {
82 84
 		dtrie_delete(root, node->child[i], delete_payload, branches);
... ...
@@ -123,6 +125,10 @@ int dtrie_insert(struct dtrie_node_t *root, const char *number, const unsigned i
123 125
 	struct dtrie_node_t *node = root;
124 126
 	unsigned char digit, i=0;
125 127
 
128
+	if (node == NULL) return -1;
129
+	if (root == NULL) return -1;
130
+	if (number == NULL) return -1;
131
+
126 132
 	while (i<numberlen) {
127 133
 		if (branches==10) {
128 134
 			digit = number[i] - '0';
... ...
@@ -202,6 +208,8 @@ unsigned int dtrie_leaves(const struct dtrie_node_t *root, const unsigned int br
202 208
 {
203 209
 	unsigned int i, sum = 0, leaf = 1;
204 210
 
211
+	if (root == NULL) return 0;
212
+
205 213
 	for (i=0; i<branches; i++) {
206 214
 		if (root->child[i]) {
207 215
 			sum += dtrie_leaves(root->child[i], branches);
... ...
@@ -220,6 +228,10 @@ void **dtrie_longest_match(struct dtrie_node_t *root, const char *number,
220 228
 	unsigned char digit, i = 0;
221 229
 	void **ret = NULL;
222 230
 
231
+	if (node == NULL) return NULL;
232
+	if (root == NULL) return NULL;
233
+	if (number == NULL) return NULL;
234
+
223 235
 	if (nmatchptr) *nmatchptr=-1;
224 236
 	if (node->data != NULL) {
225 237
 		if (nmatchptr) *nmatchptr=0;