]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
spm: handle null ptrs in destroy funcs gracefully
authorJustin Viiret <justin.viiret@intel.com>
Fri, 20 May 2016 03:31:05 +0000 (13:31 +1000)
committerVictor Julien <victor@inliniac.net>
Fri, 20 May 2016 10:32:39 +0000 (12:32 +0200)
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.

src/util-spm.c

index 04e7676f5cde788800cbff9193ec564dfd6d0ef8..d9cbf3f41bfe7eefbc23f7eb75ee6020f527394b 100644 (file)
@@ -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,