detect: improve memory handling & comments

pull/2559/head
Victor Julien 9 years ago
parent 8edc954e82
commit 6f7e4adbe8

@ -148,7 +148,7 @@ int DetectEngineAppInspectionEngine2Signature(Signature *s)
DetectEngineAppInspectionEngine *t = g_app_inspect_engines; DetectEngineAppInspectionEngine *t = g_app_inspect_engines;
while (t != NULL) { while (t != NULL) {
if (s->init_data->smlists[t->sm_list] == NULL) if (s->sm_arrays[t->sm_list] == NULL)
goto next; goto next;
if (t->alproto == ALPROTO_UNKNOWN) { if (t->alproto == ALPROTO_UNKNOWN) {
/* special case, inspect engine applies to all protocols */ /* special case, inspect engine applies to all protocols */
@ -234,6 +234,8 @@ next:
t = t->next; t = t->next;
} }
/* clear s->sm_arrays for those lists that we put
* in the inspect engines. They own it now. */
int i; int i;
for (i = 0; i < DETECT_SM_LIST_MAX; i++) { for (i = 0; i < DETECT_SM_LIST_MAX; i++) {
if (lists_used[i]) { if (lists_used[i]) {
@ -257,6 +259,7 @@ void DetectEngineAppInspectionEngineSignatureFree(Signature *s)
{ {
SigMatchData *ptrs[DETECT_SM_LIST_MAX] = { NULL }; SigMatchData *ptrs[DETECT_SM_LIST_MAX] = { NULL };
/* free engines and put smd in the array */
DetectEngineAppInspectionEngine *ie = s->app_inspect; DetectEngineAppInspectionEngine *ie = s->app_inspect;
while (ie) { while (ie) {
DetectEngineAppInspectionEngine *next = ie->next; DetectEngineAppInspectionEngine *next = ie->next;
@ -266,9 +269,22 @@ void DetectEngineAppInspectionEngineSignatureFree(Signature *s)
ie = next; ie = next;
} }
/* free the smds */
int i; int i;
for (i = 0; i < DETECT_SM_LIST_MAX; 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]); SCFree(ptrs[i]);
} }
} }

@ -1041,13 +1041,26 @@ static void SigRefFree (Signature *s)
SCReturn; SCReturn;
} }
static void SigMatchFreeArrays(Signature *s) static void SigMatchFreeArrays(Signature *s, int ctxs)
{ {
if (s != NULL) { if (s != NULL) {
int type; int type;
for (type = 0; type < DETECT_SM_LIST_MAX; 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]); 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) { if (s->sp != NULL) {
DetectPortCleanupList(s->sp); DetectPortCleanupList(s->sp);

@ -3917,6 +3917,13 @@ static int SigMatchListLen(SigMatch *sm)
return len; 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) static int SigMatchPrepare(DetectEngineCtx *de_ctx)
{ {
SCEnter(); SCEnter();
@ -3940,13 +3947,25 @@ static int SigMatchPrepare(DetectEngineCtx *de_ctx)
for (; sm != NULL; sm = sm->next, smd++) { for (; sm != NULL; sm = sm->next, smd++) {
smd->type = sm->type; smd->type = sm->type;
smd->ctx = sm->ctx; smd->ctx = sm->ctx;
sm->ctx = NULL; // SigMatch no longer owns the ctx
smd->is_last = (sm->next == NULL); smd->is_last = (sm->next == NULL);
} }
} }
} }
/* set up inspect engines */
DetectEngineAppInspectionEngine2Signature(s); 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); SCFree(s->init_data);
s->init_data = NULL; s->init_data = NULL;
} }

Loading…
Cancel
Save