]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
conf: avoid quadratic complexity 10865/head
authorPhilippe Antoine <pantoine@oisf.net>
Thu, 21 Mar 2024 08:38:25 +0000 (09:38 +0100)
committerVictor Julien <victor@inliniac.net>
Tue, 16 Apr 2024 17:42:15 +0000 (19:42 +0200)
Ticket: 6878

Follow up on 15649424a76d01eb332d85620ffc4956d4f3d9be

When adding many sequence nodes, either from start or scalar event

We add "sequence nodes" whose name is an integer cf sequence_node_name
and then run ConfNodeLookupChild to see if it had been already set
(from the command line cf comment in the code)
And ConfNodeLookupChild iterates the whole linked list...

1. We add node 1
2. To add node 2, we check if node 1 equals this new node
3. To add node 3, we check if nodes 1, or 2 equals this new node's name
And so on...

This commits avoids these checks ig the list is empty at the beginning

src/conf-yaml-loader.c

index ea64563609377c21a4527a2e90fca03010e9b4d0..463eb2e5823ccb000c8f4ea88f75885599f4a7a5 100644 (file)
@@ -394,8 +394,19 @@ static int ConfYamlParse(yaml_parser_t *parser, ConfNode *parent, int inseq, int
             if (inseq) {
                 char sequence_node_name[DEFAULT_NAME_LEN];
                 snprintf(sequence_node_name, DEFAULT_NAME_LEN, "%d", seq_idx++);
-                ConfNode *seq_node = ConfNodeLookupChild(node,
-                    sequence_node_name);
+                ConfNode *seq_node = NULL;
+                if (was_empty < 0) {
+                    // initialize was_empty
+                    if (TAILQ_EMPTY(&node->head)) {
+                        was_empty = 1;
+                    } else {
+                        was_empty = 0;
+                    }
+                }
+                // we only check if the node's list was not empty at first
+                if (was_empty == 0) {
+                    seq_node = ConfNodeLookupChild(node, sequence_node_name);
+                }
                 if (seq_node != NULL) {
                     /* The sequence node has already been set, probably
                      * from the command line.  Remove it so it gets