]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect: improve memory handling & comments
authorVictor Julien <victor@inliniac.net>
Sun, 16 Oct 2016 19:29:34 +0000 (21:29 +0200)
committerVictor Julien <victor@inliniac.net>
Thu, 16 Feb 2017 09:35:37 +0000 (10:35 +0100)
src/detect-engine.c
src/detect-parse.c
src/detect.c

index 002e69c1a366b5d4d6b42095318c93c190e6a93c..d6823fc14b061f3f3dd2ec3b1eaffacc47d5e200 100644 (file)
@@ -148,7 +148,7 @@ int DetectEngineAppInspectionEngine2Signature(Signature *s)
 
     DetectEngineAppInspectionEngine *t = g_app_inspect_engines;
     while (t != NULL) {
-        if (s->init_data->smlists[t->sm_list] == NULL)
+        if (s->sm_arrays[t->sm_list] == NULL)
             goto next;
         if (t->alproto == ALPROTO_UNKNOWN) {
             /* special case, inspect engine applies to all protocols */
@@ -234,6 +234,8 @@ next:
         t = t->next;
     }
 
+    /* clear s->sm_arrays for those lists that we put
+     * in the inspect engines. They own it now. */
     int i;
     for (i = 0; i < DETECT_SM_LIST_MAX; i++) {
         if (lists_used[i]) {
@@ -257,6 +259,7 @@ void DetectEngineAppInspectionEngineSignatureFree(Signature *s)
 {
     SigMatchData *ptrs[DETECT_SM_LIST_MAX] = { NULL };
 
+    /* free engines and put smd in the array */
     DetectEngineAppInspectionEngine *ie = s->app_inspect;
     while (ie) {
         DetectEngineAppInspectionEngine *next = ie->next;
@@ -266,9 +269,22 @@ void DetectEngineAppInspectionEngineSignatureFree(Signature *s)
         ie = next;
     }
 
+    /* free the smds */
     int i;
     for (i = 0; i < DETECT_SM_LIST_MAX; i++)
     {
+        if (ptrs[i] == NULL)
+            continue;
+
+        SigMatchData *smd = ptrs[i];
+        while(1) {
+            if (sigmatch_table[smd->type].Free != NULL) {
+                sigmatch_table[smd->type].Free(smd->ctx);
+            }
+            if (smd->is_last)
+                break;
+            smd++;
+        }
         SCFree(ptrs[i]);
     }
 }
index 50fd5c6240b5c4b3125079a39618388594e2371e..48f34de400f58a030977b506c95938218db36821 100644 (file)
@@ -1041,13 +1041,26 @@ static void SigRefFree (Signature *s)
     SCReturn;
 }
 
-static void SigMatchFreeArrays(Signature *s)
+static void SigMatchFreeArrays(Signature *s, int ctxs)
 {
     if (s != NULL) {
         int type;
         for (type = 0; type < DETECT_SM_LIST_MAX; type++) {
-            if (s->sm_arrays[type] != NULL)
+            if (s->sm_arrays[type] != NULL) {
+                if (ctxs) {
+                    SigMatchData *smd = s->sm_arrays[type];
+                    while(1) {
+                        if (sigmatch_table[smd->type].Free != NULL) {
+                            sigmatch_table[smd->type].Free(smd->ctx);
+                        }
+                        if (smd->is_last)
+                            break;
+                        smd++;
+                    }
+                }
+
                 SCFree(s->sm_arrays[type]);
+            }
         }
     }
 }
@@ -1074,7 +1087,11 @@ void SigFree(Signature *s)
             }
         }
     }
-    SigMatchFreeArrays(s);
+    SigMatchFreeArrays(s, (s->init_data == NULL));
+    if (s->init_data) {
+        SCFree(s->init_data);
+        s->init_data = NULL;
+    }
 
     if (s->sp != NULL) {
         DetectPortCleanupList(s->sp);
index 27bef2c56b35740331078fef1170e88f12bfe35b..8e8f92abdb8010b4d6f6d31f59116379f8bfefad 100644 (file)
@@ -3917,6 +3917,13 @@ static int SigMatchListLen(SigMatch *sm)
     return len;
 }
 
+/** \internal
+ *  \brief perform final per signature setup tasks
+ *
+ *  - Create SigMatchData arrays from the init only SigMatch lists
+ *  - Setup per signature inspect engines
+ *  - remove signature init data.
+ */
 static int SigMatchPrepare(DetectEngineCtx *de_ctx)
 {
     SCEnter();
@@ -3940,13 +3947,25 @@ static int SigMatchPrepare(DetectEngineCtx *de_ctx)
                 for (; sm != NULL; sm = sm->next, smd++) {
                     smd->type = sm->type;
                     smd->ctx = sm->ctx;
+                    sm->ctx = NULL; // SigMatch no longer owns the ctx
                     smd->is_last = (sm->next == NULL);
                 }
             }
         }
+
+        /* set up inspect engines */
         DetectEngineAppInspectionEngine2Signature(s);
 
-        /* TODO free lists etc */
+        /* free lists. Ctx' are xferred to sm_arrays so won't get freed */
+        int i;
+        for (i = 0; i < DETECT_SM_LIST_MAX; i++) {
+            SigMatch *sm = s->init_data->smlists[i];
+            while (sm != NULL) {
+                SigMatch *nsm = sm->next;
+                SigMatchFree(sm);
+                sm = nsm;
+            }
+        }
         SCFree(s->init_data);
         s->init_data = NULL;
     }