From: Victor Julien Date: Sun, 16 Oct 2016 19:29:34 +0000 (+0200) Subject: detect: improve memory handling & comments X-Git-Tag: suricata-4.0.0-beta1~406 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6f7e4adbe85e634b01f1cb480fbaee0b9238672d;p=thirdparty%2Fsuricata.git detect: improve memory handling & comments --- diff --git a/src/detect-engine.c b/src/detect-engine.c index 002e69c1a3..d6823fc14b 100644 --- a/src/detect-engine.c +++ b/src/detect-engine.c @@ -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]); } } diff --git a/src/detect-parse.c b/src/detect-parse.c index 50fd5c6240..48f34de400 100644 --- a/src/detect-parse.c +++ b/src/detect-parse.c @@ -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); diff --git a/src/detect.c b/src/detect.c index 27bef2c56b..8e8f92abdb 100644 --- a/src/detect.c +++ b/src/detect.c @@ -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; }