From: Justin Viiret Date: Fri, 20 May 2016 03:31:05 +0000 (+1000) Subject: spm: handle null ptrs in destroy funcs gracefully X-Git-Tag: suricata-3.1RC1~114 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f77bc5195cb7c81214a57f3c0e06993923f82b3a;p=thirdparty%2Fsuricata.git spm: handle null ptrs in destroy funcs gracefully This will handle minimal DetectEngineCtx structures (used in delayed detect mode) safely, since they don't get SPM global contexts allocated. Also added BUG_ON checks for valid spm_table entries. --- diff --git a/src/util-spm.c b/src/util-spm.c index 04e7676f5c..d9cbf3f41b 100644 --- a/src/util-spm.c +++ b/src/util-spm.c @@ -108,38 +108,57 @@ void SpmTableSetup(void) SpmGlobalThreadCtx *SpmInitGlobalThreadCtx(uint16_t matcher) { + BUG_ON(spm_table[matcher].InitGlobalThreadCtx == NULL); return spm_table[matcher].InitGlobalThreadCtx(); } void SpmDestroyGlobalThreadCtx(SpmGlobalThreadCtx *global_thread_ctx) { + if (global_thread_ctx == NULL) { + return; + } uint16_t matcher = global_thread_ctx->matcher; spm_table[matcher].DestroyGlobalThreadCtx(global_thread_ctx); } SpmThreadCtx *SpmMakeThreadCtx(const SpmGlobalThreadCtx *global_thread_ctx) { + if (global_thread_ctx == NULL) { + return NULL; + } uint16_t matcher = global_thread_ctx->matcher; + BUG_ON(spm_table[matcher].MakeThreadCtx == NULL); return spm_table[matcher].MakeThreadCtx(global_thread_ctx); } void SpmDestroyThreadCtx(SpmThreadCtx *thread_ctx) { + if (thread_ctx == NULL) { + return; + } uint16_t matcher = thread_ctx->matcher; + BUG_ON(spm_table[matcher].DestroyThreadCtx == NULL); spm_table[matcher].DestroyThreadCtx(thread_ctx); } SpmCtx *SpmInitCtx(const uint8_t *needle, uint16_t needle_len, int nocase, SpmGlobalThreadCtx *global_thread_ctx) { + BUG_ON(global_thread_ctx == NULL); uint16_t matcher = global_thread_ctx->matcher; + BUG_ON(spm_table[matcher].InitCtx == NULL); return spm_table[matcher].InitCtx(needle, needle_len, nocase, global_thread_ctx); } void SpmDestroyCtx(SpmCtx *ctx) { - spm_table[ctx->matcher].DestroyCtx(ctx); + if (ctx == NULL) { + return; + } + uint16_t matcher = ctx->matcher; + BUG_ON(spm_table[matcher].DestroyCtx == NULL); + spm_table[matcher].DestroyCtx(ctx); } uint8_t *SpmScan(const SpmCtx *ctx, SpmThreadCtx *thread_ctx,