Forráskód Böngészése

Merge pull request #483 from smititelu/master

dtrie: sanity checks
Stefan Mititelu 9 éve
szülő
commit
5efaf2292c
1 módosított fájl, 13 hozzáadás és 1 törlés
  1. 13 1
      lib/trie/dtrie.c

+ 13 - 1
lib/trie/dtrie.c

@@ -76,7 +76,9 @@ void dtrie_delete(struct dtrie_node_t *root, struct dtrie_node_t *node,
 		dt_delete_func_t delete_payload, const unsigned int branches)
 {
 	unsigned int i;
-	if (node==NULL) return;
+
+	if (node == NULL) return;
+	if (root == NULL) return;
 
 	for (i=0; i<branches; i++) {
 		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
 	struct dtrie_node_t *node = root;
 	unsigned char digit, i=0;
 
+	if (node == NULL) return -1;
+	if (root == NULL) return -1;
+	if (number == NULL) return -1;
+
 	while (i<numberlen) {
 		if (branches==10) {
 			digit = number[i] - '0';
@@ -202,6 +208,8 @@ unsigned int dtrie_leaves(const struct dtrie_node_t *root, const unsigned int br
 {
 	unsigned int i, sum = 0, leaf = 1;
 
+	if (root == NULL) return 0;
+
 	for (i=0; i<branches; i++) {
 		if (root->child[i]) {
 			sum += dtrie_leaves(root->child[i], branches);
@@ -220,6 +228,10 @@ void **dtrie_longest_match(struct dtrie_node_t *root, const char *number,
 	unsigned char digit, i = 0;
 	void **ret = NULL;
 
+	if (node == NULL) return NULL;
+	if (root == NULL) return NULL;
+	if (number == NULL) return NULL;
+
 	if (nmatchptr) *nmatchptr=-1;
 	if (node->data != NULL) {
 		if (nmatchptr) *nmatchptr=0;