]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
util/mpm: grow state queue on demand 13489/head 13490/head
authorPhilippe Antoine <pantoine@oisf.net>
Tue, 17 Jun 2025 13:06:27 +0000 (15:06 +0200)
committerPhilippe Antoine <pantoine@oisf.net>
Thu, 19 Jun 2025 12:50:20 +0000 (14:50 +0200)
Ticket: 7678
(cherry picked from commit 9f83662f2035eb1090e804acf8995b17d08bf1d1)

src/util-mpm-ac-queue.c
src/util-mpm-ac-queue.h

index 40bb951a03a94e930ed08af51e76c43939ffbdd9..6f2a146d6372826195bd1548a16d0730afbfe672 100644 (file)
@@ -25,10 +25,16 @@ StateQueue *SCACStateQueueAlloc(void)
     if (q == NULL) {
         FatalError("Error allocating memory");
     }
+    q->store = SCCalloc(STATE_QUEUE_CONTAINER_SIZE, sizeof(int32_t));
+    if (q->store == NULL) {
+        FatalError("Error allocating memory");
+    }
+    q->size = STATE_QUEUE_CONTAINER_SIZE;
     return q;
 }
 
 void SCACStateQueueFree(StateQueue *q)
 {
+    SCFree(q->store);
     SCFree(q);
 }
index 3bf8b41f9abdc83cab76136190349ed16aae3ac0..4fe970e542e7ad2553ee70ffd693a3f63a410b39 100644 (file)
  * \brief Helper structure used by AC during state table creation
  */
 typedef struct StateQueue_ {
-    int32_t store[STATE_QUEUE_CONTAINER_SIZE];
-    int top;
-    int bot;
+    uint32_t top;
+    uint32_t bot;
+    uint32_t size;
+    int32_t *store;
 } StateQueue;
 
 StateQueue *SCACStateQueueAlloc(void);
@@ -49,28 +50,36 @@ static inline int SCACStateQueueIsEmpty(StateQueue *q)
 
 static inline void SCACEnqueue(StateQueue *q, int32_t state)
 {
-    int i = 0;
-
     /*if we already have this */
-    for (i = q->bot; i < q->top; i++) {
+    for (uint32_t i = q->bot; i < q->top; i++) {
         if (q->store[i] == state)
             return;
     }
 
     q->store[q->top++] = state;
 
-    if (q->top == STATE_QUEUE_CONTAINER_SIZE)
+    if (q->top == q->size)
         q->top = 0;
 
     if (q->top == q->bot) {
-        FatalError("Just ran out of space in the queue.  "
-                   "Fatal Error.  Exiting.  Please file a bug report on this");
+        // allocate a new store and copy + realign
+        int32_t *tmp = SCCalloc(q->size + STATE_QUEUE_CONTAINER_SIZE, sizeof(int32_t));
+        if (tmp == NULL) {
+            FatalError("Error reallocating memory");
+        }
+        memcpy(tmp, q->store + q->bot, (q->size - q->bot) * sizeof(int32_t));
+        memcpy(tmp + (q->size - q->bot), q->store, q->top * sizeof(int32_t));
+        SCFree(q->store);
+        q->store = tmp;
+        q->bot = 0;
+        q->top = q->size;
+        q->size += STATE_QUEUE_CONTAINER_SIZE;
     }
 }
 
 static inline int32_t SCACDequeue(StateQueue *q)
 {
-    if (q->bot == STATE_QUEUE_CONTAINER_SIZE)
+    if (q->bot == q->size)
         q->bot = 0;
 
     if (q->bot == q->top) {