Add protection against lookup in not compiled trie (and vice-versa).
[apps/pfixtools.git] / common / trie.c
index 947a817..9a04cf4 100644 (file)
@@ -199,7 +199,6 @@ static inline void trie_entry_insert_child(trie_t *trie, trie_entry_t *entry,
         entry->children_len    = 1;
     } else {
         if (entry->children_offset + entry->children_len != pchild) {
-            trie_inspect(trie);
             printf("Inserting child %d while offset is %d[%d]\n",
                    pchild, entry->children_offset, entry->children_len);
             abort();
@@ -230,6 +229,7 @@ static inline void trie_entry_split(trie_t *trie, trie_entry_t *entry, int pos)
 
 void trie_insert(trie_t *trie, const char* key)
 {
+    assert(trie->entries == NULL && "Trie already compiled");
     GROW(trie->keys, 1, trie->keys_len, trie->keys_size);
     trie->keys[trie->keys_len++] = strdup(key);
 }
@@ -258,6 +258,9 @@ static inline void trie_compile_aux(trie_t *trie, int id,
                 current = trie->keys[i][offset];
             }
         }
+        if (fork_pos == 0 && current == '\0') {
+            return;
+        }
     }
     forks[fork_pos] = last_key;
 
@@ -296,6 +299,8 @@ typedef char *str_t;
 
 void trie_compile(trie_t *trie, bool memlock)
 {
+    assert(trie->entries == NULL && "Trie already compiled");
+    assert(trie->keys != NULL && "Trying to compile an empty trie");
     {
 #       define QSORT_TYPE str_t
 #       define QSORT_BASE trie->keys
@@ -316,6 +321,7 @@ void trie_compile(trie_t *trie, bool memlock)
 
 bool trie_lookup(const trie_t *trie, const char *key)
 {
+    assert(trie->keys == NULL && "Can't lookup: trie not compiled");
     if (trie->entries_len == 0) {
         return false;
     } else {
@@ -341,7 +347,7 @@ bool trie_lookup(const trie_t *trie, const char *key)
 /* Debug {{{1
  */
 
-static inline void trie_entry_inspect(const trie_t *trie,
+static inline void trie_entry_inspect(const trie_t *trie, bool show_content,
                                       const trie_entry_t *entry, int level)
 {
     static int max_depth = 0;
@@ -355,26 +361,28 @@ static inline void trie_entry_inspect(const trie_t *trie,
         ++leaves;
         depth_sum += level;
     }
-    for (int i = 0 ; i < level ; ++i) {
-        fputs("  ", stdout);
-    }
-    if (entry->c_len == 0) {
-        fputs("(nil)", stdout);
-    } else {
-        const char *c = trie->c + entry->c_offset;
-        printf("(%d) ", entry->c_len);
-        for (int i = 0 ; i < entry->c_len ; ++i) {
-            if (c[i]) {
-                printf("%c ", c[i]);
-            } else {
-                fputs("\\0 ", stdout);
+    if (show_content) {
+        for (int i = 0 ; i < level ; ++i) {
+            fputs("  ", stdout);
+        }
+        if (entry->c_len == 0) {
+            fputs("(nil)", stdout);
+        } else {
+            const char *c = trie->c + entry->c_offset;
+            printf("(%d) ", entry->c_len);
+            for (int i = 0 ; i < entry->c_len ; ++i) {
+                if (c[i]) {
+                    printf("%c ", c[i]);
+                } else {
+                    fputs("\\0 ", stdout);
+                }
             }
         }
+        fputs("\n", stdout);
     }
-    fputs("\n", stdout);
     for (int i = entry->children_offset ;
           i < entry->children_offset + entry->children_len ; ++i) {
-        trie_entry_inspect(trie, &trie->entries[i], level + 1);
+        trie_entry_inspect(trie, show_content, &trie->entries[i], level + 1);
     }
     if (level == 0) {
         printf("Average char per node: %d\n", trie->c_len / trie->entries_len);
@@ -387,7 +395,7 @@ static inline void trie_entry_inspect(const trie_t *trie,
     }
 }
 
-void trie_inspect(const trie_t *trie)
+void trie_inspect(const trie_t *trie, bool show_content)
 {
-    trie_entry_inspect(trie, trie->entries, 0);
+    trie_entry_inspect(trie, show_content, trie->entries, 0);
 }