From: Philippe Antoine Date: Tue, 4 Jun 2019 09:46:01 +0000 (+0200) Subject: boyermoore: optimization with one alloc less X-Git-Tag: suricata-5.0.0-rc1~146 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=63deb8862f8a06f4c0f7517def5f7f972b489f48;p=thirdparty%2Fsuricata.git boyermoore: optimization with one alloc less Fixes #1220 --- diff --git a/src/util-spm-bm.c b/src/util-spm-bm.c index 234d169597..94dbcb82b1 100644 --- a/src/util-spm-bm.c +++ b/src/util-spm-bm.c @@ -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; diff --git a/src/util-spm-bm.h b/src/util-spm-bm.h index b792be33dc..0512b09617 100644 --- a/src/util-spm-bm.h +++ b/src/util-spm-bm.h @@ -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 */