]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
mime: avoid quadratic complexity in MimeDecAddEntity
authorPhilippe Antoine <pantoine@oisf.net>
Mon, 11 Sep 2023 14:49:48 +0000 (16:49 +0200)
committerVictor Julien <vjulien@oisf.net>
Wed, 18 Oct 2023 13:26:29 +0000 (15:26 +0200)
Ticket: #6306

Keep a reference to last child, consume a bit more RAM to save CPU

(cherry picked from commit 737bc4f219ea36b4da4ffbebef15b0619dffbca1)

src/util-decode-mime.c
src/util-decode-mime.h

index 50f12e0f1f5851ef6544ced9e0a334d515001de6..69065f279452faf1b1f89d1a2a42005a360b5f8b 100644 (file)
@@ -390,7 +390,7 @@ static MimeDecUrl * MimeDecAddUrl(MimeDecEntity *entity, uint8_t *url, uint32_t
  */
 MimeDecEntity * MimeDecAddEntity(MimeDecEntity *parent)
 {
-    MimeDecEntity *curr, *node = SCMalloc(sizeof(MimeDecEntity));
+    MimeDecEntity *node = SCMalloc(sizeof(MimeDecEntity));
     if (unlikely(node == NULL)) {
         return NULL;
     }
@@ -400,12 +400,10 @@ MimeDecEntity * MimeDecAddEntity(MimeDecEntity *parent)
     if (parent != NULL) {
         if (parent->child == NULL) {
             parent->child = node;
+            parent->last_child = node;
         } else {
-            curr = parent->child;
-            while (curr->next != NULL) {
-                curr = curr->next;
-            }
-            curr->next = node;
+            parent->last_child->next = node;
+            parent->last_child = node;
         }
     }
 
index 021a79a14afdc17bc578d572754a264883f3fb59..abbbe8cd43311fc852ba93b870220ceb7d9aff82 100644 (file)
@@ -145,6 +145,7 @@ typedef struct MimeDecEntity {
     uint8_t *msg_id;  /**< Quick access pointer to message Id */
     struct MimeDecEntity *next;  /**< Pointer to list of sibling entities */
     struct MimeDecEntity *child;  /**< Pointer to list of child entities */
+    struct MimeDecEntity *last_child; /**< Pointer to tail of the list of child entities */
 } MimeDecEntity;
 
 /**