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
  • Loading branch information
catenacyber authored and victorjulien committed Oct 18, 2023
1 parent 986a441 commit 737bc4f
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
Expand Up @@ -384,7 +384,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 @@ -394,12 +394,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
Expand Up @@ -144,6 +144,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 737bc4f

Please sign in to comment.