]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
boyermoore: optimization with one alloc less
authorPhilippe Antoine <contact@catenacyber.fr>
Tue, 4 Jun 2019 09:46:01 +0000 (11:46 +0200)
committerVictor Julien <victor@inliniac.net>
Wed, 21 Aug 2019 09:55:48 +0000 (11:55 +0200)
Fixes #1220

src/util-spm-bm.c
src/util-spm-bm.h

index 234d169597dec3785e738de508ec736f5b3be895..94dbcb82b16e3dc6262b16a3946c5157c49d4a99 100644 (file)
@@ -76,7 +76,7 @@ void BoyerMooreCtxToNocase(BmCtx *bm_ctx, uint8_t *needle, uint16_t needle_len)
  */
 BmCtx *BoyerMooreCtxInit(const uint8_t *needle, uint16_t needle_len)
 {
-    BmCtx *new = SCMalloc(sizeof(BmCtx));
+    BmCtx *new = SCMalloc(sizeof(BmCtx) + sizeof(uint16_t) * (needle_len + 1));
     if (unlikely(new == NULL)) {
         SCLogError(SC_ERR_FATAL, "Fatal error encountered in BoyerMooreCtxInit. Exiting...");
         exit(EXIT_FAILURE);
@@ -85,11 +85,6 @@ BmCtx *BoyerMooreCtxInit(const uint8_t *needle, uint16_t needle_len)
     /* Prepare bad chars */
     PreBmBc(needle, needle_len, new->bmBc);
 
-    new->bmGs = SCMalloc(sizeof(uint16_t) * (needle_len + 1));
-    if (new->bmGs == NULL) {
-        exit(EXIT_FAILURE);
-    }
-
     /* Prepare good Suffixes */
     if (PreBmGs(needle, needle_len, new->bmGs) == -1) {
         SCLogError(SC_ERR_FATAL, "Fatal error encountered in BooyerMooreCtxInit. Exiting...");
@@ -128,9 +123,6 @@ void BoyerMooreCtxDeInit(BmCtx *bmctx)
     if (bmctx == NULL)
         SCReturn;
 
-    if (bmctx->bmGs != NULL)
-        SCFree(bmctx->bmGs);
-
     SCFree(bmctx);
 
     SCReturn;
index b792be33dcb5788c188406345d456408b0503286..0512b096177b5cd20ea5bc02973aec53b59e5a4a 100644 (file)
@@ -33,7 +33,8 @@
 /* Context for booyer moore */
 typedef struct BmCtx_ {
     uint16_t bmBc[ALPHABET_SIZE];
-    uint16_t *bmGs; // = SCMalloc(sizeof(int32_t)*(needlelen + 1));
+    //C99 "flexible array member"
+    uint16_t bmGs[]; // = SCMalloc(sizeof(int16_t)*(needlelen + 1));
 } BmCtx;
 
 /** Prepare and return a Boyer Moore context */