]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
default to inserting at the end for add_edge
authorAlan T. DeKok <aland@freeradius.org>
Sat, 23 Mar 2019 18:18:37 +0000 (14:18 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Sat, 23 Mar 2019 18:27:43 +0000 (14:27 -0400)
src/lib/util/trie.c

index e1a1acd281d6715bfca738706fcaa8885487b260..9b1f1dfafa38aaffe1779e06a58f1e3b9f8f2b5e 100644 (file)
@@ -1027,7 +1027,7 @@ static int fr_trie_add_edge(fr_trie_t *trie, uint16_t chunk, fr_trie_t *child)
 
                if (comp->used >= 4) return -1;
 
-               edge = 0;
+               edge = comp->used;
                for (i = 0; i < comp->used; i++) {
                        if (comp->index[i] < chunk) continue;
 
@@ -1043,15 +1043,16 @@ static int fr_trie_add_edge(fr_trie_t *trie, uint16_t chunk, fr_trie_t *child)
                 *      Move the nodes up so that we have room for the
                 *      new edge.
                 */
-               for (i = comp->used; i > edge; i--) {
-                       comp->index[i] = comp->index[i - 1];
-                       comp->trie[i] = comp->trie[i - 1];
+               for (i = edge; i < comp->used; i++) {
+                       comp->index[i + 1] = comp->index[i];
+                       comp->trie[i + 1] = comp->trie[i];
                }
 
                comp->index[edge] = chunk;
                comp->trie[edge] = child;
 
                comp->used++;
+               VERIFY(comp);
                return 0;
        }
 #endif
@@ -1149,8 +1150,7 @@ static void *fr_trie_comp_match(fr_trie_t *trie, uint8_t const *key, int start_b
        chunk = get_chunk(key, start_bit, comp->bits);
 
        for (i = 0; i < comp->used; i++) {
-               MPRINT3("%.*scomp[%d] = index %04x ? chunk %04x\n",
-                       start_bit, spaces, i, comp->index[i], chunk);
+               if (comp->index[i] < chunk) continue;
 
                if (comp->index[i] == chunk) {
                        return fr_trie_key_match(comp->trie[i], key, start_bit + comp->bits, end_bit, exact);
@@ -1161,7 +1161,7 @@ static void *fr_trie_comp_match(fr_trie_t *trie, uint8_t const *key, int start_b
                 *      if the edge is larger than the chunk, NO edge
                 *      will match the chunk.
                 */
-               if (comp->index[i] > chunk) return NULL;
+               return NULL;
        }
 
        return NULL;