Skip to content

Commit

Permalink
mime: avoid quadratic complexity in MimeDecAddEntity
Browse files Browse the repository at this point in the history
Ticket: #6306

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

(cherry picked from commit 737bc4f)
  • Loading branch information
catenacyber authored and victorjulien committed Oct 18, 2023
1 parent cc53447 commit 62b3bb0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/util-decode-mime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/util-decode-mime.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down

0 comments on commit 62b3bb0

Please sign in to comment.