]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
conf: avoid quadratic complexity
authorPhilippe Antoine <contact@catenacyber.fr>
Mon, 8 Nov 2021 13:18:30 +0000 (14:18 +0100)
committerShivani Bhardwaj <shivanib134@gmail.com>
Thu, 13 Jan 2022 16:44:10 +0000 (22:14 +0530)
Ticket: 4812

When adding many sequence nodes

(cherry picked from commit 15649424a76d01eb332d85620ffc4956d4f3d9be)

src/conf-yaml-loader.c

index d6bb378963ddd8b8fa74381f53fa17b732b500d7..5a32169b0f9f9d2a862bf7d427a3f686040a9692 100644 (file)
@@ -181,6 +181,7 @@ ConfYamlParse(yaml_parser_t *parser, ConfNode *parent, int inseq, int rlevel)
     int state = 0;
     int seq_idx = 0;
     int retval = 0;
+    int was_empty = -1;
 
     if (rlevel++ > RECURSION_LIMIT) {
         SCLogError(SC_ERR_CONF_YAML_ERROR, "Recursion limit reached while parsing "
@@ -233,8 +234,19 @@ ConfYamlParse(yaml_parser_t *parser, ConfNode *parent, int inseq, int rlevel)
             if (inseq) {
                 char sequence_node_name[DEFAULT_NAME_LEN];
                 snprintf(sequence_node_name, DEFAULT_NAME_LEN, "%d", seq_idx++);
-                ConfNode *seq_node = ConfNodeLookupChild(parent,
-                    sequence_node_name);
+                ConfNode *seq_node = NULL;
+                if (was_empty < 0) {
+                    // initialize was_empty
+                    if (TAILQ_EMPTY(&parent->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(parent, sequence_node_name);
+                }
                 if (seq_node != NULL) {
                     /* The sequence node has already been set, probably
                      * from the command line.  Remove it so it gets