From 25a3a5c6d8ad5a343e724d5202962802a3d984f9 Mon Sep 17 00:00:00 2001 From: Pablo Rincon Date: Fri, 19 Feb 2010 14:56:06 +0100 Subject: [PATCH] Adding mem wrapper to debug runtime alloc()/free() functions. Fixing some memory leaks. --- src/Makefile.am | 2 + src/alert-debuglog.c | 6 +- src/alert-fastlog.c | 6 +- src/alert-prelude.c | 6 +- src/alert-unified-alert.c | 12 ++-- src/alert-unified-log.c | 23 ++++-- src/alert-unified2-alert.c | 22 +++--- src/app-layer-dcerpc.c | 10 +-- src/app-layer-detect-proto.c | 62 ++++++++-------- src/app-layer-ftp.c | 8 +-- src/app-layer-htp.c | 48 +++++++++---- src/app-layer-htp.h | 2 + src/app-layer-parser.c | 48 ++++++------- src/app-layer-smb.c | 4 +- src/app-layer-smb2.c | 4 +- src/app-layer-tls.c | 4 +- src/conf-yaml-loader.c | 14 ++-- src/conf.c | 42 +++++------ src/counters.c | 82 ++++++++++----------- src/decode-raw.c | 5 ++ src/defrag.c | 119 +++++++++++++++---------------- src/detect-ack.c | 6 +- src/detect-bytejump.c | 10 +-- src/detect-bytetest.c | 12 ++-- src/detect-classtype.c | 2 +- src/detect-content.c | 71 ++++++++++-------- src/detect-csum.c | 106 +++++++++++++-------------- src/detect-dce-iface.c | 8 +-- src/detect-dce-opnum.c | 16 ++--- src/detect-dce-stub-data.c | 2 +- src/detect-decode-event.c | 14 ++-- src/detect-depth.c | 8 +-- src/detect-distance.c | 10 +-- src/detect-dsize.c | 26 +++---- src/detect-engine-address-ipv4.c | 2 + src/detect-engine-address.c | 24 +++---- src/detect-engine-iponly.c | 11 +-- src/detect-engine-mpm.c | 20 +++--- src/detect-engine-port.c | 76 ++++---------------- src/detect-engine-proto.c | 4 +- src/detect-engine-siggroup.c | 96 +++++++++++-------------- src/detect-engine-sigorder.c | 27 ++++--- src/detect-engine-threshold.c | 8 +-- src/detect-engine.c | 12 ++-- src/detect-fast-pattern.c | 15 ++-- src/detect-flags.c | 94 ++++++++++++------------ src/detect-flow.c | 10 +-- src/detect-flowbits.c | 10 +-- src/detect-flowint.c | 20 +++--- src/detect-flowvar.c | 22 +++--- src/detect-fragbits.c | 30 ++++---- src/detect-fragoffset.c | 10 +-- src/detect-ftpbounce.c | 1 + src/detect-gid.c | 2 +- src/detect-http-cookie.c | 29 +++++--- src/detect-http-method.c | 33 +++++---- src/detect-icmp-id.c | 10 +-- src/detect-icmp-seq.c | 10 +-- src/detect-icode.c | 10 +-- src/detect-id.c | 12 ++-- src/detect-ipopts.c | 18 ++--- src/detect-ipproto.c | 24 +++---- src/detect-isdataat.c | 14 ++-- src/detect-itype.c | 10 +-- src/detect-metadata.c | 4 +- src/detect-msg.c | 10 +-- src/detect-offset.c | 8 +-- src/detect-parse.c | 78 +++++++++++--------- src/detect-pcre.c | 26 +++---- src/detect-pktvar.c | 22 +++--- src/detect-reference.c | 4 +- src/detect-rev.c | 6 +- src/detect-rpc.c | 10 +-- src/detect-sameip.c | 2 +- src/detect-seq.c | 6 +- src/detect-sid.c | 6 +- src/detect-stream_size.c | 26 ++++--- src/detect-threshold.c | 22 +++--- src/detect-tls-version.c | 10 +-- src/detect-ttl.c | 30 ++++---- src/detect-uricontent.c | 55 +++++++++----- src/detect-urilen.c | 32 ++++----- src/detect-window.c | 10 +-- src/detect-within.c | 6 +- src/detect.c | 53 +++++++++----- src/detect.h | 2 + src/flow-alert-sid.c | 4 +- src/flow-bit.c | 5 +- src/flow-queue.c | 2 +- src/flow-util.c | 4 +- src/flow-var.c | 10 +-- src/flow.c | 6 +- src/host.c | 5 +- src/log-httplog.c | 6 +- src/output.c | 12 ++-- src/pkt-var.c | 9 +-- src/reputation.c | 20 +++--- src/runmodes.c | 14 ++-- src/source-ipfw.c | 6 +- src/source-nfq.c | 4 +- src/source-pcap-file.c | 15 ++-- src/source-pcap.c | 20 +++--- src/source-pfring.c | 4 +- src/stream-tcp-reassemble.c | 26 ++++--- src/stream-tcp.c | 12 ++-- src/stream.c | 8 +-- src/suricata-common.h | 10 ++- src/suricata.c | 47 ++++++++++-- src/suricata.h | 1 + src/tm-modules.c | 10 +-- src/tm-queues.c | 2 +- src/tm-threads.c | 10 +-- src/tmqh-flow.c | 16 ++--- src/util-bloomfilter-counting.c | 12 ++-- src/util-bloomfilter.c | 12 ++-- src/util-classification-config.c | 12 ++-- src/util-cuda-handlers.c | 22 +++--- src/util-cuda.c | 16 ++--- src/util-debug-filters.c | 38 +++++----- src/util-fmemopen.c | 4 +- src/util-hash.c | 20 +++--- src/util-hashlist.c | 22 +++--- src/util-host-os-info.c | 20 +++--- src/util-mem.h | 105 +++++++++++++++++++++++++++ src/util-mpm-b2g-cuda.c | 76 ++++++++++---------- src/util-mpm-b2g.c | 76 ++++++++++---------- src/util-mpm-b3g.c | 82 ++++++++++----------- src/util-mpm-wumanber.c | 70 +++++++++--------- src/util-mpm.c | 22 +++--- src/util-pool.c | 36 +++++----- src/util-radix-tree.c | 45 ++++++------ src/util-rule-vars.c | 4 +- src/util-spm-bm.c | 8 +-- src/util-spm.c | 24 +++---- src/util-unittest-helper.c | 80 ++++++++++++++------- src/util-unittest.c | 6 +- src/util-var-name.c | 8 +-- src/win32-misc.c | 8 +-- 138 files changed, 1654 insertions(+), 1374 deletions(-) create mode 100644 src/util-mem.h diff --git a/src/Makefile.am b/src/Makefile.am index 4d507a2bc1..fa8052a2bb 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -31,6 +31,8 @@ flow.c flow.h \ flow-queue.c flow-queue.h \ flow-hash.c flow-hash.h \ flow-util.c flow-util.h \ +flow-mem.h \ +util-mem.h \ flow-var.c flow-var.h \ flow-bit.c flow-bit.h \ flow-alert-sid.c flow-alert-sid.h \ diff --git a/src/alert-debuglog.c b/src/alert-debuglog.c index df3f614f24..ca7f18fee1 100644 --- a/src/alert-debuglog.c +++ b/src/alert-debuglog.c @@ -192,7 +192,7 @@ TmEcode AlertDebugLog (ThreadVars *tv, Packet *p, void *data, PacketQueue *pq) TmEcode AlertDebugLogThreadInit(ThreadVars *t, void *initdata, void **data) { - AlertDebugLogThread *aft = malloc(sizeof(AlertDebugLogThread)); + AlertDebugLogThread *aft = SCMalloc(sizeof(AlertDebugLogThread)); if (aft == NULL) { return TM_ECODE_FAILED; } @@ -201,7 +201,7 @@ TmEcode AlertDebugLogThreadInit(ThreadVars *t, void *initdata, void **data) if(initdata == NULL) { SCLogDebug("Error getting context for DebugLog. \"initdata\" argument NULL"); - free(aft); + SCFree(aft); return TM_ECODE_FAILED; } /** Use the Ouptut Context (file pointer and mutex) */ @@ -221,7 +221,7 @@ TmEcode AlertDebugLogThreadDeinit(ThreadVars *t, void *data) /* clear memory */ memset(aft, 0, sizeof(AlertDebugLogThread)); - free(aft); + SCFree(aft); return TM_ECODE_OK; } diff --git a/src/alert-fastlog.c b/src/alert-fastlog.c index 91df947729..57e4917307 100644 --- a/src/alert-fastlog.c +++ b/src/alert-fastlog.c @@ -171,7 +171,7 @@ TmEcode AlertFastLog (ThreadVars *tv, Packet *p, void *data, PacketQueue *pq) TmEcode AlertFastLogThreadInit(ThreadVars *t, void *initdata, void **data) { - AlertFastLogThread *aft = malloc(sizeof(AlertFastLogThread)); + AlertFastLogThread *aft = SCMalloc(sizeof(AlertFastLogThread)); if (aft == NULL) { return TM_ECODE_FAILED; } @@ -179,7 +179,7 @@ TmEcode AlertFastLogThreadInit(ThreadVars *t, void *initdata, void **data) if(initdata == NULL) { SCLogDebug("Error getting context for AlertFastLog. \"initdata\" argument NULL"); - free(aft); + SCFree(aft); return TM_ECODE_FAILED; } /** Use the Ouptut Context (file pointer and mutex) */ @@ -198,7 +198,7 @@ TmEcode AlertFastLogThreadDeinit(ThreadVars *t, void *data) /* clear memory */ memset(aft, 0, sizeof(AlertFastLogThread)); - free(aft); + SCFree(aft); return TM_ECODE_OK; } diff --git a/src/alert-prelude.c b/src/alert-prelude.c index 11f8749e8f..d5e9b6f2d7 100644 --- a/src/alert-prelude.c +++ b/src/alert-prelude.c @@ -713,7 +713,7 @@ TmEcode AlertPreludeThreadInit(ThreadVars *t, void *initdata, void **data) SCReturnInt(TM_ECODE_FAILED); } - aun = malloc(sizeof(AlertPreludeThread)); + aun = SCMalloc(sizeof(AlertPreludeThread)); if (aun == NULL) { SCReturnInt(TM_ECODE_FAILED); } @@ -744,7 +744,7 @@ TmEcode AlertPreludeThreadDeinit(ThreadVars *t, void *data) /* clear memory */ memset(aun, 0, sizeof(AlertPreludeThread)); - free(aun); + SCFree(aun); SCReturnInt(TM_ECODE_OK); } @@ -801,7 +801,7 @@ LogFileCtx *AlertPreludeInitCtx(ConfNode *conf) SCReturnPtr(NULL, "AlertPreludeCtx"); } - ctx = malloc(sizeof(AlertPreludeCtx)); + ctx = SCMalloc(sizeof(AlertPreludeCtx)); if ( ctx == NULL ) { prelude_perror(ret, "Unable to allocate memory"); prelude_client_destroy(client, PRELUDE_CLIENT_EXIT_STATUS_SUCCESS); diff --git a/src/alert-unified-alert.c b/src/alert-unified-alert.c index 3577b63397..a1be1eadb8 100644 --- a/src/alert-unified-alert.c +++ b/src/alert-unified-alert.c @@ -238,7 +238,7 @@ TmEcode AlertUnifiedAlert (ThreadVars *tv, Packet *p, void *data, PacketQueue *p TmEcode AlertUnifiedAlertThreadInit(ThreadVars *t, void *initdata, void **data) { - AlertUnifiedAlertThread *aun = malloc(sizeof(AlertUnifiedAlertThread)); + AlertUnifiedAlertThread *aun = SCMalloc(sizeof(AlertUnifiedAlertThread)); if (aun == NULL) { return TM_ECODE_FAILED; } @@ -246,7 +246,7 @@ TmEcode AlertUnifiedAlertThreadInit(ThreadVars *t, void *initdata, void **data) if (initdata == NULL) { SCLogDebug("Error getting context for UnifiedAlert. \"initdata\" argument NULL"); - free(aun); + SCFree(aun); return TM_ECODE_FAILED; } /** Use the Ouptut Context (file pointer and mutex) */ @@ -270,7 +270,7 @@ TmEcode AlertUnifiedAlertThreadDeinit(ThreadVars *t, void *data) } /* clear memory */ memset(aun, 0, sizeof(AlertUnifiedAlertThread)); - free(aun); + SCFree(aun); return TM_ECODE_OK; error: @@ -298,7 +298,7 @@ LogFileCtx *AlertUnifiedAlertInitCtx(ConfNode *conf) filename = ConfNodeLookupChildValue(conf, "filename"); if (filename == NULL) filename = DEFAULT_LOG_FILENAME; - file_ctx->prefix = strdup(filename); + file_ctx->prefix = SCStrdup(filename); const char *s_limit = NULL; uint32_t limit = DEFAULT_LIMIT; @@ -343,7 +343,7 @@ int AlertUnifiedAlertOpenFileCtx(LogFileCtx *file_ctx, const char *prefix) if (file_ctx->filename != NULL) filename = file_ctx->filename; else - filename = file_ctx->filename = malloc(PATH_MAX); /* XXX some sane default? */ + filename = file_ctx->filename = SCMalloc(PATH_MAX); /* XXX some sane default? */ /* get the time so we can have a filename with seconds since epoch */ struct timeval ts; @@ -400,7 +400,7 @@ static int AlertUnifiedAlertTestRotate01(void) lf = AlertUnifiedAlertInitCtx(NULL); if (lf == NULL) return 0; - char *filename = strdup(lf->filename); + char *filename = SCStrdup(lf->filename); memset(&tv, 0, sizeof(ThreadVars)); diff --git a/src/alert-unified-log.c b/src/alert-unified-log.c index 86859db8c1..85d6659fe3 100644 --- a/src/alert-unified-log.c +++ b/src/alert-unified-log.c @@ -254,7 +254,7 @@ TmEcode AlertUnifiedLog (ThreadVars *tv, Packet *p, void *data, PacketQueue *pq) TmEcode AlertUnifiedLogThreadInit(ThreadVars *t, void *initdata, void **data) { - AlertUnifiedLogThread *aun = malloc(sizeof(AlertUnifiedLogThread)); + AlertUnifiedLogThread *aun = SCMalloc(sizeof(AlertUnifiedLogThread)); if (aun == NULL) { return TM_ECODE_FAILED; } @@ -262,12 +262,23 @@ TmEcode AlertUnifiedLogThreadInit(ThreadVars *t, void *initdata, void **data) if (initdata == NULL) { SCLogDebug("Error getting context for UnifiedLog. \"initdata\" argument NULL"); - free(aun); + SCFree(aun); return TM_ECODE_FAILED; } /** Use the Ouptut Context (file pointer and mutex) */ aun->file_ctx = (LogFileCtx*) initdata; + /** Write Unified header */ + int ret = AlertUnifiedLogWriteFileHeader(aun->file_ctx); + if (ret != 0) { + printf("Error: AlertUnifiedLogWriteFileHeader failed.\n"); + SCFree(aun); + return TM_ECODE_FAILED; + } + + /* XXX make configurable */ + aun->file_ctx->size_limit = 1 * 1024 * 1024; + *data = (void *)aun; return TM_ECODE_OK; } @@ -287,7 +298,7 @@ TmEcode AlertUnifiedLogThreadDeinit(ThreadVars *t, void *data) /* clear memory */ memset(aun, 0, sizeof(AlertUnifiedLogThread)); - free(aun); + SCFree(aun); return TM_ECODE_OK; error: @@ -316,7 +327,7 @@ LogFileCtx *AlertUnifiedLogInitCtx(ConfNode *conf) } if (filename == NULL) filename = DEFAULT_LOG_FILENAME; - file_ctx->prefix = strdup(filename); + file_ctx->prefix = SCStrdup(filename); const char *s_limit = NULL; uint32_t limit = DEFAULT_LIMIT; @@ -362,7 +373,7 @@ int AlertUnifiedLogOpenFileCtx(LogFileCtx *file_ctx, const char *prefix) if (file_ctx->filename != NULL) filename = file_ctx->filename; else - filename = file_ctx->filename = malloc(PATH_MAX); /* XXX some sane default? */ + filename = file_ctx->filename = SCMalloc(PATH_MAX); /* XXX some sane default? */ /* get the time so we can have a filename with seconds since epoch */ struct timeval ts; @@ -417,7 +428,7 @@ static int AlertUnifiedLogTestRotate01(void) lf = AlertUnifiedLogInitCtx(NULL); if (lf == NULL) return 0; - char *filename = strdup(lf->filename); + char *filename = SCStrdup(lf->filename); memset(&tv, 0, sizeof(ThreadVars)); diff --git a/src/alert-unified2-alert.c b/src/alert-unified2-alert.c index 146f135221..72ea3321e4 100644 --- a/src/alert-unified2-alert.c +++ b/src/alert-unified2-alert.c @@ -520,7 +520,7 @@ int Unified2IPv4TypeAlert (ThreadVars *tv, Packet *p, void *data, PacketQueue *p TmEcode Unified2AlertThreadInit(ThreadVars *t, void *initdata, void **data) { - Unified2AlertThread *aun = malloc(sizeof(Unified2AlertThread)); + Unified2AlertThread *aun = SCMalloc(sizeof(Unified2AlertThread)); if (aun == NULL) { return TM_ECODE_FAILED; } @@ -528,7 +528,7 @@ TmEcode Unified2AlertThreadInit(ThreadVars *t, void *initdata, void **data) if(initdata == NULL) { SCLogDebug("Error getting context for Unified2Alert. \"initdata\" argument NULL"); - free(aun); + SCFree(aun); return TM_ECODE_FAILED; } /** Use the Ouptut Context (file pointer and mutex) */ @@ -562,7 +562,7 @@ TmEcode Unified2AlertThreadDeinit(ThreadVars *t, void *data) /* clear memory */ memset(aun, 0, sizeof(Unified2AlertThread)); - free(aun); + SCFree(aun); return TM_ECODE_OK; error: @@ -590,7 +590,7 @@ LogFileCtx *Unified2AlertInitCtx(ConfNode *conf) } if (filename == NULL) filename = DEFAULT_LOG_FILENAME; - file_ctx->prefix = strdup(filename); + file_ctx->prefix = SCStrdup(filename); const char *s_limit = NULL; uint32_t limit = DEFAULT_LIMIT; @@ -635,7 +635,7 @@ int Unified2AlertOpenFileCtx(LogFileCtx *file_ctx, const char *prefix) if (file_ctx->filename != NULL) filename = file_ctx->filename; else - filename = file_ctx->filename = malloc(PATH_MAX); /* XXX some sane default? */ + filename = file_ctx->filename = SCMalloc(PATH_MAX); /* XXX some sane default? */ /** get the time so we can have a filename with seconds since epoch */ struct timeval ts; @@ -805,7 +805,7 @@ static int Unified2Test02 (void) { * \retval 0 on failure */ -static int Unified2Test03 (void) { +static int Unified2Test03 (void) { ThreadVars tv; DecodeThreadVars dtv; PacketQueue pq; @@ -866,6 +866,12 @@ static int Unified2Test03 (void) { if(LogFileFreeCtx(lf)==0) return 0; + Packet *pkt = PacketDequeue(&pq); + while (pkt != NULL) { + SCFree(pkt); + pkt = PacketDequeue(&pq); + } + return 1; } @@ -1012,7 +1018,7 @@ static int Unified2TestRotate01(void) lf = Unified2AlertInitCtx(NULL); if (lf == NULL) return 0; - char *filename = strdup(lf->filename); + char *filename = SCStrdup(lf->filename); memset(&tv, 0, sizeof(ThreadVars)); @@ -1042,7 +1048,7 @@ static int Unified2TestRotate01(void) error: Unified2AlertThreadDeinit(&tv, data); if (lf != NULL) LogFileFreeCtx(lf); - if (filename != NULL) free(filename); + if (filename != NULL) SCFree(filename); return r; } #endif diff --git a/src/app-layer-dcerpc.c b/src/app-layer-dcerpc.c index ad125e3c41..d7b8a63819 100644 --- a/src/app-layer-dcerpc.c +++ b/src/app-layer-dcerpc.c @@ -215,7 +215,7 @@ static uint32_t DCERPCParseBINDCTXItem(DCERPC *dcerpc, uint8_t *input, uint32_t dcerpc->dcerpcbindbindack.versionminor |= *(p + 23) << 8; if (dcerpc->dcerpcbindbindack.ctxid == dcerpc->dcerpcbindbindack.numctxitems - dcerpc->dcerpcbindbindack.numctxitemsleft) { - dcerpc->dcerpcbindbindack.uuid_entry = (struct uuid_entry *) calloc(1, + dcerpc->dcerpcbindbindack.uuid_entry = (struct uuid_entry *) SCCalloc(1, sizeof(struct uuid_entry)); if (dcerpc->dcerpcbindbindack.uuid_entry == NULL) { SCReturnUInt(0); @@ -421,7 +421,7 @@ static uint32_t DCERPCParseBINDCTXItem(DCERPC *dcerpc, uint8_t *input, uint32_t p++; --input_len; if (dcerpc->dcerpcbindbindack.ctxid == dcerpc->dcerpcbindbindack.numctxitems - dcerpc->dcerpcbindbindack.numctxitemsleft) { - dcerpc->dcerpcbindbindack.uuid_entry = (struct uuid_entry *) calloc(1, + dcerpc->dcerpcbindbindack.uuid_entry = (struct uuid_entry *) SCCalloc(1, sizeof(struct uuid_entry)); if (dcerpc->dcerpcbindbindack.uuid_entry == NULL) { SCReturnUInt(0); @@ -1238,7 +1238,7 @@ static int DCERPCParse(Flow *f, void *dcerpc_state, } static void *DCERPCStateAlloc(void) { - void *s = malloc(sizeof(DCERPCState)); + void *s = SCMalloc(sizeof(DCERPCState)); if (s == NULL) return NULL; @@ -1254,11 +1254,11 @@ static void DCERPCStateFree(void *s) { while ((item = TAILQ_FIRST(&sstate->dcerpc.dcerpcbindbindack.uuid_list))) { //printUUID("Free", item); TAILQ_REMOVE(&sstate->dcerpc.dcerpcbindbindack.uuid_list, item, next); - free(item); + SCFree(item); } if (s) { - free(s); + SCFree(s); s = NULL; } } diff --git a/src/app-layer-detect-proto.c b/src/app-layer-detect-proto.c index 5404115c21..4f340b4097 100644 --- a/src/app-layer-detect-proto.c +++ b/src/app-layer-detect-proto.c @@ -306,7 +306,7 @@ uint16_t AppLayerDetectGetProto(AlpProtoDetectCtx *ctx, AlpProtoDetectThreadCtx &tdir->pmq, buf, scanlen); #else - Packet *p = malloc(sizeof(Packet)); + Packet *p = SCMalloc(sizeof(Packet)); if (p == NULL) goto end; memset(p, 0, sizeof(Packet)); p->cuda_done = 0; @@ -525,7 +525,7 @@ void AppLayerDetectProtoThreadSpawn() #ifdef UNITTESTS int AlpDetectTest01(void) { - char *buf = strdup("HTTP"); + char *buf = SCStrdup("HTTP"); int r = 1; AlpProtoDetectCtx ctx; @@ -540,18 +540,18 @@ int AlpDetectTest01(void) { AlpProtoInit(&ctx); AlpProtoAdd(&ctx, IPPROTO_TCP, ALPROTO_HTTP, buf, 4, 0, STREAM_TOCLIENT); - free(buf); + SCFree(buf); if (ctx.toclient.id != 1) { r = 0; } - buf = strdup("GET"); + buf = SCStrdup("GET"); AlpProtoAdd(&ctx, IPPROTO_TCP, ALPROTO_HTTP, buf, 4, 0, STREAM_TOSERVER); if (ctx.toserver.id != 1) { r = 0; } - free(buf); + SCFree(buf); AlpProtoTestDestroy(&ctx); @@ -566,7 +566,7 @@ int AlpDetectTest01(void) { } int AlpDetectTest02(void) { - char *buf = strdup("HTTP"); + char *buf = SCStrdup("HTTP"); int r = 1; AlpProtoDetectCtx ctx; @@ -581,7 +581,7 @@ int AlpDetectTest02(void) { AlpProtoInit(&ctx); AlpProtoAdd(&ctx, IPPROTO_TCP, ALPROTO_HTTP, buf, 4, 0, STREAM_TOCLIENT); - free(buf); + SCFree(buf); if (ctx.toclient.id != 1) { r = 0; @@ -591,9 +591,9 @@ int AlpDetectTest02(void) { r = 0; } - buf = strdup("220 "); + buf = SCStrdup("220 "); AlpProtoAdd(&ctx, IPPROTO_TCP, ALPROTO_FTP, buf, 4, 0, STREAM_TOCLIENT); - free(buf); + SCFree(buf); if (ctx.toclient.id != 2) { r = 0; @@ -617,7 +617,7 @@ int AlpDetectTest02(void) { int AlpDetectTest03(void) { uint8_t l7data[] = "HTTP/1.1 200 OK\r\nServer: Apache/1.0\r\n\r\n"; - char *buf = strdup("HTTP"); + char *buf = SCStrdup("HTTP"); int r = 1; AlpProtoDetectCtx ctx; AlpProtoDetectThreadCtx tctx; @@ -633,7 +633,7 @@ int AlpDetectTest03(void) { AlpProtoInit(&ctx); AlpProtoAdd(&ctx, IPPROTO_TCP, ALPROTO_HTTP, buf, 4, 0, STREAM_TOCLIENT); - free(buf); + SCFree(buf); if (ctx.toclient.id != 1) { r = 0; @@ -643,9 +643,9 @@ int AlpDetectTest03(void) { r = 0; } - buf = strdup("220 "); + buf = SCStrdup("220 "); AlpProtoAdd(&ctx, IPPROTO_TCP, ALPROTO_FTP, buf, 4, 0, STREAM_TOCLIENT); - free(buf); + SCFree(buf); if (ctx.toclient.id != 2) { r = 0; @@ -686,7 +686,7 @@ int AlpDetectTest03(void) { int AlpDetectTest04(void) { uint8_t l7data[] = "HTTP/1.1 200 OK\r\nServer: Apache/1.0\r\n\r\n"; - char *buf = strdup("200 "); + char *buf = SCStrdup("200 "); int r = 1; AlpProtoDetectCtx ctx; AlpProtoDetectThreadCtx tctx; @@ -702,7 +702,7 @@ int AlpDetectTest04(void) { AlpProtoInit(&ctx); AlpProtoAdd(&ctx, IPPROTO_TCP, ALPROTO_HTTP, buf, 4, 0, STREAM_TOCLIENT); - free(buf); + SCFree(buf); if (ctx.toclient.id != 1) { r = 0; @@ -743,7 +743,7 @@ int AlpDetectTest04(void) { int AlpDetectTest05(void) { uint8_t l7data[] = "HTTP/1.1 200 OK\r\nServer: Apache/1.0\r\n\r\nBlahblah"; - char *buf = strdup("HTTP"); + char *buf = SCStrdup("HTTP"); int r = 1; AlpProtoDetectCtx ctx; @@ -760,7 +760,7 @@ int AlpDetectTest05(void) { AlpProtoInit(&ctx); AlpProtoAdd(&ctx, IPPROTO_TCP, ALPROTO_HTTP, buf, 4, 0, STREAM_TOCLIENT); - free(buf); + SCFree(buf); if (ctx.toclient.id != 1) { r = 0; @@ -770,9 +770,9 @@ int AlpDetectTest05(void) { r = 0; } - buf = strdup("220 "); + buf = SCStrdup("220 "); AlpProtoAdd(&ctx, IPPROTO_TCP, ALPROTO_FTP, buf, 4, 0, STREAM_TOCLIENT); - free(buf); + SCFree(buf); if (ctx.toclient.id != 2) { r = 0; @@ -813,7 +813,7 @@ int AlpDetectTest05(void) { int AlpDetectTest06(void) { uint8_t l7data[] = "220 Welcome to the OISF FTP server\r\n"; - char *buf = strdup("HTTP"); + char *buf = SCStrdup("HTTP"); int r = 1; AlpProtoDetectCtx ctx; AlpProtoDetectThreadCtx tctx; @@ -829,7 +829,7 @@ int AlpDetectTest06(void) { AlpProtoInit(&ctx); AlpProtoAdd(&ctx, IPPROTO_TCP, ALPROTO_HTTP, buf, 4, 0, STREAM_TOCLIENT); - free(buf); + SCFree(buf); if (ctx.toclient.id != 1) { r = 0; @@ -839,9 +839,9 @@ int AlpDetectTest06(void) { r = 0; } - buf = strdup("220 "); + buf = SCStrdup("220 "); AlpProtoAdd(&ctx, IPPROTO_TCP, ALPROTO_FTP, buf, 4, 0, STREAM_TOCLIENT); - free(buf); + SCFree(buf); if (ctx.toclient.id != 2) { r = 0; @@ -882,7 +882,7 @@ int AlpDetectTest06(void) { int AlpDetectTest07(void) { uint8_t l7data[] = "220 Welcome to the OISF HTTP/FTP server\r\n"; - char *buf = strdup("HTTP"); + char *buf = SCStrdup("HTTP"); int r = 1; AlpProtoDetectCtx ctx; AlpProtoDetectThreadCtx tctx; @@ -898,7 +898,7 @@ int AlpDetectTest07(void) { AlpProtoInit(&ctx); AlpProtoAdd(&ctx, IPPROTO_TCP, ALPROTO_HTTP, buf, 4, 0, STREAM_TOCLIENT); - free(buf); + SCFree(buf); if (ctx.toclient.id != 1) { r = 0; @@ -950,7 +950,7 @@ int AlpDetectTest08(void) { "\x20\x66\x6f\x72\x20\x57\x6f\x72\x6b\x67\x72\x6f\x75\x70\x73\x20\x33\x2e\x31\x61\x00\x02\x4c" "\x4d\x31\x2e\x32\x58\x30\x30\x32\x00\x02\x4c\x41\x4e\x4d\x41\x4e\x32\x2e\x31\x00\x02\x4e\x54" "\x20\x4c\x4d\x20\x30\x2e\x31\x32\x00"; - char *buf = strdup("|ff|SMB"); + char *buf = SCStrdup("|ff|SMB"); int r = 1; AlpProtoDetectCtx ctx; AlpProtoDetectThreadCtx tctx; @@ -966,7 +966,7 @@ int AlpDetectTest08(void) { AlpProtoInit(&ctx); AlpProtoAdd(&ctx, IPPROTO_TCP, ALPROTO_SMB, buf, 8, 4, STREAM_TOCLIENT); - free(buf); + SCFree(buf); if (ctx.toclient.id != 1) { r = 0; @@ -1015,7 +1015,7 @@ int AlpDetectTest09(void) { "\x24\x00\x01\x00x00\x00\x00\x00\x00\x00\x0\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x02"; - char *buf = strdup("|fe|SMB"); + char *buf = SCStrdup("|fe|SMB"); int r = 1; AlpProtoDetectCtx ctx; AlpProtoDetectThreadCtx tctx; @@ -1031,7 +1031,7 @@ int AlpDetectTest09(void) { AlpProtoInit(&ctx); AlpProtoAdd(&ctx, IPPROTO_TCP, ALPROTO_SMB2, buf, 8, 4, STREAM_TOCLIENT); - free(buf); + SCFree(buf); if (ctx.toclient.id != 1) { r = 0; @@ -1075,7 +1075,7 @@ int AlpDetectTest10(void) { "\x01\x00\xb8\x4a\x9f\x4d\x1c\x7d\xcf\x11\x86\x1e\x00\x20\xaf\x6e\x7c\x57" "\x00\x00\x00\x00\x04\x5d\x88\x8a\xeb\x1c\xc9\x11\x9f\xe8\x08\x00\x2b\x10" "\x48\x60\x02\x00\x00\x00"; - char *buf = strdup("|05 00|"); + char *buf = SCStrdup("|05 00|"); int r = 1; AlpProtoDetectCtx ctx; AlpProtoDetectThreadCtx tctx; @@ -1091,7 +1091,7 @@ int AlpDetectTest10(void) { AlpProtoInit(&ctx); AlpProtoAdd(&ctx, IPPROTO_TCP, ALPROTO_DCERPC, buf, 4, 0, STREAM_TOCLIENT); - free(buf); + SCFree(buf); if (ctx.toclient.id != 1) { r = 0; diff --git a/src/app-layer-ftp.c b/src/app-layer-ftp.c index 7ff6366ba0..7bbba47592 100644 --- a/src/app-layer-ftp.c +++ b/src/app-layer-ftp.c @@ -110,8 +110,8 @@ static int FTPParseRequestCommandLine(Flow *f, void *ftp_state, AppLayerParserSt * the ftpbounce condition directly from detect-ftpbounce */ if (fstate->port_line != NULL) - free(fstate->port_line); - fstate->port_line = malloc(input_len); + SCFree(fstate->port_line); + fstate->port_line = SCMalloc(input_len); if (fstate->port_line == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating" "memory"); @@ -219,7 +219,7 @@ static uint64_t ftp_state_memcnt = 0; #endif static void *FTPStateAlloc(void) { - void *s = malloc(sizeof(FtpState)); + void *s = SCMalloc(sizeof(FtpState)); if (s == NULL) return NULL; @@ -235,7 +235,7 @@ static void *FTPStateAlloc(void) { } static void FTPStateFree(void *s) { - free(s); + SCFree(s); #ifdef DEBUG SCMutexLock(&ftp_state_mem_lock); ftp_state_memcnt--; diff --git a/src/app-layer-htp.c b/src/app-layer-htp.c index 97ad5235b6..81d474a6c5 100644 --- a/src/app-layer-htp.c +++ b/src/app-layer-htp.c @@ -31,6 +31,7 @@ #include "util-debug.h" #include "app-layer-htp.h" #include "util-time.h" +#include #ifdef DEBUG static SCMutex htp_state_mem_lock = PTHREAD_MUTEX_INITIALIZER; @@ -46,7 +47,7 @@ static void *HTPStateAlloc(void) { SCEnter(); - HtpState *s = malloc(sizeof(HtpState)); + HtpState *s = SCMalloc(sizeof(HtpState)); if (s == NULL) { goto error; } @@ -79,7 +80,7 @@ error: if (s->connp != NULL) htp_connp_destroy(s->connp); - free(s); + SCFree(s); } SCReturnPtr(NULL, "void"); @@ -88,7 +89,7 @@ error: /** \brief Function to frees the HTTP state memory and also frees the HTTP * connection parser memory which was used by the HTP library */ -static void HTPStateFree(void *state) +void HTPStateFree(void *state) { SCEnter(); @@ -105,7 +106,7 @@ static void HTPStateFree(void *state) } } - free(s); + SCFree(s); #ifdef DEBUG SCMutexLock(&htp_state_mem_lock); @@ -305,7 +306,7 @@ void HtpBodyAppendChunk(HtpBody *body, uint8_t *data, uint32_t len) BodyChunk *bd = NULL; if (body->nchunks == 0) { /* New chunk */ - bd = (BodyChunk *)malloc(sizeof(BodyChunk)); + bd = (BodyChunk *)SCMalloc(sizeof(BodyChunk)); if (bd == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Fatal error, error allocationg memory"); exit(EXIT_FAILURE); @@ -325,7 +326,7 @@ void HtpBodyAppendChunk(HtpBody *body, uint8_t *data, uint32_t len) body->last->len = len; bd = body->last; } else { - bd = (BodyChunk *)malloc(sizeof(BodyChunk)); + bd = (BodyChunk *)SCMalloc(sizeof(BodyChunk)); bd->len = len; bd->data = data; body->last->next = bd; @@ -384,7 +385,7 @@ void HtpBodyFree(HtpBody *body) prev = body->first; while (prev != NULL) { cur = prev->next; - free(prev); + SCFree(prev); prev = cur; } body->first = body->last = NULL; @@ -555,6 +556,8 @@ int HTPParserTest01(void) { " Data is c0oL!"; uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */ TcpSession ssn; + + HtpState *htp_state = NULL; int r = 0; memset(&f, 0, sizeof(f)); memset(&ssn, 0, sizeof(ssn)); @@ -580,7 +583,7 @@ int HTPParserTest01(void) { } } - HtpState *htp_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; + htp_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; if (htp_state == NULL) { printf("no http state: "); result = 0; @@ -610,6 +613,8 @@ int HTPParserTest01(void) { end: StreamL7DataPtrFree(&ssn); StreamTcpFreeConfig(TRUE); + if (htp_state != NULL) + HTPStateFree(htp_state); return result; } @@ -620,6 +625,7 @@ int HTPParserTest02(void) { uint8_t httpbuf1[] = "POST"; uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */ TcpSession ssn; + HtpState *http_state = NULL; memset(&f, 0, sizeof(f)); memset(&ssn, 0, sizeof(ssn)); @@ -636,7 +642,7 @@ int HTPParserTest02(void) { goto end; } - HtpState *http_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; + http_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; if (http_state == NULL) { printf("no http state: "); result = 0; @@ -660,6 +666,8 @@ int HTPParserTest02(void) { end: StreamL7DataPtrFree(&ssn); StreamTcpFreeConfig(TRUE); + if (http_state != NULL) + HTPStateFree(http_state); return result; } @@ -671,6 +679,8 @@ int HTPParserTest03(void) { uint8_t httpbuf1[] = "HELLO / HTTP/1.0\r\n"; uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */ TcpSession ssn; + + HtpState *htp_state = NULL; int r = 0; memset(&f, 0, sizeof(f)); memset(&ssn, 0, sizeof(ssn)); @@ -695,8 +705,7 @@ int HTPParserTest03(void) { goto end; } } - - HtpState *htp_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; + htp_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; if (htp_state == NULL) { printf("no http state: "); result = 0; @@ -723,6 +732,8 @@ int HTPParserTest03(void) { end: StreamL7DataPtrFree(&ssn); StreamTcpFreeConfig(TRUE); + if (htp_state != NULL) + HTPStateFree(htp_state); return result; } @@ -731,6 +742,7 @@ end: int HTPParserTest04(void) { int result = 1; Flow f; + HtpState *htp_state = NULL; uint8_t httpbuf1[] = "World!\r\n"; uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */ TcpSession ssn; @@ -745,7 +757,7 @@ int HTPParserTest04(void) { r = AppLayerParse(&f, ALPROTO_HTTP, STREAM_TOSERVER|STREAM_START| STREAM_EOF, httpbuf1, httplen1); - HtpState *htp_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; + htp_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; if (htp_state == NULL) { printf("no http state: "); result = 0; @@ -772,6 +784,8 @@ int HTPParserTest04(void) { end: StreamL7DataPtrFree(&ssn); StreamTcpFreeConfig(TRUE); + if (htp_state != NULL) + HTPStateFree(htp_state); return result; } @@ -780,6 +794,7 @@ end: int HTPParserTest05(void) { int result = 1; Flow f; + HtpState *http_state = NULL; uint8_t httpbuf1[] = "POST / HTTP/1.0\r\nUser-Agent: Victor/1.0\r\n\r\n"; uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */ uint8_t httpbuf2[] = "Post D"; @@ -848,7 +863,7 @@ int HTPParserTest05(void) { goto end; } - HtpState *http_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; + http_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; if (http_state == NULL) { printf("no http state: "); result = 0; @@ -885,6 +900,8 @@ int HTPParserTest05(void) { end: StreamL7DataPtrFree(&ssn); StreamTcpFreeConfig(TRUE); + if (http_state != NULL) + HTPStateFree(http_state); return result; } @@ -936,6 +953,7 @@ int HTPParserTest06(void) { "aHA=0\r\n\r\n"; uint32_t httplen2 = sizeof(httpbuf2) - 1; /* minus the \0 */ TcpSession ssn; + HtpState *http_state = NULL; memset(&f, 0, sizeof(f)); memset(&ssn, 0, sizeof(ssn)); @@ -960,7 +978,7 @@ int HTPParserTest06(void) { goto end; } - HtpState *http_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; + http_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; if (http_state == NULL) { printf("no http state: "); result = 0; @@ -997,6 +1015,8 @@ int HTPParserTest06(void) { end: StreamL7DataPtrFree(&ssn); StreamTcpFreeConfig(TRUE); + if (http_state != NULL) + HTPStateFree(http_state); return result; } #endif /* UNITTESTS */ diff --git a/src/app-layer-htp.h b/src/app-layer-htp.h index 947f8b2dc5..525e883d0f 100644 --- a/src/app-layer-htp.h +++ b/src/app-layer-htp.h @@ -90,6 +90,8 @@ int HTPCallbackRequestBodyData(htp_tx_data_t *); void HtpBodyPrint(HtpBody *); void HtpBodyFree(HtpBody *); void AppLayerHtpRegisterExtraCallbacks(void); +/* To free the state from unittests using app-layer-htp */ +void HTPStateFree(void *); #endif /* __APP_LAYER_HTP_H__ */ diff --git a/src/app-layer-parser.c b/src/app-layer-parser.c index b221cf9c21..50b3d6556a 100644 --- a/src/app-layer-parser.c +++ b/src/app-layer-parser.c @@ -40,7 +40,7 @@ static uint32_t al_result_pool_elmts = 0; /** \brief Alloc a AppLayerParserResultElmt func for the pool */ static void *AlpResultElmtPoolAlloc(void *null) { - AppLayerParserResultElmt *e = (AppLayerParserResultElmt *)malloc + AppLayerParserResultElmt *e = (AppLayerParserResultElmt *)SCMalloc (sizeof(AppLayerParserResultElmt)); if (e == NULL) { return NULL; @@ -61,9 +61,9 @@ static void AlpResultElmtPoolFree(void *e) if (re->flags & ALP_RESULT_ELMT_ALLOC) { if (re->data_ptr != NULL) - free(re->data_ptr); + SCFree(re->data_ptr); } - free(re); + SCFree(re); #ifdef DEBUG al_result_pool_elmts--; @@ -88,7 +88,7 @@ static void AlpReturnResultElmt(AppLayerParserResultElmt *e) { if (e->flags & ALP_RESULT_ELMT_ALLOC) { if (e->data_ptr != NULL) - free(e->data_ptr); + SCFree(e->data_ptr); } e->flags = 0; e->data_ptr = NULL; @@ -154,7 +154,7 @@ int AlpParseFieldBySize(AppLayerParserResult *output, AppLayerParserState *pstat if ((pstate->store_len + input_len) < size) { if (pstate->store_len == 0) { - pstate->store = malloc(input_len); + pstate->store = SCMalloc(input_len); if (pstate->store == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Memory allocation failed!"); SCReturnInt(-1); @@ -163,7 +163,7 @@ int AlpParseFieldBySize(AppLayerParserResult *output, AppLayerParserState *pstat memcpy(pstate->store, input, input_len); pstate->store_len = input_len; } else { - pstate->store = realloc(pstate->store, (input_len + pstate->store_len)); + pstate->store = SCRealloc(pstate->store, (input_len + pstate->store_len)); if (pstate->store == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Memory reallocation failed!"); SCReturnInt(-1); @@ -185,7 +185,7 @@ int AlpParseFieldBySize(AppLayerParserResult *output, AppLayerParserState *pstat } else { uint32_t diff = size - pstate->store_len; - pstate->store = realloc(pstate->store, (diff + pstate->store_len)); + pstate->store = SCRealloc(pstate->store, (diff + pstate->store_len)); if (pstate->store == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Memory reallocation failed!"); SCReturnInt(-1); @@ -239,7 +239,7 @@ int AlpParseFieldByEOF(AppLayerParserResult *output, AppLayerParserState *pstate SCLogDebug("store_len 0 but no EOF"); /* delimiter field not found, so store the result for the next run */ - pstate->store = malloc(input_len); + pstate->store = SCMalloc(input_len); if (pstate->store == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Memory allocation failed!"); SCReturnInt(-1); @@ -252,7 +252,7 @@ int AlpParseFieldByEOF(AppLayerParserResult *output, AppLayerParserState *pstate if (pstate->flags & APP_LAYER_PARSER_EOF) { SCLogDebug("store_len %" PRIu32 " and EOF", pstate->store_len); - pstate->store = realloc(pstate->store, (input_len + pstate->store_len)); + pstate->store = SCRealloc(pstate->store, (input_len + pstate->store_len)); if (pstate->store == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Memory reallocation failed!"); SCReturnInt(-1); @@ -275,7 +275,7 @@ int AlpParseFieldByEOF(AppLayerParserResult *output, AppLayerParserState *pstate SCLogDebug("store_len %" PRIu32 " but no EOF", pstate->store_len); /* delimiter field not found, so store the result for the next run */ - pstate->store = realloc(pstate->store, (input_len + pstate->store_len)); + pstate->store = SCRealloc(pstate->store, (input_len + pstate->store_len)); if (pstate->store == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Memory reallocation failed!"); SCReturnInt(-1); @@ -326,7 +326,7 @@ int AlpParseFieldByDelimiter(AppLayerParserResult *output, AppLayerParserState * SCLogDebug("delim not found, continue"); /* delimiter field not found, so store the result for the next run */ - pstate->store = malloc(input_len); + pstate->store = SCMalloc(input_len); if (pstate->store == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Memory allocation failed!"); SCReturnInt(-1); @@ -342,7 +342,7 @@ int AlpParseFieldByDelimiter(AppLayerParserResult *output, AppLayerParserState * SCLogDebug("len %" PRIu32 " + %" PRIu32 " = %" PRIu32 "", len, pstate->store_len, len + pstate->store_len); - pstate->store = realloc(pstate->store, (len + pstate->store_len)); + pstate->store = SCRealloc(pstate->store, (len + pstate->store_len)); if (pstate->store == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Memory reallocation failed!"); SCReturnInt(-1); @@ -369,7 +369,7 @@ int AlpParseFieldByDelimiter(AppLayerParserResult *output, AppLayerParserState * if (delim_len > input_len) { /* delimiter field not found, so store the result for the * next run */ - pstate->store = realloc(pstate->store, (input_len + + pstate->store = SCRealloc(pstate->store, (input_len + pstate->store_len)); if (pstate->store == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Memory reallocation failed!"); @@ -409,14 +409,14 @@ int AlpParseFieldByDelimiter(AppLayerParserResult *output, AppLayerParserState * } free_and_return: SCLogDebug("not found and EOF, so free what we have so far."); - free(pstate->store); + SCFree(pstate->store); pstate->store = NULL; pstate->store_len = 0; SCReturnInt(0); } /* delimiter field not found, so store the result for the next run */ - pstate->store = realloc(pstate->store, (input_len + pstate->store_len)); + pstate->store = SCRealloc(pstate->store, (input_len + pstate->store_len)); if (pstate->store == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Memory reallocation failed!"); SCReturnInt(-1); @@ -582,7 +582,7 @@ uint16_t AlpGetStateIdx(uint16_t proto) AppLayerParserStateStore *AppLayerParserStateStoreAlloc(void) { - AppLayerParserStateStore *s = (AppLayerParserStateStore *)malloc + AppLayerParserStateStore *s = (AppLayerParserStateStore *)SCMalloc (sizeof(AppLayerParserStateStore)); if (s == NULL) return NULL; @@ -596,11 +596,11 @@ AppLayerParserStateStore *AppLayerParserStateStoreAlloc(void) void AppLayerParserStateStoreFree(AppLayerParserStateStore *s) { if (s->to_server.store != NULL) - free(s->to_server.store); + SCFree(s->to_server.store); if (s->to_client.store != NULL) - free(s->to_client.store); + SCFree(s->to_client.store); - free(s); + SCFree(s); } static void AppLayerParserResultCleanup(AppLayerParserResult *result) @@ -898,7 +898,7 @@ void AppLayerParserCleanupState(TcpSession *ssn) } StreamTcpDecrMemuse((uint32_t)(StreamL7GetStorageSize() * sizeof(void *))); - free(ssn->aldata); + SCFree(ssn->aldata); ssn->aldata = NULL; } } @@ -933,7 +933,7 @@ void AppLayerParsersInitPostProcess(void) continue; al_proto_table[u16].map_size++; - al_proto_table[u16].map = (AppLayerLocalMap **)malloc + al_proto_table[u16].map = (AppLayerLocalMap **)SCMalloc (al_proto_table[u16].map_size * sizeof(AppLayerLocalMap *)); if (al_proto_table[u16].map == NULL) { @@ -956,7 +956,7 @@ void AppLayerParsersInitPostProcess(void) SCLogDebug("parser_local_id: %" PRIu32 "", parser_local_id); if (parser_local_id < al_proto_table[u16].map_size) { - al_proto_table[u16].map[parser_local_id] = malloc(sizeof(AppLayerLocalMap)); + al_proto_table[u16].map[parser_local_id] = SCMalloc(sizeof(AppLayerLocalMap)); if (al_proto_table[u16].map[parser_local_id] == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "XXX memory error"); exit(1); @@ -1007,7 +1007,7 @@ static int TestProtocolParser(Flow *f, void *test_state, AppLayerParserState *ps */ static void *TestProtocolStateAlloc(void) { - void *s = malloc(sizeof(TestState)); + void *s = SCMalloc(sizeof(TestState)); if (s == NULL) return NULL; @@ -1019,7 +1019,7 @@ static void *TestProtocolStateAlloc(void) */ static void TestProtocolStateFree(void *s) { - free(s); + SCFree(s); } /** \test Test the deallocation of app layer parser memory on occurance of diff --git a/src/app-layer-smb.c b/src/app-layer-smb.c index 5c32f9cd1e..4b0a65bdab 100644 --- a/src/app-layer-smb.c +++ b/src/app-layer-smb.c @@ -1097,7 +1097,7 @@ int isAndX(SMBState *smb_state) { } static void *SMBStateAlloc(void) { - void *s = malloc(sizeof(SMBState)); + void *s = SCMalloc(sizeof(SMBState)); if (s == NULL) return NULL; @@ -1107,7 +1107,7 @@ static void *SMBStateAlloc(void) { static void SMBStateFree(void *s) { if (s) { - free(s); + SCFree(s); s = NULL; } } diff --git a/src/app-layer-smb2.c b/src/app-layer-smb2.c index f23f9ff44e..e2d14f132e 100644 --- a/src/app-layer-smb2.c +++ b/src/app-layer-smb2.c @@ -400,7 +400,7 @@ static int SMB2Parse(Flow *f, void *smb2_state, AppLayerParserState *pstate, static void *SMB2StateAlloc(void) { - void *s = malloc(sizeof(SMB2State)); + void *s = SCMalloc(sizeof(SMB2State)); if (s == NULL) return NULL; @@ -410,7 +410,7 @@ static void *SMB2StateAlloc(void) { static void SMB2StateFree(void *s) { if (s) { - free(s); + SCFree(s); s = NULL; } } diff --git a/src/app-layer-tls.c b/src/app-layer-tls.c index f74ba1bed7..fd793d1746 100644 --- a/src/app-layer-tls.c +++ b/src/app-layer-tls.c @@ -485,7 +485,7 @@ static int TLSParseServerContentType(Flow *f, void *tls_state, AppLayerParserSta */ static void *TLSStateAlloc(void) { - void *s = malloc(sizeof(TlsState)); + void *s = SCMalloc(sizeof(TlsState)); if (s == NULL) return NULL; @@ -497,7 +497,7 @@ static void *TLSStateAlloc(void) */ static void TLSStateFree(void *s) { - free(s); + SCFree(s); } /** \brief Function to register the TLS protocol parsers and other functions diff --git a/src/conf-yaml-loader.c b/src/conf-yaml-loader.c index 97f1b41243..8436f178af 100644 --- a/src/conf-yaml-loader.c +++ b/src/conf-yaml-loader.c @@ -77,16 +77,16 @@ ConfYamlParse(yaml_parser_t *parser, ConfNode *parent, int inseq) value, inseq); if (inseq) { ConfNode *seq_node = ConfNodeNew(); - seq_node->name = calloc(1, DEFAULT_NAME_LEN); + seq_node->name = SCCalloc(1, DEFAULT_NAME_LEN); snprintf(seq_node->name, DEFAULT_NAME_LEN, "%d", seq_idx++); - seq_node->val = strdup(value); + seq_node->val = SCStrdup(value); TAILQ_INSERT_TAIL(&parent->head, seq_node, next); } else { if (state == CONF_KEY) { if (parent->is_seq) { if (parent->val == NULL) { - parent->val = strdup(value); + parent->val = SCStrdup(value); } } ConfNode *n0 = ConfNodeLookupChild(parent, value); @@ -95,7 +95,7 @@ ConfYamlParse(yaml_parser_t *parser, ConfNode *parent, int inseq) } else { node = ConfNodeNew(); - node->name = strdup(value); + node->name = SCStrdup(value); TAILQ_INSERT_TAIL(&parent->head, node, next); } state = CONF_VAL; @@ -103,8 +103,8 @@ ConfYamlParse(yaml_parser_t *parser, ConfNode *parent, int inseq) else { if (node->allow_override) { if (node->val != NULL) - free(node->val); - node->val = strdup(value); + SCFree(node->val); + node->val = SCStrdup(value); } state = CONF_KEY; } @@ -125,7 +125,7 @@ ConfYamlParse(yaml_parser_t *parser, ConfNode *parent, int inseq) if (inseq) { ConfNode *seq_node = ConfNodeNew(); seq_node->is_seq = 1; - seq_node->name = calloc(1, DEFAULT_NAME_LEN); + seq_node->name = SCCalloc(1, DEFAULT_NAME_LEN); snprintf(seq_node->name, DEFAULT_NAME_LEN, "%d", seq_idx++); TAILQ_INSERT_TAIL(&node->head, seq_node, next); ConfYamlParse(parser, seq_node, 0); diff --git a/src/conf.c b/src/conf.c index cde04d821e..2205399b17 100644 --- a/src/conf.c +++ b/src/conf.c @@ -59,7 +59,7 @@ ConfNodeNew(void) { ConfNode *new; - new = calloc(1, sizeof(*new)); + new = SCCalloc(1, sizeof(*new)); if (new == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory for new configuration node"); @@ -75,7 +75,7 @@ ConfNodeNew(void) /** * \brief Free a ConfNode and all of its children. * - * \param node The configuration node to free. + * \param node The configuration node to SCFree. */ void ConfNodeFree(ConfNode *node) @@ -88,10 +88,10 @@ ConfNodeFree(ConfNode *node) } if (node->name != NULL) - free(node->name); + SCFree(node->name); if (node->val != NULL) - free(node->val); - free(node); + SCFree(node->val); + SCFree(node); } /** @@ -110,7 +110,7 @@ ConfGetNode(char *key) char *token; /* Need to dup the key for tokenization... */ - char *tokstr = strdup(key); + char *tokstr = SCStrdup(key); token = strtok_r(tokstr, ".", &saveptr); for (;;) { @@ -121,7 +121,7 @@ ConfGetNode(char *key) if (token == NULL) break; } - free(tokstr); + SCFree(tokstr); return node; } @@ -159,20 +159,20 @@ ConfSet(char *name, char *val, int allow_override) } else { if (node->val != NULL) - free(node->val); - node->val = strdup(val); + SCFree(node->val); + node->val = SCStrdup(val); node->allow_override = allow_override; return 1; } } else { - char *tokstr = strdup(name); + char *tokstr = SCStrdup(name); token = strtok_r(tokstr, ".", &saveptr); node = ConfNodeLookupChild(parent, token); for (;;) { if (node == NULL) { node = ConfNodeNew(); - node->name = strdup(token); + node->name = SCStrdup(token); node->parent = parent; TAILQ_INSERT_TAIL(&parent->head, node, next); parent = node; @@ -185,8 +185,8 @@ ConfSet(char *name, char *val, int allow_override) if (!node->allow_override) break; if (node->val != NULL) - free(node->val); - node->val = strdup(val); + SCFree(node->val); + node->val = SCStrdup(val); node->allow_override = allow_override; break; } @@ -194,7 +194,7 @@ ConfSet(char *name, char *val, int allow_override) node = ConfNodeLookupChild(parent, token); } } - free(tokstr); + SCFree(tokstr); } SCLogDebug("configuration parameter '%s' set", name); @@ -295,7 +295,7 @@ ConfGetBool(char *name, int *val) } /** - * \brief Remove (and free) the provided configuration node. + * \brief Remove (and SCFree) the provided configuration node. */ void ConfNodeRemove(ConfNode *node) @@ -392,7 +392,7 @@ ConfNodeDump(ConfNode *node, const char *prefix) level++; TAILQ_FOREACH(child, &node->head, next) { - name[level] = strdup(child->name); + name[level] = SCStrdup(child->name); if (prefix == NULL) { printf("%s = %s\n", ConfPrintNameArray(name, level), child->val); @@ -402,7 +402,7 @@ ConfNodeDump(ConfNode *node, const char *prefix) ConfPrintNameArray(name, level), child->val); } ConfNodeDump(child, prefix); - free(name[level]); + SCFree(name[level]); } level--; } @@ -651,8 +651,8 @@ ConfNodeLookupChildTest(void) for (u = 0; u < sizeof(test_vals)/sizeof(test_vals[0]); u++) { child = ConfNodeNew(); - child->name = strdup(test_vals[u]); - child->val = strdup(test_vals[u]); + child->name = SCStrdup(test_vals[u]); + child->val = SCStrdup(test_vals[u]); TAILQ_INSERT_TAIL(&parent->head, child, next); } @@ -701,8 +701,8 @@ ConfNodeLookupChildValueTest(void) for (u = 0; u < sizeof(test_vals)/sizeof(test_vals[0]); u++) { child = ConfNodeNew(); - child->name = strdup(test_vals[u]); - child->val = strdup(test_vals[u]); + child->name = SCStrdup(test_vals[u]); + child->val = SCStrdup(test_vals[u]); TAILQ_INSERT_TAIL(&parent->head, child, next); } diff --git a/src/counters.c b/src/counters.c index e10db14e77..5fb41f77f3 100644 --- a/src/counters.c +++ b/src/counters.c @@ -26,7 +26,7 @@ static SCPerfOPIfaceContext *sc_perf_op_ctx = NULL; * * This function returns a string containing the log filename. It uses * allocated memory simply to drop into the existing code a little better - * where a strdup was used. So as before, it is up to the caller to free + * where a SCStrdup was used. So as before, it is up to the caller to free * the memory. * * \retval An allocated string containing the log filename on success or NULL on @@ -40,7 +40,7 @@ static char *SCPerfGetLogFilename(void) if (ConfGet("default-log-dir", &log_dir) != 1) log_dir = DEFAULT_LOG_DIR; - if ( (log_filename = malloc(PATH_MAX)) == NULL) { + if ( (log_filename = SCMalloc(PATH_MAX)) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } @@ -48,7 +48,7 @@ static char *SCPerfGetLogFilename(void) if (snprintf(log_filename, PATH_MAX, "%s/%s", log_dir, SC_PERF_DEFAULT_LOG_FILENAME) < 0) { SCLogError(SC_ERR_SPRINTF, "Sprintf Error"); - free(log_filename); + SCFree(log_filename); return NULL; } @@ -62,7 +62,7 @@ static char *SCPerfGetLogFilename(void) */ static void SCPerfInitOPCtx(void) { - if ( (sc_perf_op_ctx = malloc(sizeof(SCPerfOPIfaceContext))) == NULL) { + if ( (sc_perf_op_ctx = SCMalloc(sizeof(SCPerfOPIfaceContext))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } @@ -79,11 +79,11 @@ static void SCPerfInitOPCtx(void) "to using the standard output for output", sc_perf_op_ctx->file); - free(sc_perf_op_ctx->file); + SCFree(sc_perf_op_ctx->file); /* Let us use the standard output for output */ sc_perf_op_ctx->fp = stdout; - if ( (sc_perf_op_ctx->file = strdup("stdout")) == NULL) { + if ( (sc_perf_op_ctx->file = SCStrdup("stdout")) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } @@ -112,19 +112,19 @@ static void SCPerfReleaseOPCtx() fclose(sc_perf_op_ctx->fp); if (sc_perf_op_ctx->file != NULL) - free(sc_perf_op_ctx->file); + SCFree(sc_perf_op_ctx->file); if (sc_perf_op_ctx->pctmi != NULL) { if (sc_perf_op_ctx->pctmi->tm_name != NULL) - free(sc_perf_op_ctx->pctmi->tm_name); + SCFree(sc_perf_op_ctx->pctmi->tm_name); if (sc_perf_op_ctx->pctmi->head != NULL) - free(sc_perf_op_ctx->pctmi->head); + SCFree(sc_perf_op_ctx->pctmi->head); - free(sc_perf_op_ctx->pctmi); + SCFree(sc_perf_op_ctx->pctmi); } - free(sc_perf_op_ctx); + SCFree(sc_perf_op_ctx); } return; @@ -325,7 +325,7 @@ static int SCPerfParseTBCounterInterval(SCPerfCounter *pc, char *interval) pc->type_q->total_secs = ((pc->type_q->hours * 60 * 60) + (pc->type_q->minutes * 60) + pc->type_q->seconds); - free(regex); + SCFree(regex); return 0; error: @@ -343,28 +343,28 @@ static void SCPerfReleaseCounter(SCPerfCounter *pc) if (pc != NULL) { if (pc->name != NULL) { if (pc->name->cname != NULL) - free(pc->name->cname); + SCFree(pc->name->cname); if (pc->name->tm_name != NULL) - free(pc->name->tm_name); + SCFree(pc->name->tm_name); - free(pc->name); + SCFree(pc->name); } if (pc->value != NULL) { if (pc->value->cvalue != NULL) - free(pc->value->cvalue); + SCFree(pc->value->cvalue); - free(pc->value); + SCFree(pc->value); } if (pc->desc != NULL) - free(pc->desc); + SCFree(pc->desc); if (pc->type_q != NULL) - free(pc->type_q); + SCFree(pc->type_q); - free(pc); + SCFree(pc); } return; @@ -424,33 +424,33 @@ static uint16_t SCPerfRegisterQualifiedCounter(char *cname, char *tm_name, return(temp->id); /* if we reach this point we don't have a counter registered by this cname */ - if ( (pc = malloc(sizeof(SCPerfCounter))) == NULL) { + if ( (pc = SCMalloc(sizeof(SCPerfCounter))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } memset(pc, 0, sizeof(SCPerfCounter)); - if ( (pc->name = malloc(sizeof(SCPerfCounterName))) == NULL) { + if ( (pc->name = SCMalloc(sizeof(SCPerfCounterName))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); - free(pc); + SCFree(pc); exit(EXIT_FAILURE); } memset(pc->name, 0, sizeof(SCPerfCounterName)); - if ( (pc->value = malloc(sizeof(SCPerfCounterValue))) == NULL) { + if ( (pc->value = SCMalloc(sizeof(SCPerfCounterValue))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); - free(pc->name); - free(pc); + SCFree(pc->name); + SCFree(pc); exit(EXIT_FAILURE); } memset(pc->value, 0, sizeof(SCPerfCounterValue)); - if ( (pc->name->cname = strdup(cname)) == NULL) { + if ( (pc->name->cname = SCStrdup(cname)) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } - if ( (pc->name->tm_name = strdup(tm_name)) == NULL) { + if ( (pc->name->tm_name = SCStrdup(tm_name)) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } @@ -459,12 +459,12 @@ static uint16_t SCPerfRegisterQualifiedCounter(char *cname, char *tm_name, * PerfContext. Please note that the id start from 1, and not 0 */ pc->id = ++(pctx->curr_id); - if (desc != NULL && (pc->desc = strdup(desc)) == NULL) { + if (desc != NULL && (pc->desc = SCStrdup(desc)) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } - if ( (pc->type_q = malloc(sizeof(SCPerfCounterTypeQ))) == NULL) { + if ( (pc->type_q = SCMalloc(sizeof(SCPerfCounterTypeQ))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } @@ -495,7 +495,7 @@ static uint16_t SCPerfRegisterQualifiedCounter(char *cname, char *tm_name, break; } - if ( (pc->value->cvalue = malloc(pc->value->size)) == NULL) { + if ( (pc->value->cvalue = SCMalloc(pc->value->size)) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } @@ -748,7 +748,7 @@ static int SCPerfOutputCounterFileIface() pctmi = sc_perf_op_ctx->pctmi; while (pctmi != NULL) { - if ( (pc_heads = malloc(pctmi->size * sizeof(SCPerfCounter *))) == NULL) { + if ( (pc_heads = SCMalloc(pctmi->size * sizeof(SCPerfCounter *))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } @@ -812,7 +812,7 @@ static int SCPerfOutputCounterFileIface() pctmi = pctmi->next; - free(pc_heads); + SCFree(pc_heads); fflush(sc_perf_op_ctx->fp); } @@ -1113,16 +1113,16 @@ int SCPerfAddToClubbedTMTable(char *tm_name, SCPerfContext *pctx) /* get me the bugger who wrote this junk of a code :P */ if (pctmi == NULL) { - if ( (temp = malloc(sizeof(SCPerfClubTMInst))) == NULL) { + if ( (temp = SCMalloc(sizeof(SCPerfClubTMInst))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(0); } memset(temp, 0, sizeof(SCPerfClubTMInst)); temp->size++; - temp->head = realloc(temp->head, temp->size * sizeof(SCPerfContext **)); + temp->head = SCRealloc(temp->head, temp->size * sizeof(SCPerfContext **)); temp->head[0] = pctx; - temp->tm_name = strdup(tm_name); + temp->tm_name = SCStrdup(tm_name); if (prev == NULL) sc_perf_op_ctx->pctmi = temp; @@ -1142,7 +1142,7 @@ int SCPerfAddToClubbedTMTable(char *tm_name, SCPerfContext *pctx) return 1; } - pctmi->head = realloc(pctmi->head, + pctmi->head = SCRealloc(pctmi->head, (pctmi->size + 1) * sizeof(SCPerfContext **)); hpctx = pctmi->head; @@ -1193,13 +1193,13 @@ SCPerfCounterArray *SCPerfGetCounterArrayRange(uint16_t s_id, uint16_t e_id, return NULL; } - if ( (pca = malloc(sizeof(SCPerfCounterArray))) == NULL) { + if ( (pca = SCMalloc(sizeof(SCPerfCounterArray))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } memset(pca, 0, sizeof(SCPerfCounterArray)); - if ( (pca->head = malloc(sizeof(SCPCAElem) * (e_id - s_id + 2))) == NULL) { + if ( (pca->head = SCMalloc(sizeof(SCPCAElem) * (e_id - s_id + 2))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } @@ -1598,9 +1598,9 @@ void SCPerfReleasePCA(SCPerfCounterArray *pca) { if (pca != NULL) { if (pca->head != NULL) - free(pca->head); + SCFree(pca->head); - free(pca); + SCFree(pca); } return; diff --git a/src/decode-raw.c b/src/decode-raw.c index 190a29348b..566f441da2 100644 --- a/src/decode-raw.c +++ b/src/decode-raw.c @@ -73,9 +73,11 @@ static int DecodeRawTest01 (void) { DecodeRaw(&tv, &dtv, &p, raw_ip, p.pktlen, NULL); if (p.ip6h == NULL) { printf("expected a valid ipv6 header but it was NULL: "); + FlowShutdown(); return 1; } + FlowShutdown(); return 0; } @@ -108,6 +110,7 @@ static int DecodeRawTest02 (void) { FlowInitConfig(FLOW_QUIET); DecodeRaw(&tv, &dtv, &p, raw_ip, p.pktlen, NULL); + FlowShutdown(); if (p.ip4h == NULL) { printf("expected a valid ipv4 header but it was NULL: "); return 1; @@ -147,10 +150,12 @@ static int DecodeRawTest03 (void) { DecodeRaw(&tv, &dtv, &p, raw_ip, p.pktlen, NULL); if (DECODER_ISSET_EVENT(&p,IPRAW_INVALID_IPV)) { + FlowShutdown(); return 0; } else { printf("expected IPRAW_INVALID_IPV to be set but it wasn't: "); } + FlowShutdown(); return 1; } diff --git a/src/defrag.c b/src/defrag.c index 38ac1c6049..8e27606b50 100644 --- a/src/defrag.c +++ b/src/defrag.c @@ -260,7 +260,7 @@ DefragFragReset(Frag *frag) DefragContext *dc = frag->dc; if (frag->pkt != NULL) - free(frag->pkt); + SCFree(frag->pkt); memset(frag, 0, sizeof(*frag)); frag->dc = dc; } @@ -274,7 +274,7 @@ DefragFragNew(void *arg) DefragContext *dc = arg; Frag *frag; - frag = calloc(1, sizeof(*frag)); + frag = SCCalloc(1, sizeof(*frag)); frag->dc = dc; return (void *)frag; @@ -287,7 +287,7 @@ static void DefragFragFree(void *arg) { Frag *frag = arg; - free(frag); + SCFree(frag); } /** @@ -304,7 +304,7 @@ DefragTrackerFreeFrags(DefragTracker *tracker) while ((frag = TAILQ_FIRST(&tracker->frags)) != NULL) { TAILQ_REMOVE(&tracker->frags, frag, next); - /* Don't free the frag, just give it back to its pool. */ + /* Don't SCFree the frag, just give it back to its pool. */ DefragFragReset(frag); PoolReturn(frag->dc->frag_pool, frag); } @@ -343,11 +343,11 @@ DefragTrackerNew(void *arg) DefragContext *dc = arg; DefragTracker *tracker; - tracker = calloc(1, sizeof(*tracker)); + tracker = SCCalloc(1, sizeof(*tracker)); if (tracker == NULL) return NULL; if (SCMutexInit(&tracker->lock, NULL) != 0) { - free(tracker); + SCFree(tracker); return NULL; } tracker->dc = dc; @@ -367,7 +367,7 @@ DefragTrackerFree(void *arg) SCMutexDestroy(&tracker->lock); DefragTrackerFreeFrags(tracker); - free(tracker); + SCFree(tracker); } /** @@ -381,7 +381,7 @@ DefragContextNew(void) { DefragContext *dc; - dc = calloc(1, sizeof(*dc)); + dc = SCCalloc(1, sizeof(*dc)); if (dc == NULL) return NULL; @@ -471,10 +471,9 @@ DefragContextDestroy(DefragContext *dc) HashListTableFree(dc->frag_table); PoolFree(dc->frag_pool); PoolFree(dc->tracker_pool); - free(dc); + SCFree(dc); } - /** * Attempt to re-assemble a packet. * @@ -517,7 +516,7 @@ Defrag4Reassemble(ThreadVars *tv, DefragContext *dc, DefragTracker *tracker, } /* Allocate a Packet for the reassembled packet. On failure we - * free all the resources held by this tracker. */ + * SCFree all the resources held by this tracker. */ if (tv == NULL) { /* Unit test. */ rp = SetupPkt(); @@ -635,7 +634,7 @@ Defrag6Reassemble(ThreadVars *tv, DefragContext *dc, DefragTracker *tracker, } /* Allocate a Packet for the reassembled packet. On failure we - * free all the resources held by this tracker. */ + * SCFree all the resources held by this tracker. */ if (tv == NULL) { /* Unit test. */ rp = SetupPkt(); @@ -888,7 +887,7 @@ insert: if (new == NULL) { goto done; } - new->pkt = malloc(p->pktlen); + new->pkt = SCMalloc(p->pktlen); if (new->pkt == NULL) { SCMutexLock(&dc->frag_pool_lock); PoolReturn(dc->frag_pool, new); @@ -1143,7 +1142,7 @@ BuildTestPacket(uint16_t id, uint16_t off, int mf, const char content, int hlen = 20; int ttl = 64; - p = calloc(1, sizeof(*p)); + p = SCCalloc(1, sizeof(*p)); if (p == NULL) return NULL; gettimeofday(&p->ts, NULL); @@ -1191,7 +1190,7 @@ BuildTestPacket(uint16_t id, uint16_t off, int mf, const char content, return p; error: if (p != NULL) - free(p); + SCFree(p); return NULL; } @@ -1201,7 +1200,7 @@ IPV6BuildTestPacket(uint32_t id, uint16_t off, int mf, const char content, { Packet *p = NULL; - p = calloc(1, sizeof(*p)); + p = SCCalloc(1, sizeof(*p)); if (p == NULL) return NULL; gettimeofday(&p->ts, NULL); @@ -1249,7 +1248,7 @@ IPV6BuildTestPacket(uint32_t id, uint16_t off, int mf, const char content, error: fprintf(stderr, "Error building test packet.\n"); if (p != NULL) - free(p); + SCFree(p); return NULL; } @@ -1321,13 +1320,13 @@ end: if (dc != NULL) DefragContextDestroy(dc); if (p1 != NULL) - free(p1); + SCFree(p1); if (p2 != NULL) - free(p2); + SCFree(p2); if (p3 != NULL) - free(p3); + SCFree(p3); if (reassembled != NULL) - free(reassembled); + SCFree(reassembled); DefragDestroy(); return ret; @@ -1400,13 +1399,13 @@ end: if (dc != NULL) DefragContextDestroy(dc); if (p1 != NULL) - free(p1); + SCFree(p1); if (p2 != NULL) - free(p2); + SCFree(p2); if (p3 != NULL) - free(p3); + SCFree(p3); if (reassembled != NULL) - free(reassembled); + SCFree(reassembled); DefragDestroy(); return ret; @@ -1476,13 +1475,13 @@ end: if (dc != NULL) DefragContextDestroy(dc); if (p1 != NULL) - free(p1); + SCFree(p1); if (p2 != NULL) - free(p2); + SCFree(p2); if (p3 != NULL) - free(p3); + SCFree(p3); if (reassembled != NULL) - free(reassembled); + SCFree(reassembled); DefragDestroy(); return ret; @@ -1545,13 +1544,13 @@ end: if (dc != NULL) DefragContextDestroy(dc); if (p1 != NULL) - free(p1); + SCFree(p1); if (p2 != NULL) - free(p2); + SCFree(p2); if (p3 != NULL) - free(p3); + SCFree(p3); if (reassembled != NULL) - free(reassembled); + SCFree(reassembled); DefragDestroy(); return ret; @@ -1642,7 +1641,7 @@ DefragDoSturgesNovakTest(int policy, u_char *expected, size_t expected_len) for (i = 0; i < 16; i++) { Packet *tp = Defrag(NULL, NULL, dc, packets[i]); if (tp != NULL) { - free(tp); + SCFree(tp); goto end; } } @@ -1659,7 +1658,7 @@ DefragDoSturgesNovakTest(int policy, u_char *expected, size_t expected_len) if (memcmp(reassembled->pkt + 20, expected, expected_len) != 0) goto end; - free(reassembled); + SCFree(reassembled); /* Make sure the tracker was released back to the pool. */ if (dc->tracker_pool->outstanding != 0) @@ -1674,7 +1673,7 @@ end: if (dc != NULL) DefragContextDestroy(dc); for (i = 0; i < 17; i++) { - free(packets[i]); + SCFree(packets[i]); } DefragDestroy(); return ret; @@ -1765,82 +1764,82 @@ IPV6DefragDoSturgesNovakTest(int policy, u_char *expected, size_t expected_len) Packet *tp; tp = Defrag(NULL, NULL, dc, packets[0]); if (tp != NULL) { - free(tp); + SCFree(tp); goto end; } tp = Defrag(NULL, NULL, dc, packets[1]); if (tp != NULL) { - free(tp); + SCFree(tp); goto end; } tp = Defrag(NULL, NULL, dc, packets[2]); if (tp != NULL) { - free(tp); + SCFree(tp); goto end; } tp = Defrag(NULL, NULL, dc, packets[3]); if (tp != NULL) { - free(tp); + SCFree(tp); goto end; } tp = Defrag(NULL, NULL, dc, packets[4]); if (tp != NULL) { - free(tp); + SCFree(tp); goto end; } tp = Defrag(NULL, NULL, dc, packets[5]); if (tp != NULL) { - free(tp); + SCFree(tp); goto end; } tp = Defrag(NULL, NULL, dc, packets[6]); if (tp != NULL) { - free(tp); + SCFree(tp); goto end; } tp = Defrag(NULL, NULL, dc, packets[7]); if (tp != NULL) { - free(tp); + SCFree(tp); goto end; } tp = Defrag(NULL, NULL, dc, packets[8]); if (tp != NULL) { - free(tp); + SCFree(tp); goto end; } tp = Defrag(NULL, NULL, dc, packets[9]); if (tp != NULL) { - free(tp); + SCFree(tp); goto end; } tp = Defrag(NULL, NULL, dc, packets[10]); if (tp != NULL) { - free(tp); + SCFree(tp); goto end; } tp = Defrag(NULL, NULL, dc, packets[11]); if (tp != NULL) { - free(tp); + SCFree(tp); goto end; } tp = Defrag(NULL, NULL, dc, packets[12]); if (tp != NULL) { - free(tp); + SCFree(tp); goto end; } tp = Defrag(NULL, NULL, dc, packets[13]); if (tp != NULL) { - free(tp); + SCFree(tp); goto end; } tp = Defrag(NULL, NULL, dc, packets[14]); if (tp != NULL) { - free(tp); + SCFree(tp); goto end; } tp = Defrag(NULL, NULL, dc, packets[15]); if (tp != NULL) { - free(tp); + SCFree(tp); goto end; } @@ -1854,7 +1853,7 @@ IPV6DefragDoSturgesNovakTest(int policy, u_char *expected, size_t expected_len) if (IPV6_GET_PLEN(reassembled) != 192) goto end; - free(reassembled); + SCFree(reassembled); /* Make sure the tracker was released back to the pool. */ if (dc->tracker_pool->outstanding != 0) @@ -1869,7 +1868,7 @@ end: if (dc != NULL) DefragContextDestroy(dc); for (i = 0; i < 17; i++) { - free(packets[i]); + SCFree(packets[i]); } DefragDestroy(); return ret; @@ -2312,10 +2311,10 @@ DefragTimeoutTest(void) Packet *tp = Defrag(NULL, NULL, dc, p); - free(p); + SCFree(p); if (tp != NULL) { - free(tp); + SCFree(tp); goto end; } } @@ -2329,10 +2328,10 @@ DefragTimeoutTest(void) p->ts.tv_sec += dc->timeout; Packet *tp = Defrag(NULL, NULL, dc, p); - free(p); + SCFree(p); if (tp != NULL) { - free(tp); + SCFree(tp); goto end; } @@ -2402,7 +2401,7 @@ end: if (dc != NULL) DefragContextDestroy(dc); if (p != NULL) - free(p); + SCFree(p); DefragDestroy(); return ret; @@ -2441,7 +2440,7 @@ end: if (dc != NULL) DefragContextDestroy(dc); if (p != NULL) - free(p); + SCFree(p); DefragDestroy(); return ret; diff --git a/src/detect-ack.c b/src/detect-ack.c index c34a0839e8..e6f9abb871 100644 --- a/src/detect-ack.c +++ b/src/detect-ack.c @@ -83,7 +83,7 @@ static int DetectAckSetup(DetectEngineCtx *de_ctx, Signature *s, //printf("DetectAckSetup: \'%s\'\n", optstr); - data = malloc(sizeof(DetectAckData)); + data = SCMalloc(sizeof(DetectAckData)); if (data == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "malloc failed"); goto error; @@ -106,7 +106,7 @@ static int DetectAckSetup(DetectEngineCtx *de_ctx, Signature *s, return 0; error: - if (data) free(data); + if (data) SCFree(data); return -1; } @@ -120,7 +120,7 @@ error: static void DetectAckFree(void *ptr) { DetectAckData *data = (DetectAckData *)ptr; - free(data); + SCFree(data); } diff --git a/src/detect-bytejump.c b/src/detect-bytejump.c index 4e48016472..10874f1a20 100644 --- a/src/detect-bytejump.c +++ b/src/detect-bytejump.c @@ -247,7 +247,7 @@ DetectBytejumpData *DetectBytejumpParse(char *optstr) } /* Initialize the data */ - data = malloc(sizeof(DetectBytejumpData)); + data = SCMalloc(sizeof(DetectBytejumpData)); if (data == NULL) { SCLogError(SC_ERR_MEM_ALLOC,"malloc failed %s", strerror(errno)); goto error; @@ -351,14 +351,14 @@ DetectBytejumpData *DetectBytejumpParse(char *optstr) for (i = 0; i < numargs; i++){ if (i == 1) continue; /* args[1] is part of args[0] */ - if (args[i] != NULL) free(args[i]); + if (args[i] != NULL) SCFree(args[i]); } return data; error: for (i = 0; i < numargs; i++){ if (i == 1) continue; /* args[1] is part of args[0] */ - if (args[i] != NULL) free(args[i]); + if (args[i] != NULL) SCFree(args[i]); } if (data != NULL) DetectBytejumpFree(data); return NULL; @@ -388,7 +388,7 @@ int DetectBytejumpSetup(DetectEngineCtx *de_ctx, Signature *s, error: if (data != NULL) DetectBytejumpFree(data); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); return -1; } @@ -400,7 +400,7 @@ error: void DetectBytejumpFree(void *ptr) { DetectBytejumpData *data = (DetectBytejumpData *)ptr; - free(data); + SCFree(data); } diff --git a/src/detect-bytetest.c b/src/detect-bytetest.c index 7845e943df..62728c12a4 100644 --- a/src/detect-bytetest.c +++ b/src/detect-bytetest.c @@ -226,9 +226,9 @@ DetectBytetestData *DetectBytetestParse(char *optstr) } /* Initialize the data */ - data = malloc(sizeof(DetectBytetestData)); + data = SCMalloc(sizeof(DetectBytetestData)); if (data == NULL) { - SCLogError(SC_ERR_MEM_ALLOC, "malloc failed"); + SCLogError(SC_ERR_MEM_ALLOC, "SCMalloc failed"); goto error; } data->base = DETECT_BYTETEST_BASE_UNSET; @@ -335,13 +335,13 @@ DetectBytetestData *DetectBytetestParse(char *optstr) data->nbytes = (uint8_t)nbytes; for (i = 0; i < (ret - 1); i++){ - if (args[i] != NULL) free(args[i]); + if (args[i] != NULL) SCFree(args[i]); } return data; error: for (i = 0; i < (ret - 1); i++){ - if (args[i] != NULL) free(args[i]); + if (args[i] != NULL) SCFree(args[i]); } if (data != NULL) DetectBytetestFree(data); return NULL; @@ -371,7 +371,7 @@ int DetectBytetestSetup(DetectEngineCtx *de_ctx, Signature *s, error: if (data != NULL) DetectBytetestFree(data); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); return -1; } @@ -383,7 +383,7 @@ error: void DetectBytetestFree(void *ptr) { DetectBytetestData *data = (DetectBytetestData *)ptr; - free(data); + SCFree(data); } diff --git a/src/detect-classtype.c b/src/detect-classtype.c index 0fd380adca..685fd96115 100644 --- a/src/detect-classtype.c +++ b/src/detect-classtype.c @@ -74,7 +74,7 @@ static inline const char *DetectClasstypeParseRawString(char *rawstr) /* get rid of the double quotes if present */ if (rawstr[0] == '\"' && rawstr[strlen(rawstr) - 1] == '\"') { - if ( (rawstr = strdup(rawstr + 1)) == NULL) { + if ( (rawstr = SCStrdup(rawstr + 1)) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); goto end; } diff --git a/src/detect-content.c b/src/detect-content.c index dc99dbbdf2..c924679f73 100644 --- a/src/detect-content.c +++ b/src/detect-content.c @@ -587,19 +587,19 @@ DetectContentData *DetectContentParse (char *contentstr) uint16_t pos = 0; uint16_t slen = 0; - if ((temp = strdup(contentstr)) == NULL) { + if ((temp = SCStrdup(contentstr)) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); goto error; } if (strlen(temp) == 0) { - free(temp); + SCFree(temp); return NULL; } - cd = malloc(sizeof(DetectContentData)); + cd = SCMalloc(sizeof(DetectContentData)); if (cd == NULL) { - SCLogError(SC_ERR_MEM_ALLOC, "malloc failed"); + SCLogError(SC_ERR_MEM_ALLOC, "SCMalloc failed"); goto error; } memset(cd, 0, sizeof(DetectContentData)); @@ -611,22 +611,22 @@ DetectContentData *DetectContentParse (char *contentstr) }; if (temp[pos] == '!') { - free(temp); - if ((temp = strdup(contentstr + pos + 1)) == NULL) + SCFree(temp); + if ((temp = SCStrdup(contentstr + pos + 1)) == NULL) goto error; cd->negated = 1; } if (temp[pos] == '\"' && temp[strlen(temp)-1] == '\"') { - if ((str = strdup(temp + pos + 1)) == NULL) + if ((str = SCStrdup(temp + pos + 1)) == NULL) goto error; str[strlen(temp) - pos - 2] = '\0'; } else { - if ((str = strdup(temp + pos)) == NULL) + if ((str = SCStrdup(temp + pos)) == NULL) goto error; } - free(temp); + SCFree(temp); temp = NULL; /*This was submitted as a patch for bug #11. But this impliments incorrect behavior as ! @@ -638,10 +638,10 @@ DetectContentData *DetectContentParse (char *contentstr) // goto error; // } else { // temp = str; - // if ( (str = strdup(temp + 1)) == NULL) + // if ( (str = SCStrdup(temp + 1)) == NULL) // goto error; // cd->negated = 1; - // free(temp); + // SCFree(temp); // temp = NULL; // } //} @@ -730,7 +730,7 @@ DetectContentData *DetectContentParse (char *contentstr) } } - cd->content = malloc(len); + cd->content = SCMalloc(len); if (cd->content == NULL) goto error; @@ -742,16 +742,16 @@ DetectContentData *DetectContentParse (char *contentstr) cd->distance = 0; cd->flags = 0; - free(str); + SCFree(str); return cd; error: - free(str); - free(temp); + SCFree(str); + SCFree(temp); if (cd != NULL) { if (cd->content != NULL) - free(cd->content); - free(cd); + SCFree(cd->content); + SCFree(cd); } return NULL; } @@ -766,7 +766,7 @@ void DetectContentPrint(DetectContentData *cd) SCLogDebug("DetectContentData \"cd\" is NULL"); return; } - char *tmpstr=malloc(sizeof(char) * cd->content_len + 1); + char *tmpstr=SCMalloc(sizeof(char) * cd->content_len + 1); if (tmpstr != NULL) { for (i = 0; i < cd->content_len; i++) { @@ -777,7 +777,7 @@ void DetectContentPrint(DetectContentData *cd) } tmpstr[i] = '\0'; SCLogDebug("Content: \"%s\"", tmpstr); - free(tmpstr); + SCFree(tmpstr); } else { SCLogDebug("Content: "); for (i = 0; i < cd->content_len; i++) @@ -818,9 +818,9 @@ DetectContentData *DetectContentSplitChunk(DetectContentData *origcd, uint8_t remaining_content_len, uint8_t index, int32_t mpl) { - DetectContentData *cd = malloc(sizeof(DetectContentData)); + DetectContentData *cd = SCMalloc(sizeof(DetectContentData)); if (cd == NULL) { - SCLogError(SC_ERR_MEM_ALLOC, "DetectContentData malloc failed"); + SCLogError(SC_ERR_MEM_ALLOC, "DetectContentData SCMalloc failed"); goto error; } memset(cd,0,sizeof(DetectContentData)); @@ -834,9 +834,9 @@ DetectContentData *DetectContentSplitChunk(DetectContentData *origcd, if (cd->content_len <= 0) goto error; - cd->content = (uint8_t*) malloc(sizeof(uint8_t) * cd->content_len); + cd->content = (uint8_t*) SCMalloc(sizeof(uint8_t) * cd->content_len); if (cd->content == NULL) { - SCLogError(SC_ERR_MEM_ALLOC, "string for content malloc failed"); + SCLogError(SC_ERR_MEM_ALLOC, "string for content SCMalloc failed"); goto error; } @@ -847,8 +847,8 @@ DetectContentData *DetectContentSplitChunk(DetectContentData *origcd, error: if (cd != NULL) { if (cd->content != NULL) - free(cd->content); - free(cd); + SCFree(cd->content); + SCFree(cd); } return NULL; } @@ -1519,12 +1519,12 @@ int DetectContentSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char error: if (cd != NULL) DetectContentFree(cd); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); return -1; } /** - * \brief this function will free memory associated with DetectContentData + * \brief this function will SCFree memory associated with DetectContentData * * \param cd pointer to DetectCotentData */ @@ -1535,9 +1535,9 @@ void DetectContentFree(void *ptr) { return; if (cd->content != NULL) - free(cd->content); + SCFree(cd->content); - free(cd); + SCFree(cd); } #ifdef UNITTESTS /* UNITTESTS */ @@ -2757,6 +2757,13 @@ static int SigTestPositiveTestContent(char *rule, uint8_t *buf) //PatternMatchDestroy(mpm_ctx); DetectEngineCtxFree(de_ctx); end: + SigGroupCleanup(de_ctx); + SigCleanSignatures(de_ctx); + + DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx); + //PatternMatchDestroy(mpm_ctx); + DetectEngineCtxFree(de_ctx); + return result; } @@ -2805,6 +2812,12 @@ static int SigTestNegativeTestContent(char *rule, uint8_t *buf) //PatternMatchDestroy(mpm_ctx); DetectEngineCtxFree(de_ctx); end: + SigGroupCleanup(de_ctx); + SigCleanSignatures(de_ctx); + + DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx); + //PatternMatchDestroy(mpm_ctx); + DetectEngineCtxFree(de_ctx); return result; } diff --git a/src/detect-csum.c b/src/detect-csum.c index b20f87a77a..d1117386fb 100644 --- a/src/detect-csum.c +++ b/src/detect-csum.c @@ -163,19 +163,19 @@ static int DetectCsumParseArg(const char *key, DetectCsumData *cd) char *str; if (key[0] == '\"' && key[strlen(key) - 1] == '\"') { - str = strdup(key + 1); + str = SCStrdup(key + 1); str[strlen(key) - 2] = '\0'; } else - str = strdup(key); + str = SCStrdup(key); if (strcasecmp(str, DETECT_CSUM_VALID) == 0 || strcasecmp(str, DETECT_CSUM_INVALID) == 0) { cd->valid = (strcasecmp(key, DETECT_CSUM_VALID) == 0); - free(str); + SCFree(str); return 1; } - free(str); + SCFree(str); return 0; } @@ -242,7 +242,7 @@ int DetectIPV4CsumSetup(DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, sm->type = DETECT_IPV4_CSUM; - if ( (cd = malloc(sizeof(DetectCsumData))) == NULL) { + if ( (cd = SCMalloc(sizeof(DetectCsumData))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); goto error; } @@ -259,7 +259,7 @@ int DetectIPV4CsumSetup(DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, error: if (cd != NULL) DetectIPV4CsumFree(cd); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); return -1; } @@ -269,7 +269,7 @@ void DetectIPV4CsumFree(void *ptr) DetectCsumData *cd = (DetectCsumData *)ptr; if (cd != NULL) - free(cd); + SCFree(cd); return; } @@ -339,7 +339,7 @@ int DetectTCPV4CsumSetup(DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, sm->type = DETECT_TCPV4_CSUM; - if ( (cd = malloc(sizeof(DetectCsumData))) == NULL) { + if ( (cd = SCMalloc(sizeof(DetectCsumData))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); goto error; } @@ -356,7 +356,7 @@ int DetectTCPV4CsumSetup(DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, error: if (cd != NULL) DetectTCPV4CsumFree(cd); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); return -1; } @@ -366,7 +366,7 @@ void DetectTCPV4CsumFree(void *ptr) DetectCsumData *cd = (DetectCsumData *)ptr; if (cd != NULL) - free(cd); + SCFree(cd); return; } @@ -436,7 +436,7 @@ int DetectTCPV6CsumSetup(DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, sm->type = DETECT_TCPV6_CSUM; - if ( (cd = malloc(sizeof(DetectCsumData))) == NULL) { + if ( (cd = SCMalloc(sizeof(DetectCsumData))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); goto error; } @@ -453,7 +453,7 @@ int DetectTCPV6CsumSetup(DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, error: if (cd != NULL) DetectTCPV6CsumFree(cd); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); return -1; } @@ -463,7 +463,7 @@ void DetectTCPV6CsumFree(void *ptr) DetectCsumData *cd = (DetectCsumData *)ptr; if (cd != NULL) - free(cd); + SCFree(cd); return; } @@ -533,7 +533,7 @@ int DetectUDPV4CsumSetup(DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, sm->type = DETECT_UDPV4_CSUM; - if ( (cd = malloc(sizeof(DetectCsumData))) == NULL) { + if ( (cd = SCMalloc(sizeof(DetectCsumData))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); goto error; } @@ -550,7 +550,7 @@ int DetectUDPV4CsumSetup(DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, error: if (cd != NULL) DetectUDPV4CsumFree(cd); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); return -1; } @@ -560,7 +560,7 @@ void DetectUDPV4CsumFree(void *ptr) DetectCsumData *cd = (DetectCsumData *)ptr; if (cd != NULL) - free(cd); + SCFree(cd); return; } @@ -630,7 +630,7 @@ int DetectUDPV6CsumSetup(DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, sm->type = DETECT_UDPV6_CSUM; - if ( (cd = malloc(sizeof(DetectCsumData))) == NULL) { + if ( (cd = SCMalloc(sizeof(DetectCsumData))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); goto error; } @@ -647,7 +647,7 @@ int DetectUDPV6CsumSetup(DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, error: if (cd != NULL) DetectUDPV6CsumFree(cd); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); return -1; } @@ -657,7 +657,7 @@ void DetectUDPV6CsumFree(void *ptr) DetectCsumData *cd = (DetectCsumData *)ptr; if (cd != NULL) - free(cd); + SCFree(cd); return; } @@ -726,7 +726,7 @@ int DetectICMPV4CsumSetup(DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, sm->type = DETECT_ICMPV4_CSUM; - if ( (cd = malloc(sizeof(DetectCsumData))) == NULL) { + if ( (cd = SCMalloc(sizeof(DetectCsumData))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); goto error; } @@ -743,7 +743,7 @@ int DetectICMPV4CsumSetup(DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, error: if (cd != NULL) DetectICMPV4CsumFree(cd); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); return -1; } @@ -753,7 +753,7 @@ void DetectICMPV4CsumFree(void *ptr) DetectCsumData *cd = (DetectCsumData *)ptr; if (cd != NULL) - free(cd); + SCFree(cd); return; } @@ -822,7 +822,7 @@ int DetectICMPV6CsumSetup(DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, sm->type = DETECT_ICMPV6_CSUM; - if ( (cd = malloc(sizeof(DetectCsumData))) == NULL) { + if ( (cd = SCMalloc(sizeof(DetectCsumData))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); goto error; } @@ -839,7 +839,7 @@ int DetectICMPV6CsumSetup(DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, error: if (cd != NULL) DetectICMPV6CsumFree(cd); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); return -1; } @@ -849,7 +849,7 @@ void DetectICMPV6CsumFree(void *ptr) DetectCsumData *cd = (DetectCsumData *)ptr; if (cd != NULL) - free(cd); + SCFree(cd); return; } @@ -876,7 +876,7 @@ int DetectCsumIPV4ValidArgsTestParse01(void) DetectIPV4CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } return result; @@ -900,7 +900,7 @@ int DetectCsumIPV4InValidArgsTestParse02(void) DetectIPV4CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } return result; @@ -925,7 +925,7 @@ int DetectCsumIPV4ValidArgsTestParse03(void) DetectIPV4CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } s.match = NULL; @@ -939,7 +939,7 @@ int DetectCsumIPV4ValidArgsTestParse03(void) DetectIPV4CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } return result; @@ -963,7 +963,7 @@ int DetectCsumICMPV4ValidArgsTestParse01(void) DetectICMPV4CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } return result; @@ -986,7 +986,7 @@ int DetectCsumICMPV4InValidArgsTestParse02(void) { DetectICMPV4CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } return result; @@ -1010,7 +1010,7 @@ int DetectCsumICMPV4ValidArgsTestParse03(void) { DetectICMPV4CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } s.match = NULL; @@ -1024,7 +1024,7 @@ int DetectCsumICMPV4ValidArgsTestParse03(void) { DetectICMPV4CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } return result; @@ -1048,7 +1048,7 @@ int DetectCsumTCPV4ValidArgsTestParse01(void) DetectTCPV4CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } return result; @@ -1071,7 +1071,7 @@ int DetectCsumTCPV4InValidArgsTestParse02(void) { DetectTCPV4CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } return result; @@ -1095,7 +1095,7 @@ int DetectCsumTCPV4ValidArgsTestParse03(void) { DetectTCPV4CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } s.match = NULL; @@ -1109,7 +1109,7 @@ int DetectCsumTCPV4ValidArgsTestParse03(void) { DetectTCPV4CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } return result; @@ -1133,7 +1133,7 @@ int DetectCsumUDPV4ValidArgsTestParse01(void) DetectUDPV4CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } return result; @@ -1156,7 +1156,7 @@ int DetectCsumUDPV4InValidArgsTestParse02(void) { DetectUDPV4CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } return result; @@ -1180,7 +1180,7 @@ int DetectCsumUDPV4ValidArgsTestParse03(void) { DetectUDPV4CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } s.match = NULL; @@ -1194,7 +1194,7 @@ int DetectCsumUDPV4ValidArgsTestParse03(void) { DetectUDPV4CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } return result; @@ -1218,7 +1218,7 @@ int DetectCsumTCPV6ValidArgsTestParse01(void) DetectTCPV6CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } return result; @@ -1241,7 +1241,7 @@ int DetectCsumTCPV6InValidArgsTestParse02(void) { DetectTCPV6CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } return result; @@ -1265,7 +1265,7 @@ int DetectCsumTCPV6ValidArgsTestParse03(void) { DetectTCPV6CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } s.match = NULL; @@ -1279,7 +1279,7 @@ int DetectCsumTCPV6ValidArgsTestParse03(void) { DetectTCPV6CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } return result; @@ -1303,7 +1303,7 @@ int DetectCsumUDPV6ValidArgsTestParse01(void) DetectUDPV6CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } return result; @@ -1326,7 +1326,7 @@ int DetectCsumUDPV6InValidArgsTestParse02(void) { DetectUDPV6CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } return result; @@ -1350,7 +1350,7 @@ int DetectCsumUDPV6ValidArgsTestParse03(void) { DetectUDPV6CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } s.match = NULL; @@ -1364,7 +1364,7 @@ int DetectCsumUDPV6ValidArgsTestParse03(void) { DetectUDPV6CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } return result; @@ -1388,7 +1388,7 @@ int DetectCsumICMPV6ValidArgsTestParse01(void) DetectICMPV6CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } return result; @@ -1411,7 +1411,7 @@ int DetectCsumICMPV6InValidArgsTestParse02(void) { DetectICMPV6CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } return result; @@ -1435,7 +1435,7 @@ int DetectCsumICMPV6ValidArgsTestParse03(void) { DetectICMPV6CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } s.match = NULL; @@ -1449,7 +1449,7 @@ int DetectCsumICMPV6ValidArgsTestParse03(void) { DetectICMPV6CsumFree(s.match->ctx); temp = s.match; s.match = s.match->next; - free(temp); + SCFree(temp); } return result; diff --git a/src/detect-dce-iface.c b/src/detect-dce-iface.c index e49549893b..76b6c00b59 100644 --- a/src/detect-dce-iface.c +++ b/src/detect-dce-iface.c @@ -98,7 +98,7 @@ static inline DetectDceIfaceData *DetectDceIfaceArgParse(const char *arg) goto error; } - if ( (did = malloc(sizeof(DetectDceIfaceData))) == NULL) { + if ( (did = SCMalloc(sizeof(DetectDceIfaceData))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); goto error; } @@ -200,7 +200,7 @@ static inline DetectDceIfaceData *DetectDceIfaceArgParse(const char *arg) error: if (did != NULL) - free(did); + SCFree(did); return NULL; } @@ -342,13 +342,13 @@ int DetectDceIfaceSetup(DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, error: DetectDceIfaceFree(did); if (sm != NULL) - free(sm); + SCFree(sm); return -1; } void DetectDceIfaceFree(void *ptr) { - free(ptr); + SCFree(ptr); return; } diff --git a/src/detect-dce-opnum.c b/src/detect-dce-opnum.c index 3375270e8f..2d13ce2c05 100644 --- a/src/detect-dce-opnum.c +++ b/src/detect-dce-opnum.c @@ -79,7 +79,7 @@ static inline DetectDceOpnumRange *DetectDceOpnumAllocDetectDceOpnumRange(void) { DetectDceOpnumRange *dor = NULL; - if ( (dor = malloc(sizeof(DetectDceOpnumRange))) == NULL) { + if ( (dor = SCMalloc(sizeof(DetectDceOpnumRange))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } @@ -129,13 +129,13 @@ static inline DetectDceOpnumData *DetectDceOpnumArgParse(const char *arg) goto error; } - if ( (dod = malloc(sizeof(DetectDceOpnumData))) == NULL) { + if ( (dod = SCMalloc(sizeof(DetectDceOpnumData))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); goto error; } memset(dod, 0, sizeof(DetectDceOpnumData)); - if ( (dup_str = strdup(pcre_sub_str)) == NULL) { + if ( (dup_str = SCStrdup(pcre_sub_str)) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); goto error; } @@ -207,13 +207,13 @@ static inline DetectDceOpnumData *DetectDceOpnumArgParse(const char *arg) } if (dup_str_head != NULL) - free(dup_str_head); + SCFree(dup_str_head); return dod; error: if (dup_str_head != NULL) - free(dup_str_head); + SCFree(dup_str_head); DetectDceOpnumFree(dod); return NULL; } @@ -299,7 +299,7 @@ int DetectDceOpnumSetup(DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, error: DetectDceOpnumFree(dod); if (sm != NULL) - free(sm); + SCFree(sm); return -1; } @@ -315,9 +315,9 @@ void DetectDceOpnumFree(void *ptr) while (dor != NULL) { dor_temp = dor; dor = dor->next; - free(dor_temp); + SCFree(dor_temp); } - free(dod); + SCFree(dod); } return; diff --git a/src/detect-dce-stub-data.c b/src/detect-dce-stub-data.c index bde21a35dc..69abf96869 100644 --- a/src/detect-dce-stub-data.c +++ b/src/detect-dce-stub-data.c @@ -105,7 +105,7 @@ int DetectDceStubDataSetup(DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, error: if (sm != NULL) - free(sm); + SCFree(sm); return -1; } diff --git a/src/detect-decode-event.c b/src/detect-decode-event.c index 2ca2cc2ea7..db0e625fdf 100644 --- a/src/detect-decode-event.c +++ b/src/detect-decode-event.c @@ -125,7 +125,7 @@ DetectDecodeEventData *DetectDecodeEventParse (char *rawstr) if(found == 0) goto error; - de = malloc(sizeof(DetectDecodeEventData)); + de = SCMalloc(sizeof(DetectDecodeEventData)); if (de == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Malloc failed"); goto error; @@ -135,7 +135,7 @@ DetectDecodeEventData *DetectDecodeEventParse (char *rawstr) return de; error: - if (de) free(de); + if (de) SCFree(de); return NULL; } @@ -170,8 +170,8 @@ int DetectDecodeEventSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, return 0; error: - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return -1; } @@ -181,7 +181,7 @@ error: * \param de pointer to DetectDecodeEventData */ void DetectDecodeEventFree(DetectDecodeEventData *de) { - if(de) free(de); + if(de) SCFree(de); } /* @@ -295,8 +295,8 @@ int DecodeEventTestParse06 (void) { return 1; error: - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 0; } #endif /* UNITTESTS */ diff --git a/src/detect-depth.c b/src/detect-depth.c index 0c6f8e4ae7..722206df93 100644 --- a/src/detect-depth.c +++ b/src/detect-depth.c @@ -29,7 +29,7 @@ int DetectDepthSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char * /* strip "'s */ if (depthstr[0] == '\"' && depthstr[strlen(depthstr)-1] == '\"') { - str = strdup(depthstr+1); + str = SCStrdup(depthstr+1); str[strlen(depthstr)-2] = '\0'; dubbed = 1; } @@ -39,14 +39,14 @@ int DetectDepthSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char * SigMatch *pm = DetectContentFindPrevApplicableSM(m); if (pm == NULL) { SCLogError(SC_ERR_DEPTH_MISSING_CONTENT, "depth needs a preceeding content option"); - if (dubbed) free(str); + if (dubbed) SCFree(str); return -1; } DetectContentData *cd = (DetectContentData *)pm->ctx; if (cd == NULL) { SCLogError(SC_ERR_INVALID_ARGUMENT, "invalid argument"); - if (dubbed) free(str); + if (dubbed) SCFree(str); return -1; } @@ -64,7 +64,7 @@ int DetectDepthSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char * //DetectContentPrint(cd); //printf("DetectDepthSetup: set depth %" PRIu32 " for previous content\n", cd->depth); - if (dubbed) free(str); + if (dubbed) SCFree(str); return 0; } diff --git a/src/detect-distance.c b/src/detect-distance.c index cb2529b063..e3dbac87f8 100644 --- a/src/detect-distance.c +++ b/src/detect-distance.c @@ -30,7 +30,7 @@ int DetectDistanceSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, cha /* strip "'s */ if (distancestr[0] == '\"' && distancestr[strlen(distancestr)-1] == '\"') { - str = strdup(distancestr+1); + str = SCStrdup(distancestr+1); str[strlen(distancestr)-2] = '\0'; dubbed = 1; } @@ -46,14 +46,14 @@ int DetectDistanceSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, cha pm = DetectContentFindPrevApplicableSM(m); if (pm == NULL || DetectContentHasPrevSMPattern(pm) == NULL) { SCLogError(SC_ERR_DISTANCE_MISSING_CONTENT, "distance needs two preceeding content options"); - if (dubbed) free(str); + if (dubbed) SCFree(str); return -1; } DetectContentData *cd = (DetectContentData *)pm->ctx; if (cd == NULL) { SCLogError(SC_ERR_RULE_KEYWORD_UNKNOWN, "Unknown previous keyword!"); - if (dubbed) free(str); + if (dubbed) SCFree(str); return -1; } @@ -88,10 +88,10 @@ int DetectDistanceSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, cha goto error; } - if (dubbed) free(str); + if (dubbed) SCFree(str); return 0; error: - if (dubbed) free(str); + if (dubbed) SCFree(str); return -1; } diff --git a/src/detect-dsize.c b/src/detect-dsize.c index 3403ad9217..d7e8c3a561 100644 --- a/src/detect-dsize.c +++ b/src/detect-dsize.c @@ -151,7 +151,7 @@ DetectDsizeData *DetectDsizeParse (char *rawstr) value2 = (char *)str_ptr; SCLogDebug("value2 \"%s\"", value2); - dd = malloc(sizeof(DetectDsizeData)); + dd = SCMalloc(sizeof(DetectDsizeData)); if (dd == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); goto error; @@ -197,18 +197,18 @@ DetectDsizeData *DetectDsizeParse (char *rawstr) SCLogDebug("dsize parsed succesfully dsize: %"PRIu16" dsize2: %"PRIu16"",dd->dsize,dd->dsize2); - free(value1); - free(value2); - free(mode); - free(range); + SCFree(value1); + SCFree(value2); + SCFree(mode); + SCFree(range); return dd; error: - if (dd) free(dd); - if (value1) free(value1); - if (value2) free(value2); - if (mode) free(mode); - if (range) free(range); + if (dd) SCFree(dd); + if (value1) SCFree(value1); + if (value2) SCFree(value2); + if (mode) SCFree(mode); + if (range) SCFree(range); return NULL; } @@ -255,8 +255,8 @@ int DetectDsizeSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char * return 0; error: - if (dd) free(dd); - if (sm) free(sm); + if (dd) SCFree(dd); + if (sm) SCFree(sm); return -1; } @@ -268,7 +268,7 @@ error: */ void DetectDsizeFree(void *de_ptr) { DetectDsizeData *dd = (DetectDsizeData *)de_ptr; - if(dd) free(dd); + if(dd) SCFree(dd); } /* diff --git a/src/detect-engine-address-ipv4.c b/src/detect-engine-address-ipv4.c index 7205650fef..3d7ffc5fad 100644 --- a/src/detect-engine-address-ipv4.c +++ b/src/detect-engine-address-ipv4.c @@ -1054,6 +1054,8 @@ static int DetectAddressIPv4IsCompleteIPSpace02(void) a->ip2[0] = in.s_addr; result &= (DetectAddressIsCompleteIPSpaceIPv4(a) == 0); + DetectAddressFree(a); + if ( (a = DetectAddressInit()) == NULL) goto error; diff --git a/src/detect-engine-address.c b/src/detect-engine-address.c index 40544ab6d1..ad4a770021 100644 --- a/src/detect-engine-address.c +++ b/src/detect-engine-address.c @@ -63,7 +63,7 @@ static uint32_t detect_address_group_head_free_cnt = 0; */ DetectAddress *DetectAddressInit(void) { - DetectAddress *ag = malloc(sizeof(DetectAddress)); + DetectAddress *ag = SCMalloc(sizeof(DetectAddress)); if (ag == NULL) return NULL; memset(ag, 0, sizeof(DetectAddress)); @@ -116,7 +116,7 @@ void DetectAddressFree(DetectAddress *ag) detect_address_group_memory -= sizeof(DetectAddress); detect_address_group_free_cnt++; #endif - free(ag); + SCFree(ag); return; } @@ -600,7 +600,7 @@ static void DetectAddressParseIPv6CIDR(int cidr, struct in6_addr *in6) */ static int DetectAddressParseString(DetectAddress *dd, char *str) { - char *ipdup = strdup(str); + char *ipdup = SCStrdup(str); char *ip = NULL; char *ip2 = NULL; char *mask = NULL; @@ -611,7 +611,7 @@ static int DetectAddressParseString(DetectAddress *dd, char *str) /* first handle 'any' */ if (strcasecmp(str, "any") == 0) { dd->flags |= ADDRESS_FLAG_ANY; - free(ipdup); + SCFree(ipdup); SCLogDebug("address is \'any\'"); @@ -756,7 +756,7 @@ static int DetectAddressParseString(DetectAddress *dd, char *str) } - free(ipdup); + SCFree(ipdup); BUG_ON(dd->family == 0); @@ -764,7 +764,7 @@ static int DetectAddressParseString(DetectAddress *dd, char *str) error: if (ipdup) - free(ipdup); + SCFree(ipdup); return -1; } @@ -963,7 +963,7 @@ int DetectAddressParse2(DetectAddressHead *gh, DetectAddressHead *ghn, char *s, goto error; temp_rule_var_address = rule_var_address; if ((negate + n_set) % 2) { - temp_rule_var_address = malloc(strlen(rule_var_address) + 3); + temp_rule_var_address = SCMalloc(strlen(rule_var_address) + 3); if (temp_rule_var_address == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); goto error; @@ -976,7 +976,7 @@ int DetectAddressParse2(DetectAddressHead *gh, DetectAddressHead *ghn, char *s, d_set = 0; n_set = 0; if (temp_rule_var_address != rule_var_address) - free(temp_rule_var_address); + SCFree(temp_rule_var_address); } else { address[x - 1] = '\0'; @@ -1003,7 +1003,7 @@ int DetectAddressParse2(DetectAddressHead *gh, DetectAddressHead *ghn, char *s, goto error; temp_rule_var_address = rule_var_address; if ((negate + n_set) % 2) { - temp_rule_var_address = malloc(strlen(rule_var_address) + 3); + temp_rule_var_address = SCMalloc(strlen(rule_var_address) + 3); if (temp_rule_var_address == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); goto error; @@ -1015,7 +1015,7 @@ int DetectAddressParse2(DetectAddressHead *gh, DetectAddressHead *ghn, char *s, (negate + n_set) % 2); d_set = 0; if (temp_rule_var_address != rule_var_address) - free(temp_rule_var_address); + SCFree(temp_rule_var_address); } else { if (!((negate + n_set) % 2)) { if (DetectAddressSetup(gh, address) < 0) @@ -1251,7 +1251,7 @@ error: */ DetectAddressHead *DetectAddressHeadInit(void) { - DetectAddressHead *gh = malloc(sizeof(DetectAddressHead)); + DetectAddressHead *gh = SCMalloc(sizeof(DetectAddressHead)); if (gh == NULL) return NULL; memset(gh, 0, sizeof(DetectAddressHead)); @@ -1301,7 +1301,7 @@ void DetectAddressHeadFree(DetectAddressHead *gh) { if (gh != NULL) { DetectAddressHeadCleanup(gh); - free(gh); + SCFree(gh); #ifdef DEBUG detect_address_group_head_free_cnt++; detect_address_group_head_memory -= sizeof(DetectAddressHead); diff --git a/src/detect-engine-iponly.c b/src/detect-engine-iponly.c index 733775525a..00d7ceea33 100644 --- a/src/detect-engine-iponly.c +++ b/src/detect-engine-iponly.c @@ -224,7 +224,7 @@ void IPOnlyInit(DetectEngineCtx *de_ctx, DetectEngineIPOnlyCtx *io_ctx) { io_ctx->ht24_dst = HashListTableInit(65536, IPOnlyHashFunc24, IPOnlyCompareFunc, IPOnlyFreeFunc); */ io_ctx->sig_init_size = DetectEngineGetMaxSigId(de_ctx) / 8 + 1; - if ( (io_ctx->sig_init_array = malloc(io_ctx->sig_init_size)) == NULL) { + if ( (io_ctx->sig_init_array = SCMalloc(io_ctx->sig_init_size)) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } @@ -240,7 +240,7 @@ void DetectEngineIPOnlyThreadInit(DetectEngineCtx *de_ctx, DetectEngineIPOnlyThr /* initialize the signature bitarray */ io_tctx->sig_match_size = de_ctx->io_ctx.max_idx / 8 + 1; - io_tctx->sig_match_array = malloc(io_tctx->sig_match_size); + io_tctx->sig_match_array = SCMalloc(io_tctx->sig_match_size); memset(io_tctx->sig_match_array, 0, io_tctx->sig_match_size); } @@ -270,7 +270,8 @@ void IPOnlyDeinit(DetectEngineCtx *de_ctx, DetectEngineIPOnlyCtx *io_ctx) { HashListTableFree(io_ctx->ht24_dst); io_ctx->ht24_dst = NULL; */ - free(io_ctx->sig_init_array); + if (io_ctx->sig_init_array) + SCFree(io_ctx->sig_init_array); io_ctx->sig_init_array = NULL; } @@ -282,7 +283,7 @@ void DetectEngineIPOnlyThreadDeinit(DetectEngineIPOnlyThreadCtx *io_tctx) { if (io_tctx->dst != NULL) { DetectAddressFree(io_tctx->dst); } - free(io_tctx->sig_match_array); + SCFree(io_tctx->sig_match_array); } } @@ -431,7 +432,7 @@ int IPOnlyBuildMatchArray(DetectEngineCtx *de_ctx, DetectEngineIPOnlyCtx *io_ctx } //printf("IPOnlyBuildMatchArray: sig_cnt %" PRIu32 "\n", io_ctx->sig_cnt); - io_ctx->match_array = malloc(io_ctx->sig_cnt * sizeof(uint32_t)); + io_ctx->match_array = SCMalloc(io_ctx->sig_cnt * sizeof(uint32_t)); if (io_ctx->match_array == NULL) return -1; diff --git a/src/detect-engine-mpm.c b/src/detect-engine-mpm.c index 1265971a70..56116b9aed 100644 --- a/src/detect-engine-mpm.c +++ b/src/detect-engine-mpm.c @@ -262,7 +262,7 @@ void PatternMatchDestroyGroup(SigGroupHead *sh) { !(sh->flags & SIG_GROUP_HEAD_MPM_COPY)) { SCLogDebug("destroying mpm_ctx %p (sh %p)", sh->mpm_ctx, sh); mpm_table[sh->mpm_ctx->mpm_type].DestroyCtx(sh->mpm_ctx); - free(sh->mpm_ctx); + SCFree(sh->mpm_ctx); /* ready for reuse */ sh->mpm_ctx = NULL; @@ -274,7 +274,7 @@ void PatternMatchDestroyGroup(SigGroupHead *sh) { !(sh->flags & SIG_GROUP_HEAD_MPM_URI_COPY)) { SCLogDebug("destroying mpm_uri_ctx %p (sh %p)", sh->mpm_uri_ctx, sh); mpm_table[sh->mpm_uri_ctx->mpm_type].DestroyCtx(sh->mpm_uri_ctx); - free(sh->mpm_uri_ctx); + SCFree(sh->mpm_uri_ctx); /* ready for reuse */ sh->mpm_uri_ctx = NULL; @@ -352,7 +352,7 @@ char ContentHashCompareFunc(void *data1, uint16_t len1, void *data2, uint16_t le } ContentHash *ContentHashAlloc(DetectContentData *ptr) { - ContentHash *ch = malloc(sizeof(ContentHash)); + ContentHash *ch = SCMalloc(sizeof(ContentHash)); if (ch == NULL) return NULL; @@ -365,7 +365,7 @@ ContentHash *ContentHashAlloc(DetectContentData *ptr) { } void ContentHashFree(void *ch) { - free(ch); + SCFree(ch); } /** \brief Predict a strength value for patterns @@ -405,7 +405,7 @@ static int PatternMatchPreprarePopulateMpm(DetectEngineCtx *de_ctx, SigGroupHead uint32_t sig; uint32_t *fast_pattern = NULL; - fast_pattern = (uint32_t *)malloc(sgh->sig_cnt * sizeof(uint32_t)); + fast_pattern = (uint32_t *)SCMalloc(sgh->sig_cnt * sizeof(uint32_t)); if (fast_pattern == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); return -1; @@ -414,7 +414,7 @@ static int PatternMatchPreprarePopulateMpm(DetectEngineCtx *de_ctx, SigGroupHead HashTable *ht = HashTableInit(4096, ContentHashFunc, ContentHashCompareFunc, ContentHashFree); if (ht == NULL) { - free(fast_pattern); + SCFree(fast_pattern); return -1; } @@ -639,12 +639,12 @@ static int PatternMatchPreprarePopulateMpm(DetectEngineCtx *de_ctx, SigGroupHead } if (fast_pattern != NULL) - free(fast_pattern); + SCFree(fast_pattern); HashTableFree(ht); return 0; error: if (fast_pattern != NULL) - free(fast_pattern); + SCFree(fast_pattern); if (ht != NULL) HashTableFree(ht); return -1; @@ -704,7 +704,7 @@ int PatternMatchPrepareGroup(DetectEngineCtx *de_ctx, SigGroupHead *sh) /* intialize contexes */ if (sh->flags & SIG_GROUP_HAVECONTENT && !(sh->flags & SIG_GROUP_HEAD_MPM_COPY)) { /* search */ - sh->mpm_ctx = malloc(sizeof(MpmCtx)); + sh->mpm_ctx = SCMalloc(sizeof(MpmCtx)); if (sh->mpm_ctx == NULL) goto error; @@ -716,7 +716,7 @@ int PatternMatchPrepareGroup(DetectEngineCtx *de_ctx, SigGroupHead *sh) #endif } if (sh->flags & SIG_GROUP_HAVEURICONTENT && !(sh->flags & SIG_GROUP_HEAD_MPM_URI_COPY)) { - sh->mpm_uri_ctx = malloc(sizeof(MpmCtx)); + sh->mpm_uri_ctx = SCMalloc(sizeof(MpmCtx)); if (sh->mpm_uri_ctx == NULL) goto error; diff --git a/src/detect-engine-port.c b/src/detect-engine-port.c index 28f8004b46..0383c71354 100644 --- a/src/detect-engine-port.c +++ b/src/detect-engine-port.c @@ -15,6 +15,7 @@ #include "util-cidr.h" #include "util-unittest.h" +#include "util-unittest-helper.h" #include "util-rule-vars.h" #include "detect-parse.h" @@ -56,7 +57,7 @@ void DetectPortRegister(void) { * case of error. */ DetectPort *DetectPortInit(void) { - DetectPort *dp = malloc(sizeof(DetectPort)); + DetectPort *dp = SCMalloc(sizeof(DetectPort)); if (dp == NULL) { // SCLogDebug(SC_ERR_MEM_ALLOC, "Error allocating memory"); return NULL; @@ -93,7 +94,7 @@ void DetectPortFree(DetectPort *dp) { detect_port_memory -= sizeof(DetectPort); detect_port_free_cnt++; - free(dp); + SCFree(dp); } /** @@ -925,7 +926,7 @@ static int DetectPortParseInsertString(DetectPort **head, char *s) { */ if (ad2 != NULL) { if (DetectPortParseInsert(head, ad2) < 0) { - if (ad2 != NULL) free(ad2); + if (ad2 != NULL) SCFree(ad2); goto error; } } @@ -1041,7 +1042,7 @@ static int DetectPortParseDo(DetectPort **head, DetectPort **nhead, char *s, goto error; temp_rule_var_port = rule_var_port; if (negate == 1 || n_set == 1) { - temp_rule_var_port = malloc(strlen(rule_var_port) + 3); + temp_rule_var_port = SCMalloc(strlen(rule_var_port) + 3); if (temp_rule_var_port == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); goto error; @@ -1057,7 +1058,7 @@ static int DetectPortParseDo(DetectPort **head, DetectPort **nhead, char *s, d_set = 0; n_set = 0; if (temp_rule_var_port != rule_var_port) - free(temp_rule_var_port); + SCFree(temp_rule_var_port); } else { address[x - 1] = '\0'; SCLogDebug("Parsed port from DetectPortParseDo - %s", address); @@ -1088,7 +1089,7 @@ static int DetectPortParseDo(DetectPort **head, DetectPort **nhead, char *s, goto error; temp_rule_var_port = rule_var_port; if ((negate + n_set) % 2) { - temp_rule_var_port = malloc(strlen(rule_var_port) + 3); + temp_rule_var_port = SCMalloc(strlen(rule_var_port) + 3); if (temp_rule_var_port == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); goto error; @@ -1103,7 +1104,7 @@ static int DetectPortParseDo(DetectPort **head, DetectPort **nhead, char *s, d_set = 0; if (temp_rule_var_port != rule_var_port) - free(temp_rule_var_port); + SCFree(temp_rule_var_port); } else { if (!((negate + n_set) % 2)) { r = DetectPortParseInsertString(head,address); @@ -1298,7 +1299,7 @@ error: * \retval NULL on error */ DetectPort *PortParse(char *str) { - char *portdup = strdup(str); + char *portdup = SCStrdup(str); char *port2 = NULL; DetectPort *dp = NULL; @@ -1351,14 +1352,14 @@ DetectPort *PortParse(char *str) { } } - free(portdup); + SCFree(portdup); return dp; error: if (dp != NULL) DetectPortCleanupList(dp); - if (portdup) free(portdup); + if (portdup) SCFree(portdup); return NULL; } @@ -2154,56 +2155,10 @@ int PortTestMatchReal(uint8_t *raw_eth_pkt, uint16_t pktsize, char *sig, uint32_t sid) { int result = 1; - - Packet p; - DecodeThreadVars dtv; - - ThreadVars th_v; - DetectEngineThreadCtx *det_ctx = NULL; - - memset(&p, 0, sizeof(Packet)); - memset(&dtv, 0, sizeof(DecodeThreadVars)); - memset(&th_v, 0, sizeof(th_v)); - FlowInitConfig(FLOW_QUIET); - DecodeEthernet(&th_v, &dtv, &p, raw_eth_pkt, pktsize, NULL); - - DetectEngineCtx *de_ctx = DetectEngineCtxInit(); - if (de_ctx == NULL) { - result=0; - goto end; - } - - de_ctx->flags |= DE_QUIET; - - de_ctx->sig_list = SigInit(de_ctx, sig); - de_ctx->sig_list->next = NULL; - if (de_ctx->sig_list == NULL) { - result = 0; - goto end; - } - - SigGroupBuild(de_ctx); - //PatternMatchPrepare(mpm_ctx, MPM_B2G); - DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx); - - SigMatchSignatures(&th_v, de_ctx, det_ctx, &p); - if (PacketAlertCheck(&p, sid) != 1) { - result = 0; - goto end; - } - -end: - if (de_ctx != NULL) - { - //PatternMatchDestroy(mpm_ctx); - SigGroupCleanup(de_ctx); - SigCleanSignatures(de_ctx); - DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx); - DetectEngineCtxFree(de_ctx); - } + Packet *p = UTHBuildPacketFromEth(raw_eth_pkt, pktsize); + result = UTHPacketMatchSig(p, sig); FlowShutdown(); - return result; } @@ -2283,8 +2238,7 @@ int PortTestMatchRealWrp(char *sig, uint32_t sid) { int PortTestMatchReal01() { /* tcp.sport=47370 tcp.dport=80 */ - char *sig = "alert tcp any any -> any 80 (msg:\"Nothing..\";" - " content:\"GET\"; sid:1;)"; + char *sig = "alert tcp any any -> any 80 (msg:\"Nothing..\"; content:\"GET\"; sid:1;)"; return PortTestMatchRealWrp(sig, 1); } @@ -2464,7 +2418,7 @@ int PortTestMatchReal18() int PortTestMatchReal19() { char *sig = "alert tcp any any -> any 80 (msg:\"Nothing..\";" - " sid:1;)"; + " content:\"GET\"; sid:1;)"; return PortTestMatchRealWrp(sig, 1); } diff --git a/src/detect-engine-proto.c b/src/detect-engine-proto.c index 7a4a7fde2a..fd213cd1f5 100644 --- a/src/detect-engine-proto.c +++ b/src/detect-engine-proto.c @@ -41,7 +41,7 @@ void DetectProtoRegister (void) DetectProto *DetectProtoInit(void) { - DetectProto *dp = malloc(sizeof(DetectProto)); + DetectProto *dp = SCMalloc(sizeof(DetectProto)); if (dp == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "error in memory allocation"); return NULL; @@ -61,7 +61,7 @@ void DetectProtoFree(DetectProto *dp) if (dp == NULL) return; - free(dp); + SCFree(dp); } /** diff --git a/src/detect-engine-siggroup.c b/src/detect-engine-siggroup.c index b261953198..0bca02f99d 100644 --- a/src/detect-engine-siggroup.c +++ b/src/detect-engine-siggroup.c @@ -43,7 +43,7 @@ static uint32_t detect_siggroup_matcharray_init_cnt = 0; static uint32_t detect_siggroup_matcharray_free_cnt = 0; static SigGroupHeadInitData *SigGroupHeadInitDataAlloc(uint32_t size) { - SigGroupHeadInitData *sghid = malloc(sizeof(SigGroupHeadInitData)); + SigGroupHeadInitData *sghid = SCMalloc(sizeof(SigGroupHeadInitData)); if (sghid == NULL) return NULL; @@ -56,16 +56,16 @@ static SigGroupHeadInitData *SigGroupHeadInitDataAlloc(uint32_t size) { void SigGroupHeadInitDataFree(SigGroupHeadInitData *sghid) { if (sghid->content_array != NULL) { - free(sghid->content_array); + SCFree(sghid->content_array); sghid->content_array = NULL; sghid->content_size = 0; } if (sghid->uri_content_array != NULL) { - free(sghid->uri_content_array); + SCFree(sghid->uri_content_array); sghid->uri_content_array = NULL; sghid->uri_content_size = 0; } - free(sghid); + SCFree(sghid); detect_siggroup_head_initdata_free_cnt++; detect_siggroup_head_initdata_memory -= sizeof(SigGroupHeadInitData); @@ -82,7 +82,7 @@ void SigGroupHeadInitDataFree(SigGroupHeadInitData *sghid) { */ static SigGroupHead *SigGroupHeadAlloc(uint32_t size) { - SigGroupHead *sgh = malloc(sizeof(SigGroupHead)); + SigGroupHead *sgh = SCMalloc(sizeof(SigGroupHead)); if (sgh == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); return NULL; @@ -98,7 +98,7 @@ static SigGroupHead *SigGroupHeadAlloc(uint32_t size) /* initialize the signature bitarray */ sgh->sig_size = size; - if ( (sgh->sig_array = malloc(sgh->sig_size)) == NULL) { + if ( (sgh->sig_array = SCMalloc(sgh->sig_size)) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); goto error; } @@ -130,7 +130,7 @@ void SigGroupHeadFree(SigGroupHead *sgh) PatternMatchDestroyGroup(sgh); if (sgh->sig_array != NULL) { - free(sgh->sig_array); + SCFree(sgh->sig_array); sgh->sig_array = NULL; detect_siggroup_sigarray_free_cnt++; @@ -140,7 +140,7 @@ void SigGroupHeadFree(SigGroupHead *sgh) if (sgh->match_array != NULL) { detect_siggroup_matcharray_free_cnt++; detect_siggroup_matcharray_memory -= (sgh->sig_cnt * sizeof(SigIntId)); - free(sgh->match_array); + SCFree(sgh->match_array); sgh->match_array = NULL; sgh->sig_cnt = 0; } @@ -150,7 +150,7 @@ void SigGroupHeadFree(SigGroupHead *sgh) sgh->init = NULL; } - free(sgh); + SCFree(sgh); detect_siggroup_head_free_cnt++; detect_siggroup_head_memory -= sizeof(SigGroupHead); @@ -612,7 +612,7 @@ SigGroupHead *SigGroupHeadDPortHashLookup(DetectEngineCtx *de_ctx, */ void SigGroupHeadDPortHashFree(DetectEngineCtx *de_ctx) { - if (de_ctx->dport_hash_table == NULL) + if (de_ctx->sgh_dport_hash_table == NULL) return; HashListTableFree(de_ctx->sgh_dport_hash_table); @@ -693,7 +693,7 @@ SigGroupHead *SigGroupHeadSPortHashLookup(DetectEngineCtx *de_ctx, */ void SigGroupHeadSPortHashFree(DetectEngineCtx *de_ctx) { - if (de_ctx->sport_hash_table == NULL) + if (de_ctx->sgh_sport_hash_table == NULL) return; HashListTableFree(de_ctx->sgh_sport_hash_table); @@ -724,7 +724,7 @@ static void SigGroupHeadFreeSigArraysHash2(DetectEngineCtx *de_ctx, detect_siggroup_sigarray_free_cnt++; detect_siggroup_sigarray_memory -= sgh->sig_size; - free(sgh->sig_array); + SCFree(sgh->sig_array); sgh->sig_array = NULL; sgh->sig_size = 0; } @@ -760,7 +760,7 @@ static void SigGroupHeadFreeSigArraysHash(DetectEngineCtx *de_ctx, detect_siggroup_sigarray_free_cnt++; detect_siggroup_sigarray_memory -= sgh->sig_size; - free(sgh->sig_array); + SCFree(sgh->sig_array); sgh->sig_array = NULL; sgh->sig_size = 0; } @@ -1118,7 +1118,7 @@ int SigGroupHeadLoadContent(DetectEngineCtx *de_ctx, SigGroupHead *sgh) BUG_ON(sgh->init == NULL); sgh->init->content_size = (DetectContentMaxId(de_ctx) / 8) + 1; - sgh->init->content_array = malloc(sgh->init->content_size); + sgh->init->content_array = SCMalloc(sgh->init->content_size); if (sgh->init->content_array == NULL) return -1; @@ -1164,7 +1164,7 @@ int SigGroupHeadClearContent(SigGroupHead *sh) return 0; if (sh->init->content_array != NULL) { - free(sh->init->content_array); + SCFree(sh->init->content_array); sh->init->content_array = NULL; sh->init->content_size = 0; } @@ -1201,7 +1201,7 @@ int SigGroupHeadLoadUricontent(DetectEngineCtx *de_ctx, SigGroupHead *sgh) BUG_ON(sgh->init == NULL); sgh->init->uri_content_size = (DetectUricontentMaxId(de_ctx) / 8) + 1; - sgh->init->uri_content_array = malloc(sgh->init->uri_content_size); + sgh->init->uri_content_array = SCMalloc(sgh->init->uri_content_size); if (sgh->init->uri_content_array == NULL) return -1; @@ -1248,7 +1248,7 @@ int SigGroupHeadClearUricontent(SigGroupHead *sh) return 0; if (sh->init->uri_content_array != NULL) { - free(sh->init->uri_content_array); + SCFree(sh->init->uri_content_array); sh->init->uri_content_array = NULL; sh->init->uri_content_size = 0; } @@ -1279,7 +1279,7 @@ int SigGroupHeadBuildMatchArray(DetectEngineCtx *de_ctx, SigGroupHead *sgh, BUG_ON(sgh->match_array != NULL); - sgh->match_array = malloc(sgh->sig_cnt * sizeof(SigIntId)); + sgh->match_array = SCMalloc(sgh->sig_cnt * sizeof(SigIntId)); if (sgh->match_array == NULL) return -1; @@ -1362,17 +1362,15 @@ static int SigGroupHeadTest01(void) { int result = 1; - DetectEngineCtx *de_ctx = DetectEngineCtxInit(); + DetectEngineCtx de_ctx; - SigGroupHeadMpmHashInit(de_ctx); + SigGroupHeadMpmHashInit(&de_ctx); - result &= (de_ctx->sgh_mpm_hash_table != NULL); + result &= (de_ctx.sgh_mpm_hash_table != NULL); - SigGroupHeadMpmHashFree(de_ctx); + SigGroupHeadMpmHashFree(&de_ctx); - result &= (de_ctx->sgh_mpm_hash_table == NULL); - - DetectEngineCtxFree(de_ctx); + result &= (de_ctx.sgh_mpm_hash_table == NULL); return result; } @@ -1386,17 +1384,15 @@ static int SigGroupHeadTest02(void) { int result = 1; - DetectEngineCtx *de_ctx = DetectEngineCtxInit(); - - SigGroupHeadMpmUriHashInit(de_ctx); + DetectEngineCtx de_ctx; - result &= (de_ctx->sgh_mpm_uri_hash_table != NULL); + SigGroupHeadMpmUriHashInit(&de_ctx); - SigGroupHeadMpmUriHashFree(de_ctx); + result &= (de_ctx.sgh_mpm_uri_hash_table != NULL); - result &= (de_ctx->sgh_mpm_uri_hash_table == NULL); + SigGroupHeadMpmUriHashFree(&de_ctx); - DetectEngineCtxFree(de_ctx); + result &= (de_ctx.sgh_mpm_uri_hash_table == NULL); return result; } @@ -1410,17 +1406,15 @@ static int SigGroupHeadTest03(void) { int result = 1; - DetectEngineCtx *de_ctx = DetectEngineCtxInit(); + DetectEngineCtx de_ctx; - SigGroupHeadHashInit(de_ctx); + SigGroupHeadHashInit(&de_ctx); - result &= (de_ctx->sgh_hash_table != NULL); + result &= (de_ctx.sgh_hash_table != NULL); - SigGroupHeadHashFree(de_ctx); + SigGroupHeadHashFree(&de_ctx); - result &= (de_ctx->sgh_hash_table == NULL); - - DetectEngineCtxFree(de_ctx); + result &= (de_ctx.sgh_hash_table == NULL); return result; } @@ -1434,17 +1428,15 @@ static int SigGroupHeadTest04(void) { int result = 1; - DetectEngineCtx *de_ctx = DetectEngineCtxInit(); - - SigGroupHeadDPortHashInit(de_ctx); + DetectEngineCtx de_ctx; - result &= (de_ctx->sgh_dport_hash_table != NULL); + SigGroupHeadDPortHashInit(&de_ctx); - SigGroupHeadDPortHashFree(de_ctx); + result &= (de_ctx.sgh_dport_hash_table != NULL); - result &= (de_ctx->sgh_dport_hash_table == NULL); + SigGroupHeadDPortHashFree(&de_ctx); - DetectEngineCtxFree(de_ctx); + result &= (de_ctx.sgh_dport_hash_table == NULL); return result; } @@ -1458,17 +1450,15 @@ static int SigGroupHeadTest05(void) { int result = 1; - DetectEngineCtx *de_ctx = DetectEngineCtxInit(); - - SigGroupHeadSPortHashInit(de_ctx); + DetectEngineCtx de_ctx; - result &= (de_ctx->sgh_sport_hash_table != NULL); + SigGroupHeadSPortHashInit(&de_ctx); - SigGroupHeadSPortHashFree(de_ctx); + result &= (de_ctx.sgh_sport_hash_table != NULL); - result &= (de_ctx->sgh_sport_hash_table == NULL); + SigGroupHeadSPortHashFree(&de_ctx); - DetectEngineCtxFree(de_ctx); + result &= (de_ctx.sgh_sport_hash_table == NULL); return result; } diff --git a/src/detect-engine-sigorder.c b/src/detect-engine-sigorder.c index ec2d030d2b..48ca5e924e 100644 --- a/src/detect-engine-sigorder.c +++ b/src/detect-engine-sigorder.c @@ -55,7 +55,7 @@ static void SCSigRegisterSignatureOrderingFunc(DetectEngineCtx *de_ctx, if (curr != NULL) return; - if ( (temp = malloc(sizeof(SCSigOrderFunc))) == NULL) { + if ( (temp = SCMalloc(sizeof(SCSigOrderFunc))) == NULL) { printf("Error allocating memory\n"); exit(EXIT_FAILURE); } @@ -739,7 +739,7 @@ static inline SCSigSignatureWrapper *SCSigAllocSignatureWrapper(Signature *sig) SCSigSignatureWrapper *sw = NULL; int i = 0; - if ( (sw = malloc(sizeof(SCSigSignatureWrapper))) == NULL) { + if ( (sw = SCMalloc(sizeof(SCSigSignatureWrapper))) == NULL) { printf("Error allocating memory\n"); exit(EXIT_FAILURE); } @@ -747,14 +747,14 @@ static inline SCSigSignatureWrapper *SCSigAllocSignatureWrapper(Signature *sig) sw->sig = sig; - if ( (sw->user = malloc(SC_RADIX_USER_DATA_MAX * sizeof(int *))) == NULL) { + if ( (sw->user = SCMalloc(SC_RADIX_USER_DATA_MAX * sizeof(int *))) == NULL) { printf("Error allocating memory\n"); exit(EXIT_FAILURE); } memset(sw->user, 0, SC_RADIX_USER_DATA_MAX * sizeof(int *)); for (i = 0; i < SC_RADIX_USER_DATA_MAX; i++) { - if ( (sw->user[i] = malloc(sizeof(int))) == NULL) { + if ( (sw->user[i] = SCMalloc(sizeof(int))) == NULL) { printf("Error allocating memory\n"); exit(EXIT_FAILURE); } @@ -871,7 +871,7 @@ void SCSigSignatureOrderingModuleCleanup(DetectEngineCtx *de_ctx) while (funcs != NULL) { temp = funcs; funcs = funcs->next; - free(temp); + SCFree(temp); } de_ctx->sc_sig_order_funcs = NULL; @@ -880,7 +880,7 @@ void SCSigSignatureOrderingModuleCleanup(DetectEngineCtx *de_ctx) while (sigw != NULL) { temp = sigw; sigw = sigw->next; - free(temp); + SCFree(temp); } de_ctx->sc_sig_sig_wrapper = NULL; @@ -1070,8 +1070,9 @@ static int SCSigTestSignatureOrdering02(void) sw = sw->next; } - DetectEngineCtxFree(de_ctx); end: + if (de_ctx != NULL) + DetectEngineCtxFree(de_ctx); return result; } @@ -1207,8 +1208,9 @@ static int SCSigTestSignatureOrdering03(void) sw = sw->next; } - DetectEngineCtxFree(de_ctx); end: + if (de_ctx != NULL) + DetectEngineCtxFree(de_ctx); return result; } @@ -1308,8 +1310,9 @@ static int SCSigTestSignatureOrdering04(void) sw = sw->next; } - DetectEngineCtxFree(de_ctx); end: + if (de_ctx) + DetectEngineCtxFree(de_ctx); return result; } @@ -1402,8 +1405,9 @@ static int SCSigTestSignatureOrdering05(void) sw = sw->next; } - DetectEngineCtxFree(de_ctx); end: + if (de_ctx != NULL) + DetectEngineCtxFree(de_ctx); return result; } @@ -1496,8 +1500,9 @@ static int SCSigTestSignatureOrdering06(void) sw = sw->next; } - DetectEngineCtxFree(de_ctx); end: + if (de_ctx != NULL) + DetectEngineCtxFree(de_ctx); return result; } diff --git a/src/detect-engine-threshold.c b/src/detect-engine-threshold.c index 2c8fac3df1..d9493911fc 100644 --- a/src/detect-engine-threshold.c +++ b/src/detect-engine-threshold.c @@ -236,9 +236,9 @@ void PacketAlertThreshold(DetectEngineCtx *de_ctx, DetectThresholdData *td, Pack SCReturn; /* setup the Entry we use to search our hash with */ - ste = malloc(sizeof(DetectThresholdEntry)); + ste = SCMalloc(sizeof(DetectThresholdEntry)); if (ste == NULL) { - SCLogError(SC_ERR_MEM_ALLOC, "malloc failed: %s", strerror(errno)); + SCLogError(SC_ERR_MEM_ALLOC, "SCMalloc failed: %s", strerror(errno)); SCReturn; } memset(ste, 0x00, sizeof(ste)); @@ -361,7 +361,7 @@ void PacketAlertThreshold(DetectEngineCtx *de_ctx, DetectThresholdData *td, Pack SCMutexUnlock(&de_ctx->ths_ctx.threshold_table_lock); if (ste != NULL) - free(ste); + SCFree(ste); ThresholdTimeoutRemove(de_ctx); SCReturn; @@ -370,7 +370,7 @@ void PacketAlertThreshold(DetectEngineCtx *de_ctx, DetectThresholdData *td, Pack void ThresholdFreeFunc(void *data) { if (data != NULL) - free(data); + SCFree(data); return; } diff --git a/src/detect-engine.c b/src/detect-engine.c index 7fe7df2fd5..6c46789af1 100644 --- a/src/detect-engine.c +++ b/src/detect-engine.c @@ -34,7 +34,7 @@ static uint8_t DetectEngineCtxLoadConf(DetectEngineCtx *); DetectEngineCtx *DetectEngineCtxInit(void) { DetectEngineCtx *de_ctx; - de_ctx = malloc(sizeof(DetectEngineCtx)); + de_ctx = SCMalloc(sizeof(DetectEngineCtx)); if (de_ctx == NULL) { goto error; } @@ -83,11 +83,11 @@ void DetectEngineCtxFree(DetectEngineCtx *de_ctx) { VariableNameFreeHash(de_ctx); if (de_ctx->sig_array) - free(de_ctx->sig_array); + SCFree(de_ctx->sig_array); if (de_ctx->class_conf_ht != NULL) HashTableFree(de_ctx->class_conf_ht); - free(de_ctx); + SCFree(de_ctx); //DetectAddressGroupPrintMemory(); //DetectSigGroupPrintMemory(); //DetectPortPrintMemory(); @@ -404,7 +404,7 @@ TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data) { if (de_ctx == NULL) return TM_ECODE_FAILED; - DetectEngineThreadCtx *det_ctx = malloc(sizeof(DetectEngineThreadCtx)); + DetectEngineThreadCtx *det_ctx = SCMalloc(sizeof(DetectEngineThreadCtx)); if (det_ctx == NULL) { return TM_ECODE_FAILED; } @@ -444,7 +444,7 @@ TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data) { char *cuda_outq_name = "cuda_mpm_rc_disp_outq"; uint8_t disp_outq_name_len = (strlen(tv->name) + strlen(cuda_outq_name) + 1); - char *disp_outq_name = malloc(disp_outq_name_len * sizeof(char)); + char *disp_outq_name = SCMalloc(disp_outq_name_len * sizeof(char)); if (disp_outq_name == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); @@ -489,7 +489,7 @@ TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data) { PatternMatchThreadDestroy(&det_ctx->mtc, det_ctx->de_ctx->mpm_matcher); PatternMatchThreadDestroy(&det_ctx->mtcu, det_ctx->de_ctx->mpm_matcher); - free(det_ctx); + SCFree(det_ctx); return TM_ECODE_OK; } diff --git a/src/detect-fast-pattern.c b/src/detect-fast-pattern.c index 0ab940ac86..86cca74967 100644 --- a/src/detect-fast-pattern.c +++ b/src/detect-fast-pattern.c @@ -606,13 +606,15 @@ int DetectFastPatternTest11(void) if (PacketPatternScan(&th_v, det_ctx, &p) == 0) result = 1; - SigGroupCleanup(de_ctx); - SigCleanSignatures(de_ctx); - - DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx); end: - DetectEngineCtxFree(de_ctx); + if (de_ctx != NULL) { + SigGroupCleanup(de_ctx); + SigCleanSignatures(de_ctx); + if (det_ctx != NULL) + DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx); + DetectEngineCtxFree(de_ctx); + } return result; } @@ -785,13 +787,14 @@ int DetectFastPatternTest14(void) }else{ SCLogInfo("match on sig 1 fast_pattern no match sig 2 inspecting same payload"); } +end: SigGroupCleanup(de_ctx); SigCleanSignatures(de_ctx); DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx); -end: DetectEngineCtxFree(de_ctx); + FlowShutdown(); return result; } diff --git a/src/detect-flags.c b/src/detect-flags.c index 9be7f54373..b97402753d 100644 --- a/src/detect-flags.c +++ b/src/detect-flags.c @@ -180,7 +180,7 @@ static DetectFlagsData *DetectFlagsParse (char *rawstr) goto error; } - de = malloc(sizeof(DetectFlagsData)); + de = SCMalloc(sizeof(DetectFlagsData)); if (de == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "malloc failed"); goto error; @@ -396,16 +396,16 @@ static DetectFlagsData *DetectFlagsParse (char *rawstr) } for (i = 0; i < (ret - 1); i++){ - if (args[i] != NULL) free(args[i]); + if (args[i] != NULL) SCFree(args[i]); } SCReturnPtr(de, "DetectFlagsData"); error: for (i = 0; i < (ret - 1); i++){ - if (args[i] != NULL) free(args[i]); + if (args[i] != NULL) SCFree(args[i]); } - if (de) free(de); + if (de) SCFree(de); SCReturnPtr(NULL, "DetectFlagsData"); } @@ -441,8 +441,8 @@ static int DetectFlagsSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, return 0; error: - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return -1; } @@ -454,7 +454,7 @@ error: */ static void DetectFlagsFree(void *de_ptr) { DetectFlagsData *de = (DetectFlagsData *)de_ptr; - if(de) free(de); + if(de) SCFree(de); } /* @@ -535,14 +535,14 @@ static int FlagsTestParse03 (void) { ret = DetectFlagsMatch(&tv,NULL,&p,NULL,sm); if(ret) { - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 1; } error: - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 0; } @@ -585,14 +585,14 @@ static int FlagsTestParse04 (void) { ret = DetectFlagsMatch(&tv,NULL,&p,NULL,sm); if(ret) { - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 1; } error: - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 0; } @@ -635,14 +635,14 @@ static int FlagsTestParse05 (void) { ret = DetectFlagsMatch(&tv,NULL,&p,NULL,sm); if(ret) { - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 1; } error: - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 0; } @@ -685,14 +685,14 @@ static int FlagsTestParse06 (void) { ret = DetectFlagsMatch(&tv,NULL,&p,NULL,sm); if(ret) { - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 1; } error: - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 0; } @@ -735,14 +735,14 @@ static int FlagsTestParse07 (void) { ret = DetectFlagsMatch(&tv,NULL,&p,NULL,sm); if(ret) { - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 1; } error: - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 0; } @@ -785,14 +785,14 @@ static int FlagsTestParse08 (void) { ret = DetectFlagsMatch(&tv,NULL,&p,NULL,sm); if(ret) { - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 1; } error: - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 0; } @@ -835,14 +835,14 @@ static int FlagsTestParse09 (void) { ret = DetectFlagsMatch(&tv,NULL,&p,NULL,sm); if(ret) { - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 1; } error: - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 0; } @@ -885,14 +885,14 @@ static int FlagsTestParse10 (void) { ret = DetectFlagsMatch(&tv,NULL,&p,NULL,sm); if(ret) { - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 1; } error: - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 0; } @@ -935,14 +935,14 @@ static int FlagsTestParse11 (void) { ret = DetectFlagsMatch(&tv,NULL,&p,NULL,sm); if(ret) { - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 1; } error: - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 0; } @@ -987,14 +987,14 @@ static int FlagsTestParse12 (void) { ret = DetectFlagsMatch(&tv,NULL,&p,NULL,sm); if(ret) { - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 1; } error: - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 0; } diff --git a/src/detect-flow.c b/src/detect-flow.c index 9faf467302..5310e192a2 100644 --- a/src/detect-flow.c +++ b/src/detect-flow.c @@ -155,7 +155,7 @@ DetectFlowData *DetectFlowParse (char *flowstr) } } - fd = malloc(sizeof(DetectFlowData)); + fd = SCMalloc(sizeof(DetectFlowData)); if (fd == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "malloc failed"); goto error; @@ -231,13 +231,13 @@ DetectFlowData *DetectFlowParse (char *flowstr) } } for (i = 0; i < (ret -1); i++){ - if (args[i] != NULL) free(args[i]); + if (args[i] != NULL) SCFree(args[i]); } return fd; error: for (i = 0; i < (ret -1); i++){ - if (args[i] != NULL) free(args[i]); + if (args[i] != NULL) SCFree(args[i]); } if (fd != NULL) DetectFlowFree(fd); return NULL; @@ -281,7 +281,7 @@ int DetectFlowSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char *f error: if (fd != NULL) DetectFlowFree(fd); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); return -1; } @@ -293,7 +293,7 @@ error: */ void DetectFlowFree(void *ptr) { DetectFlowData *fd = (DetectFlowData *)ptr; - free(fd); + SCFree(fd); } #ifdef UNITTESTS diff --git a/src/detect-flowbits.c b/src/detect-flowbits.c index e4d5f143b2..f88e9bfefa 100644 --- a/src/detect-flowbits.c +++ b/src/detect-flowbits.c @@ -199,7 +199,7 @@ int DetectFlowbitSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char break; } - cd = malloc(sizeof(DetectFlowbitsData)); + cd = SCMalloc(sizeof(DetectFlowbitsData)); if (cd == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "DetectFlowbitsSetup malloc failed"); goto error; @@ -239,9 +239,9 @@ error: if (fb_cmd_str != NULL) pcre_free_substring(fb_cmd_str); if (cd != NULL) - free(cd); + SCFree(cd); if (sm != NULL) - free(sm); + SCFree(sm); return -1; } @@ -251,7 +251,7 @@ void DetectFlowbitFree (void *ptr) { if (fd == NULL) return; - free(fd); + SCFree(fd); } #ifdef UNITTESTS @@ -814,7 +814,7 @@ static int FlowBitsTestSig07(void) { goto end; } - s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Flowbit unset\"; flowbits:unset,myflow2; sid:11;)"); + s = s->next = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Flowbit unset\"; flowbits:unset,myflow2; sid:11;)"); if (s == NULL) { goto end; } diff --git a/src/detect-flowint.c b/src/detect-flowint.c index 06705f320c..6006228bee 100644 --- a/src/detect-flowint.c +++ b/src/detect-flowint.c @@ -263,7 +263,7 @@ DetectFlowintData *DetectFlowintParse(DetectEngineCtx *de_ctx, goto error; } - sfd = malloc(sizeof(DetectFlowintData)); + sfd = SCMalloc(sizeof(DetectFlowintData)); if (sfd == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "malloc failed"); goto error; @@ -276,13 +276,13 @@ DetectFlowintData *DetectFlowintParse(DetectEngineCtx *de_ctx, varval =(char *) str_ptr; if (res < 0 || strcmp(varval,"") == 0) { SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed"); - free(sfd); + SCFree(sfd); return NULL; } /* get the target value to operate with *(it should be a value or another var) */ - str = strdup(varval); + str = SCStrdup(varval); if (str == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "malloc from strdup failed"); goto error; @@ -305,7 +305,7 @@ DetectFlowintData *DetectFlowintParse(DetectEngineCtx *de_ctx, } /* Set the name of the origin var to modify/compared with the target */ - sfd->name = strdup(varname); + sfd->name = SCStrdup(varname); if (de_ctx != NULL) sfd->idx = VariableNameGetIdx(de_ctx, varname, DETECT_FLOWINT); sfd->target.value =(uint32_t) value_long; @@ -314,8 +314,8 @@ DetectFlowintData *DetectFlowintParse(DetectEngineCtx *de_ctx, return sfd; error: - if (sfd != NULL) free(sfd); - free(str); + if (sfd != NULL) SCFree(sfd); + SCFree(str); return NULL; } @@ -355,7 +355,7 @@ int DetectFlowintSetup(DetectEngineCtx *de_ctx, error: if (sfd) DetectFlowintFree(sfd); - if (sm) free(sm); + if (sm) SCFree(sm); return -1; } @@ -367,11 +367,11 @@ void DetectFlowintFree(void *tmp) DetectFlowintData *sfd =(DetectFlowintData*) tmp; if (sfd != NULL) { if (sfd->name != NULL) - free(sfd->name); + SCFree(sfd->name); if (sfd->targettype == FLOWINT_TARGET_VAR) if (sfd->target.tvar.name != NULL) - free(sfd->target.tvar.name); - free(sfd); + SCFree(sfd->target.tvar.name); + SCFree(sfd); } } diff --git a/src/detect-flowvar.c b/src/detect-flowvar.c index 1af93d24cd..f481b84df7 100644 --- a/src/detect-flowvar.c +++ b/src/detect-flowvar.c @@ -117,18 +117,18 @@ int DetectFlowvarSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char //printf("DetectFlowvarSetup: varname %s, varcontent %s\n", varname, varcontent); if (varcontent[0] == '\"' && varcontent[strlen(varcontent)-1] == '\"') { - str = strdup(varcontent+1); + str = SCStrdup(varcontent+1); str[strlen(varcontent)-2] = '\0'; dubbed = 1; } len = strlen(str); if (len == 0) { - if (dubbed) free(str); + if (dubbed) SCFree(str); return -1; } - cd = malloc(sizeof(DetectFlowvarData)); + cd = SCMalloc(sizeof(DetectFlowvarData)); if (cd == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "malloc failed"); goto error; @@ -191,14 +191,14 @@ int DetectFlowvarSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char len = x; } - cd->content = malloc(len); + cd->content = SCMalloc(len); if (cd->content == NULL) { - if (dubbed) free(str); - free(cd); + if (dubbed) SCFree(str); + SCFree(cd); return -1; } - cd->name = strdup(varname); + cd->name = SCStrdup(varname); cd->idx = VariableNameGetIdx(de_ctx,varname,DETECT_FLOWVAR); memcpy(cd->content, str, len); cd->content_len = len; @@ -215,13 +215,13 @@ int DetectFlowvarSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char SigMatchAppend(s,m,sm); - if (dubbed) free(str); + if (dubbed) SCFree(str); return 0; error: - if (dubbed) free(str); - if (cd) free(cd); - if (sm) free(sm); + if (dubbed) SCFree(str); + if (cd) SCFree(cd); + if (sm) SCFree(sm); return -1; } diff --git a/src/detect-fragbits.c b/src/detect-fragbits.c index beedc7b53c..c24db0ae49 100644 --- a/src/detect-fragbits.c +++ b/src/detect-fragbits.c @@ -165,7 +165,7 @@ static DetectFragBitsData *DetectFragBitsParse (char *rawstr) goto error; } - de = malloc(sizeof(DetectFragBitsData)); + de = SCMalloc(sizeof(DetectFragBitsData)); if (de == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); goto error; @@ -228,15 +228,15 @@ static DetectFragBitsData *DetectFragBitsParse (char *rawstr) goto error; for (i = 0; i < (ret - 1); i++){ - if (args[i] != NULL) free(args[i]); + if (args[i] != NULL) SCFree(args[i]); } return de; error: for (i = 0; i < (ret - 1); i++){ - if (args[i] != NULL) free(args[i]); + if (args[i] != NULL) SCFree(args[i]); } - if (de) free(de); + if (de) SCFree(de); return NULL; } @@ -272,8 +272,8 @@ static int DetectFragBitsSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch return 0; error: - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return -1; } @@ -285,7 +285,7 @@ error: */ static void DetectFragBitsFree(void *de_ptr) { DetectFragBitsData *de = (DetectFragBitsData *)de_ptr; - if(de) free(de); + if(de) SCFree(de); } /* @@ -408,14 +408,14 @@ static int FragBitsTestParse03 (void) { ret = DetectFragBitsMatch(&tv,NULL,&p,NULL,sm); if(ret) { - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 1; } error: - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 0; } @@ -500,14 +500,14 @@ static int FragBitsTestParse04 (void) { ret = DetectFragBitsMatch(&tv,NULL,&p,NULL,sm); if(ret) { - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 1; } error: - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 0; } #endif /* UNITTESTS */ diff --git a/src/detect-fragoffset.c b/src/detect-fragoffset.c index a19c49e38c..0f393fc780 100644 --- a/src/detect-fragoffset.c +++ b/src/detect-fragoffset.c @@ -134,7 +134,7 @@ DetectFragOffsetData *DetectFragOffsetParse (char *fragoffsetstr) { substr[i-1] = (char *)str_ptr; } - fragoff = malloc(sizeof(DetectFragOffsetData)); + fragoff = SCMalloc(sizeof(DetectFragOffsetData)); if (fragoff == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); goto error; @@ -163,14 +163,14 @@ DetectFragOffsetData *DetectFragOffsetParse (char *fragoffsetstr) { ByteExtractStringUint16(&fragoff->frag_off, 10, 0, substr[1]); for (i = 0; i < 2; i++) { - if (substr[i] != NULL) free(substr[i]); + if (substr[i] != NULL) SCFree(substr[i]); } return fragoff; error: for (i = 0; i < 2; i++) { - if (substr[i] != NULL) free(substr[i]); + if (substr[i] != NULL) SCFree(substr[i]); } if (fragoff != NULL) DetectFragOffsetFree(fragoff); return NULL; @@ -207,7 +207,7 @@ int DetectFragOffsetSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, c error: if (fragoff != NULL) DetectFragOffsetFree(fragoff); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); return -1; } @@ -219,7 +219,7 @@ error: */ void DetectFragOffsetFree (void *ptr) { DetectFragOffsetData *fragoff = (DetectFragOffsetData *)ptr; - free(fragoff); + SCFree(fragoff); } #ifdef UNITTESTS diff --git a/src/detect-ftpbounce.c b/src/detect-ftpbounce.c index 51471a8ab5..b0e2a75f28 100644 --- a/src/detect-ftpbounce.c +++ b/src/detect-ftpbounce.c @@ -333,6 +333,7 @@ static int DetectFtpbounceTestALMatch02(void) { SigGroupBuild(de_ctx); DetectEngineThreadCtxInit(&th_v,(void *)de_ctx,(void *)&det_ctx); + StreamL7DataPtrInit(&ssn); int r = AppLayerParse(&f, ALPROTO_FTP, STREAM_TOSERVER, ftpbuf1, ftplen1); if (r != 0) { SCLogDebug("toserver chunk 1 returned %" PRId32 ", expected 0: ", r); diff --git a/src/detect-gid.c b/src/detect-gid.c index 7d6e58abba..b68cce5320 100644 --- a/src/detect-gid.c +++ b/src/detect-gid.c @@ -93,7 +93,7 @@ static uint32_t DetectGidParse (char *rawstr) rc = (uint32_t )atol(ptr); - free(ptr); + SCFree(ptr); return rc; } diff --git a/src/detect-http-cookie.c b/src/detect-http-cookie.c index 8d3034be61..2565934432 100644 --- a/src/detect-http-cookie.c +++ b/src/detect-http-cookie.c @@ -187,24 +187,24 @@ int DetectHttpCookieSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, } /* Setup the HttpCookie data from Content data structure */ - hd = malloc(sizeof(DetectHttpCookieData)); + hd = SCMalloc(sizeof(DetectHttpCookieData)); if (hd == NULL) { - SCLogError(SC_ERR_MEM_ALLOC, "malloc failed"); + SCLogError(SC_ERR_MEM_ALLOC, "SCMalloc failed"); goto error; } memset(hd, 0, sizeof(DetectHttpCookieData)); hd->data_len = ((DetectContentData *)m->ctx)->content_len; - hd->data = malloc(hd->data_len); + hd->data = SCMalloc(hd->data_len); if (hd->data == NULL) { - SCLogError(SC_ERR_MEM_ALLOC, "malloc failed"); + SCLogError(SC_ERR_MEM_ALLOC, "SCMalloc failed"); goto error; } memcpy(hd->data, ((DetectContentData *)m->ctx)->content, hd->data_len); /* Okay we need to replace the type to HTTP_COOKIE from CONTENT */ - free(((DetectContentData *)m->ctx)->content); - free(m->ctx); + SCFree(((DetectContentData *)m->ctx)->content); + SCFree(m->ctx); m->type = DETECT_AL_HTTP_COOKIE; m->ctx = (void *)hd; @@ -215,10 +215,10 @@ int DetectHttpCookieSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, error: if (hd != NULL) { if (hd->data != NULL) - free(hd->data); - free(hd); + SCFree(hd->data); + SCFree(hd); } - if(sm !=NULL) free(sm); + if(sm !=NULL) SCFree(sm); return -1; } @@ -418,6 +418,7 @@ static int DetectHttpCookieSigTest01(void) { Signature *s = NULL; ThreadVars th_v; DetectEngineThreadCtx *det_ctx; + HtpState *http_state = NULL; memset(&th_v, 0, sizeof(th_v)); memset(&p, 0, sizeof(p)); @@ -469,7 +470,7 @@ static int DetectHttpCookieSigTest01(void) { goto end; } - HtpState *http_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; + http_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; if (http_state == NULL) { printf("no http state: "); result = 0; @@ -490,8 +491,11 @@ static int DetectHttpCookieSigTest01(void) { result = 1; end: + if (http_state != NULL) + HTPStateFree(http_state); if (de_ctx != NULL) SigGroupCleanup(de_ctx); if (de_ctx != NULL) SigCleanSignatures(de_ctx); + if (det_ctx != NULL) DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx); if (de_ctx != NULL) DetectEngineCtxFree(de_ctx); StreamL7DataPtrFree(&ssn); @@ -510,6 +514,7 @@ static int DetectHttpCookieSigTest02(void) { Signature *s = NULL; ThreadVars th_v; DetectEngineThreadCtx *det_ctx; + HtpState *http_state = NULL; memset(&th_v, 0, sizeof(th_v)); memset(&p, 0, sizeof(p)); @@ -554,7 +559,7 @@ static int DetectHttpCookieSigTest02(void) { goto end; } - HtpState *http_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; + http_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; if (http_state == NULL) { printf("no http state: "); result = 0; @@ -570,6 +575,8 @@ static int DetectHttpCookieSigTest02(void) { result = 1; end: + if (http_state != NULL) + HTPStateFree(http_state); if (de_ctx != NULL) SigGroupCleanup(de_ctx); if (de_ctx != NULL) SigCleanSignatures(de_ctx); if (de_ctx != NULL) DetectEngineCtxFree(de_ctx); diff --git a/src/detect-http-method.c b/src/detect-http-method.c index c2bb356238..fa375e6cad 100644 --- a/src/detect-http-method.c +++ b/src/detect-http-method.c @@ -173,18 +173,18 @@ int DetectHttpMethodSetup(DetectEngineCtx *de_ctx, Signature *s, SCReturnInt(-1); } - data = malloc(sizeof(DetectHttpMethodData)); + data = SCMalloc(sizeof(DetectHttpMethodData)); if (data == NULL) { // XXX: Should we bother with an error - it may fail too? - SCLogError(SC_ERR_MEM_ALLOC, "malloc failed"); + SCLogError(SC_ERR_MEM_ALLOC, "SCMalloc failed"); goto error; } data->content_len = ((DetectContentData *)m->ctx)->content_len; - data->content = malloc(data->content_len); + data->content = SCMalloc(data->content_len); if (data->content == NULL) { // XXX: Should we bother with an error - it may fail too? - SCLogError(SC_ERR_MEM_ALLOC, "malloc failed"); + SCLogError(SC_ERR_MEM_ALLOC, "SCMalloc failed"); goto error; } memcpy(data->content, @@ -194,8 +194,8 @@ int DetectHttpMethodSetup(DetectEngineCtx *de_ctx, Signature *s, data->method = htp_convert_method_to_number(method); /* Okay we need to replace the type to HTTP_METHOD from CONTENT */ - free(((DetectContentData *)m->ctx)->content); - free(m->ctx); + SCFree(((DetectContentData *)m->ctx)->content); + SCFree(m->ctx); m->type = DETECT_AL_HTTP_METHOD; m->ctx = (void *)data; @@ -206,7 +206,7 @@ int DetectHttpMethodSetup(DetectEngineCtx *de_ctx, Signature *s, error: if (data != NULL) DetectHttpMethodFree(data); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); SCReturnInt(-1); } @@ -218,8 +218,8 @@ error: void DetectHttpMethodFree(void *ptr) { DetectHttpMethodData *data = (DetectHttpMethodData *)ptr; - if (data->content != NULL) free(data->content); - free(data); + if (data->content != NULL) SCFree(data->content); + SCFree(data); } #ifdef UNITTESTS /* UNITTESTS */ @@ -371,6 +371,7 @@ static int DetectHttpMethodSigTest01(void) Signature *s = NULL; ThreadVars th_v; DetectEngineThreadCtx *det_ctx; + HtpState *http_state = NULL; memset(&th_v, 0, sizeof(th_v)); memset(&p, 0, sizeof(p)); @@ -425,7 +426,7 @@ static int DetectHttpMethodSigTest01(void) goto end; } - HtpState *http_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; + http_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; if (http_state == NULL) { SCLogDebug("no http state: "); goto end; @@ -443,6 +444,7 @@ static int DetectHttpMethodSigTest01(void) result = 1; end: + if (http_state != NULL) HTPStateFree(http_state); if (de_ctx != NULL) SigGroupCleanup(de_ctx); if (de_ctx != NULL) SigCleanSignatures(de_ctx); if (de_ctx != NULL) DetectEngineCtxFree(de_ctx); @@ -465,7 +467,8 @@ static int DetectHttpMethodSigTest02(void) Packet p; Signature *s = NULL; ThreadVars th_v; - DetectEngineThreadCtx *det_ctx; + DetectEngineThreadCtx *det_ctx = NULL; + HtpState *http_state = NULL; memset(&th_v, 0, sizeof(th_v)); memset(&p, 0, sizeof(p)); @@ -520,7 +523,7 @@ static int DetectHttpMethodSigTest02(void) goto end; } - HtpState *http_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; + http_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; if (http_state == NULL) { SCLogDebug("no http state: "); goto end; @@ -538,8 +541,10 @@ static int DetectHttpMethodSigTest02(void) result = 1; end: + if (http_state != NULL) HTPStateFree(http_state); if (de_ctx != NULL) SigGroupCleanup(de_ctx); if (de_ctx != NULL) SigCleanSignatures(de_ctx); + if (det_ctx != NULL) DetectEngineThreadCtxDeinit(&th_v, (void *) det_ctx); if (de_ctx != NULL) DetectEngineCtxFree(de_ctx); StreamL7DataPtrFree(&ssn); @@ -559,6 +564,7 @@ static int DetectHttpMethodSigTest03(void) Signature *s = NULL; ThreadVars th_v; DetectEngineThreadCtx *det_ctx; + HtpState *http_state = NULL; memset(&th_v, 0, sizeof(th_v)); memset(&p, 0, sizeof(p)); @@ -605,7 +611,7 @@ static int DetectHttpMethodSigTest03(void) goto end; } - HtpState *http_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; + http_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; if (http_state == NULL) { SCLogDebug("no http state: "); goto end; @@ -620,6 +626,7 @@ static int DetectHttpMethodSigTest03(void) result = 1; end: + if (http_state != NULL) HTPStateFree(http_state); if (de_ctx != NULL) SigGroupCleanup(de_ctx); if (de_ctx != NULL) SigCleanSignatures(de_ctx); if (de_ctx != NULL) DetectEngineCtxFree(de_ctx); diff --git a/src/detect-icmp-id.c b/src/detect-icmp-id.c index 90a345010d..f4f6db270a 100644 --- a/src/detect-icmp-id.c +++ b/src/detect-icmp-id.c @@ -142,7 +142,7 @@ DetectIcmpIdData *DetectIcmpIdParse (char *icmpidstr) { substr[i-1] = (char *)str_ptr; } - iid = malloc(sizeof(DetectIcmpIdData)); + iid = SCMalloc(sizeof(DetectIcmpIdData)); if (iid == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); goto error; @@ -163,13 +163,13 @@ DetectIcmpIdData *DetectIcmpIdParse (char *icmpidstr) { ByteExtractStringUint16(&iid->id, 10, 0, substr[1]); for (i = 0; i < 3; i++) { - if (substr[i] != NULL) free(substr[i]); + if (substr[i] != NULL) SCFree(substr[i]); } return iid; error: for (i = 0; i < 3; i++) { - if (substr[i] != NULL) free(substr[i]); + if (substr[i] != NULL) SCFree(substr[i]); } if (iid != NULL) DetectIcmpIdFree(iid); return NULL; @@ -206,7 +206,7 @@ int DetectIcmpIdSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char error: if (iid != NULL) DetectIcmpIdFree(iid); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); return -1; } @@ -218,7 +218,7 @@ error: */ void DetectIcmpIdFree (void *ptr) { DetectIcmpIdData *iid = (DetectIcmpIdData *)ptr; - free(iid); + SCFree(iid); } #ifdef UNITTESTS diff --git a/src/detect-icmp-seq.c b/src/detect-icmp-seq.c index b02f5b22b4..bcf15f2987 100644 --- a/src/detect-icmp-seq.c +++ b/src/detect-icmp-seq.c @@ -139,7 +139,7 @@ DetectIcmpSeqData *DetectIcmpSeqParse (char *icmpseqstr) { substr[i-1] = (char *)str_ptr; } - iseq = malloc(sizeof(DetectIcmpSeqData)); + iseq = SCMalloc(sizeof(DetectIcmpSeqData)); if (iseq == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); goto error; @@ -162,14 +162,14 @@ DetectIcmpSeqData *DetectIcmpSeqParse (char *icmpseqstr) { ByteExtractStringUint16(&iseq->seq, 10, 0, substr[1]); for (i = 0; i < 3; i++) { - if (substr[i] != NULL) free(substr[i]); + if (substr[i] != NULL) SCFree(substr[i]); } return iseq; error: for (i = 0; i < 3; i++) { - if (substr[i] != NULL) free(substr[i]); + if (substr[i] != NULL) SCFree(substr[i]); } if (iseq != NULL) DetectIcmpSeqFree(iseq); return NULL; @@ -206,7 +206,7 @@ int DetectIcmpSeqSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char error: if (iseq != NULL) DetectIcmpSeqFree(iseq); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); return -1; } @@ -218,7 +218,7 @@ error: */ void DetectIcmpSeqFree (void *ptr) { DetectIcmpSeqData *iseq = (DetectIcmpSeqData *)ptr; - free(iseq); + SCFree(iseq); } #ifdef UNITTESTS diff --git a/src/detect-icode.c b/src/detect-icode.c index 6488ed3474..aaa9d33d7b 100644 --- a/src/detect-icode.c +++ b/src/detect-icode.c @@ -140,7 +140,7 @@ DetectICodeData *DetectICodeParse(char *icodestr) { args[i-1] = (char *)str_ptr; } - icd = malloc(sizeof(DetectICodeData)); + icd = SCMalloc(sizeof(DetectICodeData)); if (icd == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); goto error; @@ -180,13 +180,13 @@ DetectICodeData *DetectICodeParse(char *icodestr) { } for (i = 0; i < (ret-1); i++) { - if (args[i] != NULL) free(args[i]); + if (args[i] != NULL) SCFree(args[i]); } return icd; error: for (i = 0; i < (ret-1); i++) { - if (args[i] != NULL) free(args[i]); + if (args[i] != NULL) SCFree(args[i]); } if (icd != NULL) DetectICodeFree(icd); return NULL; @@ -223,7 +223,7 @@ int DetectICodeSetup(DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char *i error: if (icd != NULL) DetectICodeFree(icd); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); return -1; } @@ -234,7 +234,7 @@ error: */ void DetectICodeFree(void *ptr) { DetectICodeData *icd = (DetectICodeData *)ptr; - free(icd); + SCFree(icd); } #ifdef UNITTESTS diff --git a/src/detect-id.c b/src/detect-id.c index d814512b5a..270b8c4054 100644 --- a/src/detect-id.c +++ b/src/detect-id.c @@ -144,13 +144,13 @@ DetectIdData *DetectIdParse (char *idstr) } /* We have a correct id option */ - id_d = malloc(sizeof(DetectIdData)); + id_d = SCMalloc(sizeof(DetectIdData)); if (id_d == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "malloc failed"); goto error; } - orig = strdup((char*)str_ptr); + orig = SCStrdup((char*)str_ptr); tmp_str=orig; /* Let's see if we need to scape "'s */ if (tmp_str[0] == '"') @@ -167,12 +167,12 @@ DetectIdData *DetectIdParse (char *idstr) "the range %u - %u", DETECT_IPID_MIN, DETECT_IPID_MAX); - free(orig); + SCFree(orig); goto error; } id_d->id = temp; - free(orig); + SCFree(orig); SCLogDebug("detect-id: will look for ip_id: %u\n", id_d->id); } @@ -221,7 +221,7 @@ int DetectIdSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, error: if (id_d != NULL) DetectIdFree(id_d); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); return -1; } @@ -233,7 +233,7 @@ error: */ void DetectIdFree(void *ptr) { DetectIdData *id_d = (DetectIdData *)ptr; - free(id_d); + SCFree(id_d); } #ifdef UNITTESTS /* UNITTESTS */ diff --git a/src/detect-ipopts.c b/src/detect-ipopts.c index be8d89af1b..cf09b79da3 100644 --- a/src/detect-ipopts.c +++ b/src/detect-ipopts.c @@ -138,7 +138,7 @@ DetectIpOptsData *DetectIpOptsParse (char *rawstr) if(found == 0) goto error; - de = malloc(sizeof(DetectIpOptsData)); + de = SCMalloc(sizeof(DetectIpOptsData)); if (de == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "malloc failed"); goto error; @@ -149,7 +149,7 @@ DetectIpOptsData *DetectIpOptsParse (char *rawstr) return de; error: - if (de) free(de); + if (de) SCFree(de); return NULL; } @@ -185,8 +185,8 @@ int DetectIpOptsSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char return 0; error: - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return -1; } @@ -198,7 +198,7 @@ error: */ void DetectIpOptsFree(void *de_ptr) { DetectIpOptsData *de = (DetectIpOptsData *)de_ptr; - if(de) free(de); + if(de) SCFree(de); } /* @@ -281,8 +281,8 @@ int IpOptsTestParse03 (void) { return 1; error: - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 0; } @@ -327,8 +327,8 @@ int IpOptsTestParse04 (void) { return 1; error: - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return 0; } #endif /* UNITTESTS */ diff --git a/src/detect-ipproto.c b/src/detect-ipproto.c index a6a31f1b83..849dcaa0fc 100644 --- a/src/detect-ipproto.c +++ b/src/detect-ipproto.c @@ -108,7 +108,7 @@ static DetectIPProtoData *DetectIPProtoParse(const char *optstr) } /* Initialize the data */ - data = malloc(sizeof(DetectIPProtoData)); + data = SCMalloc(sizeof(DetectIPProtoData)); if (data == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); goto error; @@ -138,15 +138,15 @@ static DetectIPProtoData *DetectIPProtoParse(const char *optstr) } for (i = 0; i < (ret - 1); i++){ - if (args[i] != NULL) free(args[i]); + if (args[i] != NULL) SCFree(args[i]); } return data; error: for (i = 0; i < (ret - 1); i++){ - if (args[i] != NULL) free(args[i]); + if (args[i] != NULL) SCFree(args[i]); } - if (data != NULL) free(data); + if (data != NULL) SCFree(data); return NULL; } @@ -213,7 +213,7 @@ static int DetectIPProtoSetup(DetectEngineCtx *de_ctx, Signature *s, ret = 0; cleanup: - if (data != NULL) free(data); + if (data != NULL) SCFree(data); return ret; } @@ -272,7 +272,7 @@ static int DetectIPProtoTestParse01(void) { result = 1; } - if (data) free(data); + if (data) SCFree(data); return result; } @@ -288,7 +288,7 @@ static int DetectIPProtoTestParse02(void) { result = 1; } - if (data) free(data); + if (data) SCFree(data); return result; } @@ -333,7 +333,7 @@ static int DetectIPProtoTestSetup01(void) { result = 1; cleanup: - if (data) free(data); + if (data) SCFree(data); SigGroupCleanup(de_ctx); SigCleanSignatures(de_ctx); DetectEngineCtxFree(de_ctx); @@ -375,7 +375,7 @@ static int DetectIPProtoTestSetup02(void) { result = 1; cleanup: - if (data) free(data); + if (data) SCFree(data); SigGroupCleanup(de_ctx); SigCleanSignatures(de_ctx); DetectEngineCtxFree(de_ctx); @@ -418,7 +418,7 @@ static int DetectIPProtoTestSetup03(void) { result = 1; cleanup: - if (data) free(data); + if (data) SCFree(data); SigGroupCleanup(de_ctx); SigCleanSignatures(de_ctx); DetectEngineCtxFree(de_ctx); @@ -465,7 +465,7 @@ static int DetectIPProtoTestSetup04(void) { result = 1; cleanup: - if (data) free(data); + if (data) SCFree(data); SigGroupCleanup(de_ctx); SigCleanSignatures(de_ctx); DetectEngineCtxFree(de_ctx); @@ -509,7 +509,7 @@ static int DetectIPProtoTestSetup05(void) { result = 1; cleanup: - if (data) free(data); + if (data) SCFree(data); SigGroupCleanup(de_ctx); SigCleanSignatures(de_ctx); DetectEngineCtxFree(de_ctx); diff --git a/src/detect-isdataat.c b/src/detect-isdataat.c index bcaeef72e0..773ea6b42c 100644 --- a/src/detect-isdataat.c +++ b/src/detect-isdataat.c @@ -150,7 +150,7 @@ DetectIsdataatData *DetectIsdataatParse (char *isdataatstr) args[2] = (char *)str_ptr; } - idad = malloc(sizeof(DetectIsdataatData)); + idad = SCMalloc(sizeof(DetectIsdataatData)); if (idad == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "malloc failed"); goto error; @@ -163,8 +163,8 @@ DetectIsdataatData *DetectIsdataatParse (char *isdataatstr) if (ByteExtractStringUint16(&idad->dataat, 10, strlen(args[0]), args[0]) < 0 ) { SCLogError(SC_ERR_INVALID_VALUE, "isdataat out of range"); - free(idad); - idad=NULL; + SCFree(idad); + idad = NULL; goto error; } } else { @@ -179,7 +179,7 @@ DetectIsdataatData *DetectIsdataatParse (char *isdataatstr) } for (i = 0; i < (ret -1); i++) { - if (args[i] != NULL) free(args[i]); + if (args[i] != NULL) SCFree(args[i]); } return idad; @@ -189,7 +189,7 @@ DetectIsdataatData *DetectIsdataatParse (char *isdataatstr) error: for (i = 0; i < (ret -1); i++){ - if (args[i] != NULL) free(args[i]); + if (args[i] != NULL) SCFree(args[i]); } if (idad != NULL) DetectIsdataatFree(idad); @@ -266,7 +266,7 @@ int DetectIsdataatSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, cha error: if (idad != NULL) DetectIsdataatFree(idad); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); return -1; } @@ -278,7 +278,7 @@ error: */ void DetectIsdataatFree(void *ptr) { DetectIsdataatData *idad = (DetectIsdataatData *)ptr; - free(idad); + SCFree(idad); } diff --git a/src/detect-itype.c b/src/detect-itype.c index 42ca09727e..06c21c679c 100644 --- a/src/detect-itype.c +++ b/src/detect-itype.c @@ -140,7 +140,7 @@ DetectITypeData *DetectITypeParse(char *itypestr) { args[i-1] = (char *)str_ptr; } - itd = malloc(sizeof(DetectITypeData)); + itd = SCMalloc(sizeof(DetectITypeData)); if (itd == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); goto error; @@ -180,13 +180,13 @@ DetectITypeData *DetectITypeParse(char *itypestr) { } for (i = 0; i < (ret-1); i++) { - if (args[i] != NULL) free(args[i]); + if (args[i] != NULL) SCFree(args[i]); } return itd; error: for (i = 0; i < (ret-1); i++) { - if (args[i] != NULL) free(args[i]); + if (args[i] != NULL) SCFree(args[i]); } if (itd != NULL) DetectITypeFree(itd); return NULL; @@ -223,7 +223,7 @@ int DetectITypeSetup(DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char *i error: if (itd != NULL) DetectITypeFree(itd); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); return -1; } @@ -234,7 +234,7 @@ error: */ void DetectITypeFree(void *ptr) { DetectITypeData *itd = (DetectITypeData *)ptr; - free(itd); + SCFree(itd); } #ifdef UNITTESTS diff --git a/src/detect-metadata.c b/src/detect-metadata.c index 0f1119db7d..41500b82d8 100644 --- a/src/detect-metadata.c +++ b/src/detect-metadata.c @@ -20,14 +20,14 @@ int DetectMetadataSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, cha /* strip "'s */ if (rawstr[0] == '\"' && rawstr[strlen(rawstr)-1] == '\"') { - str = strdup(rawstr+1); + str = SCStrdup(rawstr+1); str[strlen(rawstr)-2] = '\0'; dubbed = 1; } /* XXX */ - if (dubbed) free(str); + if (dubbed) SCFree(str); return 0; } diff --git a/src/detect-msg.c b/src/detect-msg.c index 61110e7453..6085ce9976 100644 --- a/src/detect-msg.c +++ b/src/detect-msg.c @@ -32,11 +32,11 @@ int DetectMsgSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char *ms /* strip "'s */ if (msgstr[0] == '\"' && msgstr[strlen(msgstr)-1] == '\"') { - str = strdup(msgstr+1); + str = SCStrdup(msgstr+1); str[strlen(msgstr)-2] = '\0'; } else if (msgstr[1] == '\"' && msgstr[strlen(msgstr)-1] == '\"') { /* XXX do this parsing in a better way */ - str = strdup(msgstr+2); + str = SCStrdup(msgstr+2); str[strlen(msgstr)-3] = '\0'; //printf("DetectMsgSetup: format hack applied: \'%s\'\n", str); } else { @@ -93,17 +93,17 @@ int DetectMsgSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char *ms } } - s->msg = malloc(len + 1); + s->msg = SCMalloc(len + 1); if (s->msg == NULL) goto error; strlcpy(s->msg, str, len + 1); - free(str); + SCFree(str); return 0; error: - free(str); + SCFree(str); return -1; } diff --git a/src/detect-offset.c b/src/detect-offset.c index 3135f7d6c6..37a571bfab 100644 --- a/src/detect-offset.c +++ b/src/detect-offset.c @@ -32,7 +32,7 @@ int DetectOffsetSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char /* strip "'s */ if (offsetstr[0] == '\"' && offsetstr[strlen(offsetstr)-1] == '\"') { - str = strdup(offsetstr+1); + str = SCStrdup(offsetstr+1); str[strlen(offsetstr)-2] = '\0'; dubbed = 1; } @@ -42,14 +42,14 @@ int DetectOffsetSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char SigMatch *pm = DetectContentFindPrevApplicableSM(m); if (pm == NULL) { SCLogError(SC_ERR_OFFSET_MISSING_CONTENT, "offset needs a preceeding content option"); - if (dubbed) free(str); + if (dubbed) SCFree(str); return -1; } DetectContentData *cd = (DetectContentData *)pm->ctx; if (cd == NULL) { SCLogError(SC_ERR_INVALID_ARGUMENT, "invalid argument"); - if (dubbed) free(str); + if (dubbed) SCFree(str); return -1; } @@ -68,7 +68,7 @@ int DetectOffsetSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char if (cd->flags & DETECT_CONTENT_IS_CHUNK) DetectContentPropagateOffset(pm); - if (dubbed) free(str); + if (dubbed) SCFree(str); return 0; } diff --git a/src/detect-parse.c b/src/detect-parse.c index 3de59cd076..35caf0770c 100644 --- a/src/detect-parse.c +++ b/src/detect-parse.c @@ -64,7 +64,7 @@ uint32_t DbgGetDstPortAnyCnt(void) { } SigMatch *SigMatchAlloc(void) { - SigMatch *sm = malloc(sizeof(SigMatch)); + SigMatch *sm = SCMalloc(sizeof(SigMatch)); if (sm == NULL) return NULL; @@ -87,7 +87,7 @@ void SigMatchFree(SigMatch *sm) { sigmatch_table[sm->type].Free(sm->ctx); } } - free(sm); + SCFree(sm); } /* Get the detection module by name */ @@ -225,7 +225,7 @@ int SigParseOptions(DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char *op SigTableElmt *st = NULL; char *optname = NULL, *optvalue = NULL, *optmore = NULL; - const char **arr = calloc(OPTION_PARTS+1, sizeof(char *)); + const char **arr = SCCalloc(OPTION_PARTS+1, sizeof(char *)); if (arr == NULL) return -1; @@ -289,25 +289,25 @@ int SigParseOptions(DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char *op if (optname) pcre_free_substring(optname); if (optvalue) pcre_free_substring(optvalue); - if (optstr) free(optstr); + if (optstr) SCFree(optstr); //if (optmore) pcre_free_substring(optmore); - if (arr != NULL) free(arr); + if (arr != NULL) SCFree(arr); return SigParseOptions(de_ctx, s, m, optmore); } if (optname) pcre_free_substring(optname); if (optvalue) pcre_free_substring(optvalue); if (optmore) pcre_free_substring(optmore); - if (optstr) free(optstr); - if (arr != NULL) free(arr); + if (optstr) SCFree(optstr); + if (arr != NULL) SCFree(arr); return 0; error: if (optname) pcre_free_substring(optname); if (optvalue) pcre_free_substring(optvalue); if (optmore) pcre_free_substring(optmore); - if (optstr) free(optstr); - if (arr != NULL) free(arr); + if (optstr) SCFree(optstr); + if (arr != NULL) SCFree(arr); return -1; } @@ -451,7 +451,7 @@ int SigParseBasics(Signature *s, char *sigstr, char ***result, uint8_t addrs_dir int ov[MAX_SUBSTRINGS]; int ret = 0, i = 0; - const char **arr = calloc(CONFIG_PARTS + 1, sizeof(char *)); + const char **arr = SCCalloc(CONFIG_PARTS + 1, sizeof(char *)); if (arr == NULL) return -1; @@ -506,7 +506,7 @@ error: pcre_free_substring(arr[i - 1]); } - free(arr); + SCFree(arr); } *result = NULL; return -1; @@ -534,7 +534,7 @@ int SigParse(DetectEngineCtx *de_ctx, Signature *s, char *sigstr, uint8_t addrs_ /* we can have no options, so make sure we have them */ if (basics[CONFIG_OPTS] != NULL) { - ret = SigParseOptions(de_ctx, s, NULL, strdup(basics[CONFIG_OPTS])); + ret = SigParseOptions(de_ctx, s, NULL, SCStrdup(basics[CONFIG_OPTS])); SCLogDebug("ret from SigParseOptions %d", ret); } @@ -542,17 +542,17 @@ int SigParse(DetectEngineCtx *de_ctx, Signature *s, char *sigstr, uint8_t addrs_ if (basics != NULL) { int i = 0; while (basics[i] != NULL) { - free(basics[i]); + SCFree(basics[i]); i++; } - free(basics); + SCFree(basics); } SCReturnInt(ret); } Signature *SigAlloc (void) { - Signature *sig = malloc(sizeof(Signature)); + Signature *sig = SCMalloc(sizeof(Signature)); if (sig == NULL) return NULL; @@ -586,9 +586,9 @@ void SigFree(Signature *s) { DetectPortCleanupList(s->dp); } - if (s->msg != NULL) free(s->msg); + if (s->msg != NULL) SCFree(s->msg); - free(s); + SCFree(s); } /** @@ -1194,30 +1194,31 @@ end: */ int SigTestBidirec01 (void) { Signature *sig = NULL; + int result = 0; DetectEngineCtx *de_ctx = DetectEngineCtxInit(); if (de_ctx == NULL) - goto error; + goto end; sig = DetectEngineAppendSig(de_ctx, "alert tcp 1.2.3.4 1024:65535 -> !1.2.3.4 any (msg:\"SigTestBidirec01\"; sid:1;)"); if (sig == NULL) - goto error; + goto end; if (sig->next != NULL) - goto error; + goto end; if (sig->flags & SIG_FLAG_BIDIREC) - goto error; + goto end; if (de_ctx->signum != 1) - goto error; + goto end; - return 1; -error: - if (sig != NULL) { - if (sig->next != NULL) - SigFree(sig->next); - SigFree(sig); + result = 1; + +end: + if (de_ctx != NULL) { + SigCleanSignatures(de_ctx); + SigGroupCleanup(de_ctx); + DetectEngineCtxFree(de_ctx); } - if (de_ctx != NULL) DetectEngineCtxFree(de_ctx); - return 0; + return result; } /** \test Ensure that we set a bidirectional Signature correctly */ @@ -1252,7 +1253,12 @@ int SigTestBidirec02 (void) { result = 1; end: - if (de_ctx != NULL) DetectEngineCtxFree(de_ctx); + if (de_ctx != NULL) { + SigCleanSignatures(de_ctx); + SigGroupCleanup(de_ctx); + DetectEngineCtxFree(de_ctx); + } + return result; } @@ -1360,9 +1366,15 @@ int SigTestBidirec03 (void) { uint32_t results[3] = {1, 1, 1}; result = UTHCheckPacketMatchResults(p, sids, results, 1); - FlowShutdown(); end: - if (de_ctx != NULL) DetectEngineCtxFree(de_ctx); + if (p != NULL) SCFree(p); + if (de_ctx != NULL) { + SigCleanSignatures(de_ctx); + SigGroupCleanup(de_ctx); + DetectEngineCtxFree(de_ctx); + } + FlowShutdown(); + return result; } diff --git a/src/detect-pcre.c b/src/detect-pcre.c index 8013aa976c..e11e37fc3c 100644 --- a/src/detect-pcre.c +++ b/src/detect-pcre.c @@ -413,7 +413,7 @@ DetectPcreData *DetectPcreParse (char *regexstr) } //printf("ret %" PRId32 " re \'%s\', op \'%s\'\n", ret, re, op); - pd = malloc(sizeof(DetectPcreData)); + pd = SCMalloc(sizeof(DetectPcreData)); if (pd == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "malloc failed"); goto error; @@ -490,7 +490,7 @@ DetectPcreData *DetectPcreParse (char *regexstr) } if(pd->sd == NULL) - pd->sd = (pcre_extra *) calloc(1,sizeof(pcre_extra)); + pd->sd = (pcre_extra *) SCCalloc(1,sizeof(pcre_extra)); if(pd->sd) { @@ -519,16 +519,16 @@ DetectPcreData *DetectPcreParse (char *regexstr) } - if (re != NULL) free(re); - if (op_ptr != NULL) free(op_ptr); + if (re != NULL) SCFree(re); + if (op_ptr != NULL) SCFree(op_ptr); return pd; error: - if (re != NULL) free(re); - if (op_ptr != NULL) free(op_ptr); + if (re != NULL) SCFree(re); + if (op_ptr != NULL) SCFree(op_ptr); if (pd != NULL && pd->re != NULL) pcre_free(pd->re); if (pd != NULL && pd->sd != NULL) pcre_free(pd->sd); - if (pd) free(pd); + if (pd) SCFree(pd); return NULL; } @@ -562,7 +562,7 @@ DetectPcreData *DetectPcreParseCapture(char *regexstr, DetectEngineCtx *de_ctx, //printf("DetectPcreParseCapture: capture \'%s\'\n", capture_str_ptr ? capture_str_ptr : "NULL"); if (capture_str_ptr != NULL) { - pd->capname = strdup((char *)capture_str_ptr); + pd->capname = SCStrdup((char *)capture_str_ptr); } if (type_str_ptr != NULL) { @@ -585,8 +585,8 @@ DetectPcreData *DetectPcreParseCapture(char *regexstr, DetectEngineCtx *de_ctx, return pd; error: - if (pd != NULL && pd->capname != NULL) free(pd->capname); - if (pd) free(pd); + if (pd != NULL && pd->capname != NULL) SCFree(pd->capname); + if (pd) SCFree(pd); return NULL; } @@ -623,18 +623,18 @@ int DetectPcreSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char *r error: if (pd != NULL) DetectPcreFree(pd); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); return -1; } void DetectPcreFree(void *ptr) { DetectPcreData *pd = (DetectPcreData *)ptr; - if (pd->capname != NULL) free(pd->capname); + if (pd->capname != NULL) SCFree(pd->capname); if (pd->re != NULL) pcre_free(pd->re); if (pd->sd != NULL) pcre_free(pd->sd); - free(pd); + SCFree(pd); return; } diff --git a/src/detect-pktvar.c b/src/detect-pktvar.c index 22a5456fdd..363f90a21d 100644 --- a/src/detect-pktvar.c +++ b/src/detect-pktvar.c @@ -111,18 +111,18 @@ int DetectPktvarSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char SCLogDebug("varname %s, varcontent %s", varname, varcontent); if (varcontent[0] == '\"' && varcontent[strlen(varcontent)-1] == '\"') { - str = strdup(varcontent+1); + str = SCStrdup(varcontent+1); str[strlen(varcontent)-2] = '\0'; dubbed = 1; } len = strlen(str); if (len == 0) { - if (dubbed) free(str); + if (dubbed) SCFree(str); return -1; } - cd = malloc(sizeof(DetectPktvarData)); + cd = SCMalloc(sizeof(DetectPktvarData)); if (cd == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "malloc failed"); goto error; @@ -185,14 +185,14 @@ int DetectPktvarSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char len = x; } - cd->content = malloc(len); + cd->content = SCMalloc(len); if (cd->content == NULL) { - free(cd); - if (dubbed) free(str); + SCFree(cd); + if (dubbed) SCFree(str); return -1; } - cd->name = strdup(varname); + cd->name = SCStrdup(varname); memcpy(cd->content, str, len); cd->content_len = len; cd->flags = 0; @@ -208,13 +208,13 @@ int DetectPktvarSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char SigMatchAppend(s,m,sm); - if (dubbed) free(str); + if (dubbed) SCFree(str); return 0; error: - if (dubbed) free(str); - if (cd) free(cd); - if (sm) free(sm); + if (dubbed) SCFree(str); + if (cd) SCFree(cd); + if (sm) SCFree(sm); return -1; } diff --git a/src/detect-reference.c b/src/detect-reference.c index d8454cb9bf..a4fda053c7 100644 --- a/src/detect-reference.c +++ b/src/detect-reference.c @@ -20,14 +20,14 @@ int DetectReferenceSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, ch /* strip "'s */ if (rawstr[0] == '\"' && rawstr[strlen(rawstr)-1] == '\"') { - str = strdup(rawstr+1); + str = SCStrdup(rawstr+1); str[strlen(rawstr)-2] = '\0'; dubbed = 1; } /* XXX */ - if (dubbed) free(str); + if (dubbed) SCFree(str); return 0; } diff --git a/src/detect-rev.c b/src/detect-rev.c index abce2ccb05..a1bce1d5e3 100644 --- a/src/detect-rev.c +++ b/src/detect-rev.c @@ -2,6 +2,8 @@ #include "suricata-common.h" #include "detect.h" +#include "util-debug.h" +#include "util-error.h" int DetectRevSetup (DetectEngineCtx *, Signature *s, SigMatch *m, char *str); @@ -20,14 +22,14 @@ int DetectRevSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char *ra /* strip "'s */ if (rawstr[0] == '\"' && rawstr[strlen(rawstr)-1] == '\"') { - str = strdup(rawstr+1); + str = SCStrdup(rawstr+1); str[strlen(rawstr)-2] = '\0'; dubbed = 1; } s->rev = (uint8_t)atoi(str); - if (dubbed) free(str); + if (dubbed) SCFree(str); return 0; } diff --git a/src/detect-rpc.c b/src/detect-rpc.c index a277caefc0..76c7d473c1 100644 --- a/src/detect-rpc.c +++ b/src/detect-rpc.c @@ -180,7 +180,7 @@ DetectRpcData *DetectRpcParse (char *rpcstr) } } - rd = malloc(sizeof(DetectRpcData)); + rd = SCMalloc(sizeof(DetectRpcData)); if (rd == NULL) { SCLogError(SC_ERR_MEM_ALLOC ,"malloc failed"); goto error; @@ -226,13 +226,13 @@ DetectRpcData *DetectRpcParse (char *rpcstr) } } for (i = 0; i < (ret -1); i++){ - if (args[i] != NULL) free(args[i]); + if (args[i] != NULL) SCFree(args[i]); } return rd; error: for (i = 0; i < (ret -1); i++){ - if (args[i] != NULL) free(args[i]); + if (args[i] != NULL) SCFree(args[i]); } if (rd != NULL) DetectRpcFree(rd); return NULL; @@ -271,7 +271,7 @@ int DetectRpcSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char *rp error: if (rd != NULL) DetectRpcFree(rd); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); return -1; } @@ -283,7 +283,7 @@ error: */ void DetectRpcFree(void *ptr) { DetectRpcData *rd = (DetectRpcData *)ptr; - free(rd); + SCFree(rd); } #ifdef UNITTESTS diff --git a/src/detect-sameip.c b/src/detect-sameip.c index 95fb2701ce..47de9736a3 100644 --- a/src/detect-sameip.c +++ b/src/detect-sameip.c @@ -90,7 +90,7 @@ static int DetectSameipSetup(DetectEngineCtx *de_ctx, Signature *s, error: if (sm != NULL) - free(sm); + SCFree(sm); return -1; } diff --git a/src/detect-seq.c b/src/detect-seq.c index 46b735074b..ac3dd43d26 100644 --- a/src/detect-seq.c +++ b/src/detect-seq.c @@ -84,7 +84,7 @@ static int DetectSeqSetup (DetectEngineCtx *de_ctx, Signature *s, //printf("DetectSeqSetup: \'%s\'\n", optstr); - data = malloc(sizeof(DetectSeqData)); + data = SCMalloc(sizeof(DetectSeqData)); if (data == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "malloc failed"); goto error; @@ -107,7 +107,7 @@ static int DetectSeqSetup (DetectEngineCtx *de_ctx, Signature *s, return 0; error: - if (data) free(data); + if (data) SCFree(data); return -1; } @@ -121,7 +121,7 @@ error: static void DetectSeqFree(void *ptr) { DetectSeqData *data = (DetectSeqData *)ptr; - free(data); + SCFree(data); } diff --git a/src/detect-sid.c b/src/detect-sid.c index 5b6c02b7bb..ffed73a396 100644 --- a/src/detect-sid.c +++ b/src/detect-sid.c @@ -2,6 +2,8 @@ #include "suricata-common.h" #include "detect.h" +#include "util-debug.h" +#include "util-error.h" int DetectSidSetup (DetectEngineCtx *, Signature *s, SigMatch *m, char *sidstr); @@ -20,14 +22,14 @@ int DetectSidSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char *si /* strip "'s */ if (sidstr[0] == '\"' && sidstr[strlen(sidstr)-1] == '\"') { - str = strdup(sidstr+1); + str = SCStrdup(sidstr+1); str[strlen(sidstr)-2] = '\0'; dubbed = 1; } s->id = (uint32_t)atoi(str); - if (dubbed) free(str); + if (dubbed) SCFree(str); return 0; } diff --git a/src/detect-stream_size.c b/src/detect-stream_size.c index 653b9d73ca..b73526befd 100644 --- a/src/detect-stream_size.c +++ b/src/detect-stream_size.c @@ -57,8 +57,8 @@ void DetectStreamSizeRegister(void) { return; error: - if (parse_regex != NULL) free(parse_regex); - if (parse_regex_study != NULL) free(parse_regex_study); + if (parse_regex != NULL) SCFree(parse_regex); + if (parse_regex_study != NULL) SCFree(parse_regex_study); return; } @@ -209,7 +209,7 @@ DetectStreamSizeData *DetectStreamSizeParse (char *streamstr) { } value = (char *)str_ptr; - sd = malloc(sizeof(DetectStreamSizeData)); + sd = SCMalloc(sizeof(DetectStreamSizeData)); if (sd == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "malloc failed"); goto error; @@ -269,15 +269,15 @@ DetectStreamSizeData *DetectStreamSizeParse (char *streamstr) { goto error; } - if (mode != NULL) free(mode); - if (arg != NULL) free(arg); - if (value != NULL) free(value); + if (mode != NULL) SCFree(mode); + if (arg != NULL) SCFree(arg); + if (value != NULL) SCFree(value); return sd; error: - if (mode != NULL) free(mode); - if (arg != NULL) free(arg); - if (value != NULL) free(value); + if (mode != NULL) SCFree(mode); + if (arg != NULL) SCFree(arg); + if (value != NULL) SCFree(value); if (sd != NULL) DetectStreamSizeFree(sd); return NULL; @@ -316,7 +316,7 @@ int DetectStreamSizeSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, c error: if (sd != NULL) DetectStreamSizeFree(sd); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); return -1; } @@ -327,7 +327,7 @@ error: */ void DetectStreamSizeFree(void *ptr) { DetectStreamSizeData *sd = (DetectStreamSizeData *)ptr; - free(sd); + SCFree(sd); } #ifdef UNITTESTS @@ -400,16 +400,19 @@ static int DetectStreamSizeParseTest03 (void) { if (sd != NULL) { if (!(sd->flags & STREAM_SIZE_CLIENT)) { printf("sd->flags not STREAM_SIZE_CLIENT: "); + DetectStreamSizeFree(sd); return 0; } if (sd->mode != DETECTSSIZE_GT) { printf("sd->mode not DETECTSSIZE_GT: "); + DetectStreamSizeFree(sd); return 0; } if (sd->ssize != 8) { printf("sd->ssize is %"PRIu32", not 8: ", sd->ssize); + DetectStreamSizeFree(sd); return 0; } } else { @@ -429,6 +432,7 @@ static int DetectStreamSizeParseTest03 (void) { if (result == 0) { printf("result 0 != 1: "); } + DetectStreamSizeFree(sd); return result; } diff --git a/src/detect-threshold.c b/src/detect-threshold.c index 3cfa8873be..ce8f6522a8 100644 --- a/src/detect-threshold.c +++ b/src/detect-threshold.c @@ -90,7 +90,7 @@ static DetectThresholdData *DetectThresholdParse (char *rawstr) uint16_t pos = 0; int i = 0; - copy_str = strdup(rawstr); + copy_str = SCStrdup(rawstr); for(pos = 0, threshold_opt = strtok(copy_str,","); pos < strlen(copy_str) && threshold_opt != NULL; pos++, threshold_opt = strtok(NULL,",")) { @@ -105,7 +105,7 @@ static DetectThresholdData *DetectThresholdParse (char *rawstr) } if(copy_str) - free(copy_str); + SCFree(copy_str); if(count_found != 1 || second_found != 1 || type_found != 1 || track_found != 1) goto error; @@ -117,7 +117,7 @@ static DetectThresholdData *DetectThresholdParse (char *rawstr) goto error; } - de = malloc(sizeof(DetectThresholdData)); + de = SCMalloc(sizeof(DetectThresholdData)); if (de == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "malloc failed"); goto error; @@ -161,15 +161,15 @@ static DetectThresholdData *DetectThresholdParse (char *rawstr) } for (i = 0; i < (ret - 1); i++){ - if (args[i] != NULL) free(args[i]); + if (args[i] != NULL) SCFree(args[i]); } return de; error: for (i = 0; i < (ret - 1); i++){ - if (args[i] != NULL) free(args[i]); + if (args[i] != NULL) SCFree(args[i]); } - if (de) free(de); + if (de) SCFree(de); return NULL; } @@ -206,8 +206,8 @@ static int DetectThresholdSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch return 0; error: - if (de) free(de); - if (sm) free(sm); + if (de) SCFree(de); + if (sm) SCFree(sm); return -1; } @@ -219,7 +219,7 @@ error: */ static void DetectThresholdFree(void *de_ptr) { DetectThresholdData *de = (DetectThresholdData *)de_ptr; - if (de) free(de); + if (de) SCFree(de); } /* @@ -522,9 +522,9 @@ static int DetectThresholdTestSig3(void) { td = SigGetThresholdType(s,&p); /* setup the Entry we use to search our hash with */ - ste = malloc(sizeof(DetectThresholdEntry)); + ste = SCMalloc(sizeof(DetectThresholdEntry)); if (ste == NULL) { - SCLogError(SC_ERR_MEM_ALLOC, "malloc failed: %s", strerror(errno)); + SCLogError(SC_ERR_MEM_ALLOC, "SCMalloc failed: %s", strerror(errno)); goto end; } memset(ste, 0x00, sizeof(ste)); diff --git a/src/detect-tls-version.c b/src/detect-tls-version.c index d8b31fe37c..9821d24354 100644 --- a/src/detect-tls-version.c +++ b/src/detect-tls-version.c @@ -152,13 +152,13 @@ DetectTlsVersionData *DetectTlsVersionParse (char *str) } /* We have a correct id option */ - tls = malloc(sizeof(DetectTlsVersionData)); + tls = SCMalloc(sizeof(DetectTlsVersionData)); if (tls == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "malloc failed"); goto error; } - orig = strdup((char*)str_ptr); + orig = SCStrdup((char*)str_ptr); tmp_str=orig; /* Let's see if we need to scape "'s */ if (tmp_str[0] == '"') @@ -180,7 +180,7 @@ DetectTlsVersionData *DetectTlsVersionParse (char *str) tls->ver = temp; - free(orig); + SCFree(orig); SCLogDebug("will look for tls %"PRIu8"", tls->ver); } @@ -228,7 +228,7 @@ int DetectTlsVersionSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, c error: if (tls != NULL) DetectTlsVersionFree(tls); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); return -1; } @@ -240,7 +240,7 @@ error: */ void DetectTlsVersionFree(void *ptr) { DetectTlsVersionData *id_d = (DetectTlsVersionData *)ptr; - free(id_d); + SCFree(id_d); } #ifdef UNITTESTS /* UNITTESTS */ diff --git a/src/detect-ttl.c b/src/detect-ttl.c index f0e9c20bfa..8f4d428e93 100644 --- a/src/detect-ttl.c +++ b/src/detect-ttl.c @@ -55,8 +55,8 @@ void DetectTtlRegister(void) { return; error: - if (parse_regex != NULL) free(parse_regex); - if (parse_regex_study != NULL) free(parse_regex_study); + if (parse_regex != NULL) SCFree(parse_regex); + if (parse_regex_study != NULL) SCFree(parse_regex_study); return; } @@ -148,7 +148,7 @@ DetectTtlData *DetectTtlParse (char *ttlstr) { arg3 = (char *) str_ptr; SCLogDebug("Arg3 \"%s\"", arg3); - ttld = malloc(sizeof (DetectTtlData)); + ttld = SCMalloc(sizeof (DetectTtlData)); if (ttld == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "malloc failed"); goto error; @@ -199,16 +199,16 @@ DetectTtlData *DetectTtlParse (char *ttlstr) { break; } - free(arg1); - free(arg2); - free(arg3); + SCFree(arg1); + SCFree(arg2); + SCFree(arg3); return ttld; error: - if (ttld) free(ttld); - if (arg1) free(arg1); - if (arg2) free(arg2); - if (arg3) free(arg3); + if (ttld) SCFree(ttld); + if (arg1) SCFree(arg1); + if (arg2) SCFree(arg2); + if (arg3) SCFree(arg3); return NULL; } @@ -245,7 +245,7 @@ int DetectTtlSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char *tt error: if (ttld != NULL) DetectTtlFree(ttld); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); return -1; } @@ -256,7 +256,7 @@ error: */ void DetectTtlFree(void *ptr) { DetectTtlData *ttld = (DetectTtlData *)ptr; - free(ttld); + SCFree(ttld); } #ifdef UNITTESTS @@ -410,7 +410,7 @@ static int DetectTtlParseTest06 (void) { ttld = DetectTtlParse(" 1 = 2 "); if (ttld == NULL) res = 1; - if (ttld) free(ttld); + if (ttld) SCFree(ttld); return res; } @@ -428,7 +428,7 @@ static int DetectTtlParseTest07 (void) { if (ttld == NULL) res = 1; - if (ttld) free(ttld); + if (ttld) SCFree(ttld); return res; } @@ -461,7 +461,7 @@ static int DetectTtlSetpTest01(void) { } cleanup: - if (ttld) free(ttld); + if (ttld) SCFree(ttld); SigGroupCleanup(de_ctx); SigCleanSignatures(de_ctx); DetectEngineCtxFree(de_ctx); diff --git a/src/detect-uricontent.c b/src/detect-uricontent.c index 41b4d66e14..89d50e1a5b 100644 --- a/src/detect-uricontent.c +++ b/src/detect-uricontent.c @@ -72,14 +72,14 @@ uint32_t DetectUricontentMaxId(DetectEngineCtx *de_ctx) /** * \brief Free the stored http_uri in the given packet - * \param p pointer to the given packet whose uri has to be freed + * \param p pointer to the given packet whose uri has to be SCFreed */ void PktHttpUriFree(Packet *p) { int i; for (i = 0; i < p->http_uri.cnt; i++) { - free(p->http_uri.raw[i]); + SCFree(p->http_uri.raw[i]); p->http_uri.raw[i] = NULL; } p->http_uri.cnt = 0; @@ -277,17 +277,17 @@ DetectUricontentData *DoDetectUricontentSetup (char * contentstr) uint16_t pos = 0; uint16_t slen = 0; - if ((temp = strdup(contentstr)) == NULL) + if ((temp = SCStrdup(contentstr)) == NULL) goto error; if (strlen(temp) == 0) { - free(temp); + SCFree(temp); return NULL; } - cd = malloc(sizeof(DetectUricontentData)); + cd = SCMalloc(sizeof(DetectUricontentData)); if (cd == NULL) { - SCLogError(SC_ERR_MEM_ALLOC, "malloc failed"); + SCLogError(SC_ERR_MEM_ALLOC, "SCMalloc failed"); goto error; } memset(cd,0,sizeof(DetectUricontentData)); @@ -305,15 +305,15 @@ DetectUricontentData *DoDetectUricontentSetup (char * contentstr) } if (temp[pos] == '\"' && temp[strlen(temp)-1] == '\"') { - if ((str = strdup(temp + pos + 1)) == NULL) + if ((str = SCStrdup(temp + pos + 1)) == NULL) goto error; str[strlen(temp) - pos - 2] = '\0'; } else { - if ((str = strdup(temp + pos)) == NULL) + if ((str = SCStrdup(temp + pos)) == NULL) goto error; } - free(temp); + SCFree(temp); temp = NULL; len = strlen(str); @@ -378,10 +378,10 @@ DetectUricontentData *DoDetectUricontentSetup (char * contentstr) SCLogDebug("len %" PRIu32 "", len); - cd->uricontent = malloc(len); + cd->uricontent = SCMalloc(len); if (cd->uricontent == NULL) { - free(cd); - free(str); + SCFree(cd); + SCFree(str); return NULL;; } @@ -393,12 +393,12 @@ DetectUricontentData *DoDetectUricontentSetup (char * contentstr) cd->distance = 0; cd->flags = 0; - free(str); + SCFree(str); return cd; error: - free(str); - if (cd) free(cd); + SCFree(str); + if (cd) SCFree(cd); return NULL; } @@ -445,7 +445,7 @@ int DetectUricontentSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, SCReturnInt(0); error: - if (cd) free(cd); + if (cd) SCFree(cd); SCReturnInt(-1); } @@ -651,6 +651,7 @@ end: static int HTTPUriTest02(void) { int result = 1; Flow f; + HtpState *htp_state = NULL; uint8_t httpbuf1[] = "GET /%2e%2e/images.gif HTTP/1.1\r\nHost: www.ExA" "mPlE.cOM\r\n\r\n"; uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */ @@ -666,7 +667,7 @@ static int HTTPUriTest02(void) { r = AppLayerParse(&f, ALPROTO_HTTP, STREAM_TOSERVER|STREAM_START| STREAM_EOF, httpbuf1, httplen1); - HtpState *htp_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; + htp_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; if (htp_state == NULL) { printf("no http state: "); result = 0; @@ -707,6 +708,8 @@ static int HTTPUriTest02(void) { end: StreamL7DataPtrFree(&ssn); StreamTcpFreeConfig(TRUE); + if (htp_state == NULL) + HTPStateFree(htp_state); return result; } @@ -770,6 +773,8 @@ static int HTTPUriTest03(void) { end: StreamL7DataPtrFree(&ssn); StreamTcpFreeConfig(TRUE); + if (htp_state == NULL) + HTPStateFree(htp_state); return result; } @@ -835,6 +840,8 @@ static int HTTPUriTest04(void) { end: StreamL7DataPtrFree(&ssn); StreamTcpFreeConfig(TRUE); + if (htp_state == NULL) + HTPStateFree(htp_state); return result; } @@ -875,7 +882,9 @@ int DetectUriSigTest01(void) } end: + if (de_ctx != NULL) SigGroupCleanup(de_ctx); if (de_ctx != NULL) SigCleanSignatures(de_ctx); + if (det_ctx != NULL) DetectEngineThreadCtxDeinit(&th_v, det_ctx); if (de_ctx != NULL) DetectEngineCtxFree(de_ctx); return result; } @@ -892,6 +901,7 @@ static int DetectUriSigTest02(void) { Signature *s = NULL; ThreadVars th_v; DetectEngineThreadCtx *det_ctx; + HtpState *http_state = NULL; memset(&th_v, 0, sizeof(th_v)); memset(&p, 0, sizeof(p)); @@ -950,7 +960,7 @@ static int DetectUriSigTest02(void) { goto end; } - HtpState *http_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; + http_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; if (http_state == NULL) { printf("no http state: "); goto end; @@ -972,7 +982,10 @@ static int DetectUriSigTest02(void) { result = 1; end: + if (http_state != NULL) HTPStateFree(http_state); if (de_ctx != NULL) SigCleanSignatures(de_ctx); + if (de_ctx != NULL) SigGroupCleanup(de_ctx); + if (det_ctx != NULL) DetectEngineThreadCtxDeinit(&th_v, det_ctx); if (de_ctx != NULL) DetectEngineCtxFree(de_ctx); StreamL7DataPtrFree(&ssn); @@ -985,6 +998,7 @@ end: static int DetectUriSigTest03(void) { int result = 0; Flow f; + HtpState *http_state = NULL; uint8_t httpbuf1[] = "POST /one HTTP/1.0\r\nUser-Agent: Mozilla/1.0\r\nCookie:" " hellocatch\r\n\r\n"; uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */ @@ -1062,7 +1076,7 @@ static int DetectUriSigTest03(void) { goto end; } - HtpState *http_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; + http_state = ssn.aldata[AlpGetStateIdx(ALPROTO_HTTP)]; if (http_state == NULL) { printf("no http state: "); goto end; @@ -1084,7 +1098,10 @@ static int DetectUriSigTest03(void) { result = 1; end: + if (http_state != NULL) HTPStateFree(http_state); + if (de_ctx != NULL) SigGroupCleanup(de_ctx); if (de_ctx != NULL) SigCleanSignatures(de_ctx); + if (det_ctx != NULL) DetectEngineThreadCtxDeinit(&th_v, det_ctx); if (de_ctx != NULL) DetectEngineCtxFree(de_ctx); StreamL7DataPtrFree(&ssn); diff --git a/src/detect-urilen.c b/src/detect-urilen.c index 0636e65143..40ebe80ac1 100644 --- a/src/detect-urilen.c +++ b/src/detect-urilen.c @@ -64,8 +64,8 @@ void DetectUrilenRegister(void) return; error: - if (parse_regex != NULL) free(parse_regex); - if (parse_regex_study != NULL) free(parse_regex_study); + if (parse_regex != NULL) SCFree(parse_regex); + if (parse_regex_study != NULL) SCFree(parse_regex_study); return; } @@ -191,9 +191,9 @@ DetectUrilenData *DetectUrilenParse (char *urilenstr) arg4 = (char *) str_ptr; SCLogDebug("Arg4 \"%s\"", arg4); - urilend = malloc(sizeof (DetectUrilenData)); + urilend = SCMalloc(sizeof (DetectUrilenData)); if (urilend == NULL) { - SCLogDebug("DetectUrilenSetup malloc failed"); + SCLogDebug("DetectUrilenSetup SCMalloc failed"); goto error; } urilend->urilen1 = 0; @@ -238,18 +238,18 @@ DetectUrilenData *DetectUrilenParse (char *urilenstr) } } - free(arg1); - free(arg2); - free(arg3); - free(arg4); + SCFree(arg1); + SCFree(arg2); + SCFree(arg3); + SCFree(arg4); return urilend; error: - if (urilend) free(urilend); - if (arg1) free(arg1); - if (arg2) free(arg2); - if (arg3) free(arg3); - if (arg4) free(arg4); + if (urilend) SCFree(urilend); + if (arg1) SCFree(arg1); + if (arg2) SCFree(arg2); + if (arg3) SCFree(arg3); + if (arg4) SCFree(arg4); return NULL; } @@ -291,7 +291,7 @@ int DetectUrilenSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, error: if (urilend != NULL) DetectUrilenFree(urilend); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); SCReturnInt(-1); } @@ -303,7 +303,7 @@ error: void DetectUrilenFree(void *ptr) { DetectUrilenData *urilend = (DetectUrilenData *)ptr; - free(urilend); + SCFree(urilend); } #ifdef UNITTESTS @@ -452,7 +452,7 @@ static int DetectUrilenSetpTest01(void) { } cleanup: - if (urilend) free(urilend); + if (urilend) SCFree(urilend); SigGroupCleanup(de_ctx); SigCleanSignatures(de_ctx); DetectEngineCtxFree(de_ctx); diff --git a/src/detect-window.c b/src/detect-window.c index 9761bdb99b..779e2a10ae 100644 --- a/src/detect-window.c +++ b/src/detect-window.c @@ -118,7 +118,7 @@ DetectWindowData *DetectWindowParse(char *windowstr) { goto error; } - wd = malloc(sizeof(DetectWindowData)); + wd = SCMalloc(sizeof(DetectWindowData)); if (wd == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "malloc failed"); goto error; @@ -155,13 +155,13 @@ DetectWindowData *DetectWindowParse(char *windowstr) { int i = 0; for (i = 0; i < (ret -1); i++){ - if (args[i] != NULL) free(args[i]); + if (args[i] != NULL) SCFree(args[i]); } return wd; error: for (i = 0; i < (ret -1); i++){ - if (args[i] != NULL) free(args[i]); + if (args[i] != NULL) SCFree(args[i]); } if (wd != NULL) DetectWindowFree(wd); return NULL; @@ -202,7 +202,7 @@ int DetectWindowSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char error: if (wd != NULL) DetectWindowFree(wd); - if (sm != NULL) free(sm); + if (sm != NULL) SCFree(sm); return -1; } @@ -214,7 +214,7 @@ error: */ void DetectWindowFree(void *ptr) { DetectWindowData *wd = (DetectWindowData *)ptr; - free(wd); + SCFree(wd); } #ifdef UNITTESTS /* UNITTESTS */ diff --git a/src/detect-within.c b/src/detect-within.c index 05ffd9bc3d..620e02fc3a 100644 --- a/src/detect-within.c +++ b/src/detect-within.c @@ -34,7 +34,7 @@ int DetectWithinSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char /* strip "'s */ if (withinstr[0] == '\"' && withinstr[strlen(withinstr)-1] == '\"') { - str = strdup(withinstr+1); + str = SCStrdup(withinstr+1); str[strlen(withinstr)-2] = '\0'; dubbed = 1; } @@ -91,10 +91,10 @@ int DetectWithinSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char goto error; } - if (dubbed) free(str); + if (dubbed) SCFree(str); return 0; error: - if (dubbed) free(str); + if (dubbed) SCFree(str); return -1; } diff --git a/src/detect.c b/src/detect.c index 78b0826be7..c4bb56bc6a 100644 --- a/src/detect.c +++ b/src/detect.c @@ -211,16 +211,16 @@ char *DetectLoadCompleteSigPath(char *sig_file) SCLogDebug("Default path: %s", defaultpath); size_t path_len = sizeof(char) * (strlen(defaultpath) + strlen(sig_file) + 2); - path = malloc(path_len); + path = SCMalloc(path_len); strlcpy(path, defaultpath, path_len); if (path[strlen(path) - 1] != '/') strlcat(path, "/", path_len); strlcat(path, sig_file, path_len); } else { - path = strdup(sig_file); + path = SCStrdup(sig_file); } } else { - path = strdup(sig_file); + path = SCStrdup(sig_file); } return path; } @@ -341,7 +341,7 @@ int SigLoadSignatures (DetectEngineCtx *de_ctx, char *sig_file) exit(EXIT_FAILURE); } } - free(sfile); + SCFree(sfile); } } @@ -946,7 +946,7 @@ int SigAddressPrepareStage1(DetectEngineCtx *de_ctx) { de_ctx->sig_array_len = DetectEngineGetMaxSigId(de_ctx); de_ctx->sig_array_size = (de_ctx->sig_array_len * sizeof(Signature *)); - de_ctx->sig_array = (Signature **)malloc(de_ctx->sig_array_size); + de_ctx->sig_array = (Signature **)SCMalloc(de_ctx->sig_array_size); if (de_ctx->sig_array == NULL) goto error; memset(de_ctx->sig_array,0,de_ctx->sig_array_size); @@ -3032,6 +3032,8 @@ static int SigTest01Real (int mpm_type) { } end: + if (p != NULL) + UTHFreePacket(p); return result; } @@ -3056,7 +3058,9 @@ static int SigTest02Real (int mpm_type) { uint16_t buflen = strlen((char *)buf); Packet *p = UTHBuildPacket( buf, buflen, IPPROTO_TCP); char sig[] = "alert tcp any any -> any any (msg:\"HTTP TEST\"; content:\"Host: one.example.org\"; offset:20; depth:41; sid:1;)"; - return UTHPacketMatchSigMpm(p, sig, mpm_type); + int ret = UTHPacketMatchSigMpm(p, sig, mpm_type); + UTHFreePacket(p); + return ret; } static int SigTest02B2g (void) { @@ -3424,15 +3428,16 @@ static int SigTest07Real (int mpm_type) { else result = 1; +end: + StreamL7DataPtrFree(&ssn); + StreamTcpFreeConfig(TRUE); + AppLayerParserCleanupState(&ssn); SigGroupCleanup(de_ctx); SigCleanSignatures(de_ctx); DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx); //PatternMatchDestroy(mpm_ctx); DetectEngineCtxFree(de_ctx); -end: - StreamL7DataPtrFree(&ssn); - StreamTcpFreeConfig(TRUE); return result; } static int SigTest07B2g (void) { @@ -3519,6 +3524,7 @@ static int SigTest08Real (int mpm_type) { PacketAlertCheck(&p, 1) ? "OK" : "FAIL", PacketAlertCheck(&p, 2) ? "OK" : "FAIL"); + AppLayerParserCleanupState(&ssn); SigGroupCleanup(de_ctx); SigCleanSignatures(de_ctx); @@ -3610,6 +3616,7 @@ static int SigTest09Real (int mpm_type) { else result = 0; + AppLayerParserCleanupState(&ssn); SigGroupCleanup(de_ctx); SigCleanSignatures(de_ctx); DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx); @@ -3695,6 +3702,7 @@ static int SigTest10Real (int mpm_type) { else result = 1; + AppLayerParserCleanupState(&ssn); SigGroupCleanup(de_ctx); SigCleanSignatures(de_ctx); DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx); @@ -3774,6 +3782,7 @@ static int SigTest11Real (int mpm_type) { else result = 0; + AppLayerParserCleanupState(&ssn); SigGroupCleanup(de_ctx); DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx); DetectEngineCtxFree(de_ctx); @@ -3837,12 +3846,15 @@ static int SigTest12Real (int mpm_type) { else result = 0; - SigGroupCleanup(de_ctx); - SigCleanSignatures(de_ctx); - DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx); - //PatternMatchDestroy(mpm_ctx); - DetectEngineCtxFree(de_ctx); end: + if (de_ctx != NULL) { + SigGroupCleanup(de_ctx); + SigCleanSignatures(de_ctx); + if (det_ctx != NULL) + DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx); + //PatternMatchDestroy(mpm_ctx); + DetectEngineCtxFree(de_ctx); + } return result; } static int SigTest12B2g (void) { @@ -4163,16 +4175,19 @@ static int SigTest17Real (int mpm_type) { PrintRawUriFp(stdout, pv_hn->value, pv_hn->value_len); printf("\" != \"one.example.org\": "); } + PktVarFree(pv_hn); } else { printf("Pkt var http_host not captured: "); } - SigGroupCleanup(de_ctx); - SigCleanSignatures(de_ctx); - - DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx); - DetectEngineCtxFree(de_ctx); end: + if (de_ctx != NULL) { + SigGroupCleanup(de_ctx); + SigCleanSignatures(de_ctx); + if (det_ctx != NULL) + DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx); + DetectEngineCtxFree(de_ctx); + } ConfDeInit(); ConfRestoreContextBackup(); return result; diff --git a/src/detect.h b/src/detect.h index cd9279f1a0..8de84a9f46 100644 --- a/src/detect.h +++ b/src/detect.h @@ -11,6 +11,8 @@ #include "util-mpm.h" #include "util-hash.h" #include "util-hashlist.h" +#include "util-debug.h" +#include "util-error.h" #include "detect-threshold.h" diff --git a/src/flow-alert-sid.c b/src/flow-alert-sid.c index a99df7d08d..719d40ef0f 100644 --- a/src/flow-alert-sid.c +++ b/src/flow-alert-sid.c @@ -43,7 +43,7 @@ static FlowAlertSid *FlowAlertSidGet(Flow *f, uint32_t sid) { static void FlowAlertSidAdd(Flow *f, uint32_t sid) { FlowAlertSid *fb = FlowAlertSidGet(f, sid); if (fb == NULL) { - fb = malloc(sizeof(FlowAlertSid)); + fb = SCMalloc(sizeof(FlowAlertSid)); if (fb == NULL) return; @@ -153,7 +153,7 @@ void FlowAlertSidFree(FlowAlertSid *fb) { if (fb == NULL) return; - free(fb); + SCFree(fb); #ifdef FLOWALERTSID_STATS SCMutexLock(&flowbits_mutex); diff --git a/src/flow-bit.c b/src/flow-bit.c index f60fcf967c..38b1f9ec73 100644 --- a/src/flow-bit.c +++ b/src/flow-bit.c @@ -19,6 +19,7 @@ #include "flow-private.h" #include "detect.h" #include "util-var.h" +#include "util-debug.h" #include "util-unittest.h" /* get the flowbit with idx from the flow */ @@ -37,7 +38,7 @@ static FlowBit *FlowBitGet(Flow *f, uint16_t idx) { static void FlowBitAdd(Flow *f, uint16_t idx) { FlowBit *fb = FlowBitGet(f, idx); if (fb == NULL) { - fb = malloc(sizeof(FlowBit)); + fb = SCMalloc(sizeof(FlowBit)); if (fb == NULL) return; @@ -144,7 +145,7 @@ void FlowBitFree(FlowBit *fb) { if (fb == NULL) return; - free(fb); + SCFree(fb); #ifdef FLOWBITS_STATS SCMutexLock(&flowbits_mutex); diff --git a/src/flow-queue.c b/src/flow-queue.c index b5368902a7..d642998dad 100644 --- a/src/flow-queue.c +++ b/src/flow-queue.c @@ -11,7 +11,7 @@ #include FlowQueue *FlowQueueNew() { - FlowQueue *q = (FlowQueue *)malloc(sizeof(FlowQueue)); + FlowQueue *q = (FlowQueue *)SCMalloc(sizeof(FlowQueue)); if (q == NULL) { SCLogError(SC_ERR_MEM_ALLOC,"Error allocating flow queue"); exit(EXIT_SUCCESS); diff --git a/src/flow-util.c b/src/flow-util.c index 4c9f9ff24a..82a9faa420 100644 --- a/src/flow-util.c +++ b/src/flow-util.c @@ -21,7 +21,7 @@ Flow *FlowAlloc(void) SCMutexUnlock(&flow_memuse_mutex); return NULL; } - f = malloc(sizeof(Flow)); + f = SCMalloc(sizeof(Flow)); if (f == NULL) { SCMutexUnlock(&flow_memuse_mutex); return NULL; @@ -47,7 +47,7 @@ void FlowFree(Flow *f) flow_memuse -= sizeof(Flow); SCMutexUnlock(&flow_memuse_mutex); - free(f); + SCFree(f); } /** diff --git a/src/flow-var.c b/src/flow-var.c index f7e37e43dc..6594faa6c6 100644 --- a/src/flow-var.c +++ b/src/flow-var.c @@ -17,7 +17,7 @@ /* puts a new value into a flowvar */ void FlowVarUpdateStr(FlowVar *fv, uint8_t *value, uint16_t size) { - if (fv->data.fv_str.value) free(fv->data.fv_str.value); + if (fv->data.fv_str.value) SCFree(fv->data.fv_str.value); fv->data.fv_str.value = value; fv->data.fv_str.value_len = size; } @@ -49,7 +49,7 @@ void FlowVarAddStr(Flow *f, uint8_t idx, uint8_t *value, uint16_t size) { FlowVar *fv = FlowVarGet(f, idx); if (fv == NULL) { - fv = malloc(sizeof(FlowVar)); + fv = SCMalloc(sizeof(FlowVar)); if (fv == NULL) goto out; @@ -77,7 +77,7 @@ void FlowVarAddInt(Flow *f, uint8_t idx, uint32_t value) { FlowVar *fv = FlowVarGet(f, idx); if (fv == NULL) { - fv = malloc(sizeof(FlowVar)); + fv = SCMalloc(sizeof(FlowVar)); if (fv == NULL) goto out; @@ -102,9 +102,9 @@ void FlowVarFree(FlowVar *fv) { if (fv->datatype == FLOWVAR_TYPE_STR) { if (fv->data.fv_str.value != NULL) - free(fv->data.fv_str.value); + SCFree(fv->data.fv_str.value); } - free(fv); + SCFree(fv); } void FlowVarPrint(GenericVar *gv) { diff --git a/src/flow.c b/src/flow.c index 206855b432..af75315557 100644 --- a/src/flow.c +++ b/src/flow.c @@ -483,9 +483,9 @@ void FlowInitConfig (char quiet) flow_config.hash_size, flow_config.prealloc); /* alloc hash memory */ - flow_hash = calloc(flow_config.hash_size, sizeof(FlowBucket)); + flow_hash = SCCalloc(flow_config.hash_size, sizeof(FlowBucket)); if (flow_hash == NULL) { - printf("calloc failed %s\n", strerror(errno)); + printf("SCCalloc failed %s\n", strerror(errno)); exit(1); } uint32_t i = 0; @@ -598,7 +598,7 @@ void FlowShutdown(void) { } if (flow_hash != NULL) { - free(flow_hash); + SCFree(flow_hash); flow_hash = NULL; } flow_memuse -= flow_config.hash_size * sizeof(FlowBucket); diff --git a/src/host.c b/src/host.c index 644345f22d..fa9e2d4149 100644 --- a/src/host.c +++ b/src/host.c @@ -1,8 +1,9 @@ #include "suricata-common.h" +#include "util-debug.h" #include "host.h" Host *HostAlloc(void) { - Host *h = malloc(sizeof(Host)); + Host *h = SCMalloc(sizeof(Host)); if (h == NULL) goto error; @@ -13,7 +14,7 @@ error: } void HostFree(Host *h) { - free(h); + SCFree(h); } Host *HostNew(Address *a) { diff --git a/src/log-httplog.c b/src/log-httplog.c index b8f3e2d609..37369ba46a 100644 --- a/src/log-httplog.c +++ b/src/log-httplog.c @@ -326,7 +326,7 @@ TmEcode LogHttpLog (ThreadVars *tv, Packet *p, void *data, PacketQueue *pq) TmEcode LogHttpLogThreadInit(ThreadVars *t, void *initdata, void **data) { - LogHttpLogThread *aft = malloc(sizeof(LogHttpLogThread)); + LogHttpLogThread *aft = SCMalloc(sizeof(LogHttpLogThread)); if (aft == NULL) { return TM_ECODE_FAILED; } @@ -335,7 +335,7 @@ TmEcode LogHttpLogThreadInit(ThreadVars *t, void *initdata, void **data) if(initdata == NULL) { SCLogDebug("Error getting context for HTTPLog. \"initdata\" argument NULL"); - free(aft); + SCFree(aft); return TM_ECODE_FAILED; } /** Use the Ouptut Context (file pointer and mutex) */ @@ -355,7 +355,7 @@ TmEcode LogHttpLogThreadDeinit(ThreadVars *t, void *data) /* clear memory */ memset(aft, 0, sizeof(LogHttpLogThread)); - free(aft); + SCFree(aft); return TM_ECODE_OK; } diff --git a/src/output.c b/src/output.c index 2643d711de..049b2e3ef9 100644 --- a/src/output.c +++ b/src/output.c @@ -27,15 +27,15 @@ static TAILQ_HEAD(, OutputModule_) output_modules = void OutputRegisterModule(char *name, char *conf_name, LogFileCtx *(*InitFunc)(ConfNode *)) { - OutputModule *module = calloc(1, sizeof(*module)); + OutputModule *module = SCCalloc(1, sizeof(*module)); if (module == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Failed to allocated memory for new output module"); exit(EXIT_FAILURE); } - module->name = strdup(name); - module->conf_name = strdup(conf_name); + module->name = SCStrdup(name); + module->conf_name = SCStrdup(conf_name); module->InitFunc = InitFunc; TAILQ_INSERT_TAIL(&output_modules, module, entries); @@ -71,8 +71,8 @@ OutputDeregisterAll(void) while ((module = TAILQ_FIRST(&output_modules))) { TAILQ_REMOVE(&output_modules, module, entries); - free(module->name); - free(module->conf_name); - free(module); + SCFree(module->name); + SCFree(module->conf_name); + SCFree(module); } } diff --git a/src/pkt-var.c b/src/pkt-var.c index 0a89cd09c1..0c0d34db71 100644 --- a/src/pkt-var.c +++ b/src/pkt-var.c @@ -11,10 +11,11 @@ #include "suricata-common.h" #include "decode.h" #include "pkt-var.h" +#include "util-debug.h" /* puts a new value into a pktvar */ void PktVarUpdate(PktVar *pv, uint8_t *value, uint16_t size) { - if (pv->value) free(pv->value); + if (pv->value) SCFree(pv->value); pv->value = value; pv->value_len = size; } @@ -39,7 +40,7 @@ void PktVarAdd(Packet *p, char *name, uint8_t *value, uint16_t size) { PktVar *pv = PktVarGet(p, name); if (pv == NULL) { - pv = malloc(sizeof(PktVar)); + pv = SCMalloc(sizeof(PktVar)); if (pv == NULL) return; @@ -71,10 +72,10 @@ void PktVarFree(PktVar *pv) { pv->name = NULL; if (pv->value != NULL) - free(pv->value); + SCFree(pv->value); PktVar *pv_next = pv->next; - free(pv); + SCFree(pv); if (pv_next != NULL) PktVarFree(pv_next); diff --git a/src/reputation.c b/src/reputation.c index 78ca7a8691..4d717d2eb7 100644 --- a/src/reputation.c +++ b/src/reputation.c @@ -27,7 +27,7 @@ IPReputationCtx *rep_ctx; * NULL Error initializing moule; */ IPReputationCtx *SCReputationInitCtx() { - rep_ctx = (IPReputationCtx *)malloc(sizeof(IPReputationCtx)); + rep_ctx = (IPReputationCtx *)SCMalloc(sizeof(IPReputationCtx)); if (rep_ctx == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory for Reputation context"); exit(EXIT_FAILURE); @@ -72,7 +72,7 @@ Reputation *SCReputationAllocData() { Reputation *rep_data = NULL; - if ( (rep_data = malloc(sizeof(Reputation))) == NULL) { + if ( (rep_data = SCMalloc(sizeof(Reputation))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } @@ -84,14 +84,14 @@ Reputation *SCReputationAllocData() } /** - * \brief Used to free the reputation data that is allocated by Reputation API + * \brief Used to SCFree the reputation data that is allocated by Reputation API * - * \param Pointer to the data that has to be freed + * \param Pointer to the data that has to be SCFreed */ void SCReputationFreeData(void *data) { if (data != NULL) - free(data); + SCFree(data); return; } @@ -105,7 +105,7 @@ ReputationTransaction *SCReputationTransactionAlloc() { ReputationTransaction *rtx = NULL; - if ( (rtx = malloc(sizeof(ReputationTransaction))) == NULL) { + if ( (rtx = SCMalloc(sizeof(ReputationTransaction))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } @@ -115,14 +115,14 @@ ReputationTransaction *SCReputationTransactionAlloc() } /** - * \brief Used to free the transaction data + * \brief Used to SCFree the transaction data * - * \param Pointer to the data that has to be freed + * \param Pointer to the data that has to be SCFreed */ void SCReputationTransactionFreeData(void *data) { if (data != NULL) - free(data); + SCFree(data); return; } @@ -213,7 +213,7 @@ Reputation *SCReputationClone(Reputation *orig) return NULL; } - if ( (rep = malloc(sizeof(Reputation))) == NULL) { + if ( (rep = SCMalloc(sizeof(Reputation))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } diff --git a/src/runmodes.c b/src/runmodes.c index 8b5739faf2..4abf07efd9 100644 --- a/src/runmodes.c +++ b/src/runmodes.c @@ -51,7 +51,7 @@ void RunModeShutDown(void) TAILQ_REMOVE(&RunModeOutputs, output, entries); if (output->logfile_ctx != NULL) LogFileFreeCtx(output->logfile_ctx); - free(output); + SCFree(output); } } @@ -101,7 +101,7 @@ void RunModeInitializeOutputs(void) "TmModuleGetByName for %s failed", module->name); exit(EXIT_FAILURE); } - RunModeOutput *runmode_output = calloc(1, sizeof(RunModeOutput)); + RunModeOutput *runmode_output = SCCalloc(1, sizeof(RunModeOutput)); if (runmode_output == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Failed to allocate memory for output."); @@ -1951,7 +1951,7 @@ int RunModeIdsPcapAuto(DetectEngineCtx *de_ctx, char *iface) { if (tname == NULL) break; - char *thread_name = strdup(tname); + char *thread_name = SCStrdup(tname); SCLogDebug("Assigning %s affinity to cpu %u", thread_name, cpu); ThreadVars *tv_detect_ncpu = TmThreadCreatePacketHandler(thread_name,"stream-queue1","simple","verdict-queue","simple","1slot"); @@ -2118,7 +2118,7 @@ int RunModeFilePcapAuto(DetectEngineCtx *de_ctx, char *file) { if (tname == NULL) break; - char *thread_name = strdup(tname); + char *thread_name = SCStrdup(tname); SCLogDebug("Assigning %s affinity to cpu %u", thread_name, cpu); ThreadVars *tv_detect_ncpu = TmThreadCreatePacketHandler(thread_name,"stream-queue1","simple","alert-queue1","simple","1slot"); @@ -2264,7 +2264,7 @@ int RunModeIpsIPFWAuto(DetectEngineCtx *de_ctx) { if (tname == NULL) break; - char *thread_name = strdup(tname); + char *thread_name = SCStrdup(tname); SCLogDebug("Assigning %s affinity to cpu %u", thread_name, cpu); ThreadVars *tv_detect_ncpu = TmThreadCreatePacketHandler(thread_name,"stream-queue1","simple","verdict-queue","simple","1slot"); @@ -2451,7 +2451,7 @@ int RunModeIpsNFQAuto(DetectEngineCtx *de_ctx, char *nfq_id) { if (tname == NULL) break; - char *thread_name = strdup(tname); + char *thread_name = SCStrdup(tname); SCLogDebug("Assigning %s affinity to cpu %u", thread_name, cpu); ThreadVars *tv_detect_ncpu = TmThreadCreatePacketHandler(thread_name,"stream-queue1","simple","verdict-queue","simple","1slot"); @@ -2637,7 +2637,7 @@ int RunModeIdsPfringAuto(DetectEngineCtx *de_ctx, char *iface) { if (tname == NULL) break; - char *thread_name = strdup(tname); + char *thread_name = SCStrdup(tname); SCLogDebug("Assigning %s affinity to cpu %u", thread_name, cpu); ThreadVars *tv_detect_ncpu = TmThreadCreatePacketHandler(thread_name,"stream-queue1","simple","verdict-queue","simple","1slot"); diff --git a/src/source-ipfw.c b/src/source-ipfw.c index c7abeb554f..e3c42bc970 100644 --- a/src/source-ipfw.c +++ b/src/source-ipfw.c @@ -290,7 +290,7 @@ TmEcode ReceiveIPFWThreadInit(ThreadVars *tv, void *initdata, void **data) { } /* Setup Threadvars */ - IPFWThreadVars *ptv = malloc(sizeof(IPFWThreadVars)); + IPFWThreadVars *ptv = SCMalloc(sizeof(IPFWThreadVars)); if (ptv == NULL) { SCLogError(SC_ERR_MEM_ALLOC,"Error Allocating memory for IPFW Receive PTV: %s",strerror(errno)); SCReturnInt(TM_ECODE_FAILED); @@ -427,7 +427,7 @@ TmEcode DecodeIPFWThreadInit(ThreadVars *tv, void *initdata, void **data) { DecodeThreadVars *dtv = NULL; - if ( (dtv = malloc(sizeof(DecodeThreadVars))) == NULL) { + if ( (dtv = SCMalloc(sizeof(DecodeThreadVars))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC,"Error Allocating memory for IPFW Decode DTV: %s",strerror(errno)); SCReturnInt(TM_ECODE_FAILED); } @@ -576,7 +576,7 @@ TmEcode VerdictIPFWThreadInit(ThreadVars *tv, void *initdata, void **data) { SCEnter(); /* Setup Thread vars */ - if ( (ptv = malloc(sizeof(IPFWThreadVars))) == NULL) { + if ( (ptv = SCMalloc(sizeof(IPFWThreadVars))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC,"Error Allocating memory for IPFW Verdict PTV: %s", strerror(errno)); SCReturnInt(TM_ECODE_FAILED); } diff --git a/src/source-nfq.c b/src/source-nfq.c index f6fb97b140..6fb9080004 100644 --- a/src/source-nfq.c +++ b/src/source-nfq.c @@ -516,8 +516,8 @@ TmEcode DecodeNFQThreadInit(ThreadVars *tv, void *initdata, void **data) { DecodeThreadVars *dtv = NULL; - if ( (dtv = malloc(sizeof(DecodeThreadVars))) == NULL) { - SCLogError(SC_ERR_MEM_ALLOC, "malloc failed"); + if ( (dtv = SCMalloc(sizeof(DecodeThreadVars))) == NULL) { + SCLogError(SC_ERR_MEM_ALLOC, "SCMalloc failed"); return TM_ECODE_FAILED; } memset(dtv, 0, sizeof(DecodeThreadVars)); diff --git a/src/source-pcap-file.c b/src/source-pcap-file.c index e3d8cea166..9a1b454c8d 100644 --- a/src/source-pcap-file.c +++ b/src/source-pcap-file.c @@ -138,7 +138,7 @@ TmEcode ReceivePcapFileThreadInit(ThreadVars *tv, void *initdata, void **data) { SCLogInfo("reading pcap file %s", (char *)initdata); - PcapFileThreadVars *ptv = malloc(sizeof(PcapFileThreadVars)); + PcapFileThreadVars *ptv = SCMalloc(sizeof(PcapFileThreadVars)); if (ptv == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory for PcapFileThreadVars"); SCReturnInt(TM_ECODE_FAILED); @@ -149,7 +149,7 @@ TmEcode ReceivePcapFileThreadInit(ThreadVars *tv, void *initdata, void **data) { pcap_g.pcap_handle = pcap_open_offline((char *)initdata, errbuf); if (pcap_g.pcap_handle == NULL) { SCLogError(SC_ERR_FOPEN, "%s\n", errbuf); - free(ptv); + SCFree(ptv); exit(EXIT_FAILURE); } @@ -160,13 +160,13 @@ TmEcode ReceivePcapFileThreadInit(ThreadVars *tv, void *initdata, void **data) { if(pcap_compile(pcap_g.pcap_handle,&pcap_g.filter,tmpbpfstring,1,0) < 0) { SCLogError(SC_ERR_BPF,"bpf compilation error %s",pcap_geterr(pcap_g.pcap_handle)); - free(ptv); + SCFree(ptv); return TM_ECODE_FAILED; } if(pcap_setfilter(pcap_g.pcap_handle,&pcap_g.filter) < 0) { SCLogError(SC_ERR_BPF,"could not set bpf filter %s",pcap_geterr(pcap_g.pcap_handle)); - free(ptv); + SCFree(ptv); return TM_ECODE_FAILED; } } @@ -189,8 +189,9 @@ TmEcode ReceivePcapFileThreadInit(ThreadVars *tv, void *initdata, void **data) { break; default: - SCLogError(SC_ERR_DATALINK_UNIMPLEMENTED, "Error: datalink type %" PRId32 " not yet supported in module PcapFile", pcap_g.datalink); - free(ptv); + SCLogError(SC_ERR_UNIMPLEMENTED, "datalink type %" PRId32 " not " + "(yet) supported in module PcapFile.\n", pcap_g.datalink); + SCFree(ptv); SCReturnInt(TM_ECODE_FAILED); } @@ -240,7 +241,7 @@ TmEcode DecodePcapFileThreadInit(ThreadVars *tv, void *initdata, void **data) SCEnter(); DecodeThreadVars *dtv = NULL; - if ( (dtv = malloc(sizeof(DecodeThreadVars))) == NULL) { + if ( (dtv = SCMalloc(sizeof(DecodeThreadVars))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error Allocating memory for DecodeThreadVars"); SCReturnInt(TM_ECODE_FAILED); } diff --git a/src/source-pcap.c b/src/source-pcap.c index 4284682a56..467c3bd4bf 100644 --- a/src/source-pcap.c +++ b/src/source-pcap.c @@ -174,7 +174,7 @@ TmEcode ReceivePcapThreadInit(ThreadVars *tv, void *initdata, void **data) { SCReturnInt(TM_ECODE_FAILED); } - PcapThreadVars *ptv = malloc(sizeof(PcapThreadVars)); + PcapThreadVars *ptv = SCMalloc(sizeof(PcapThreadVars)); if (ptv == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Couldn't allocate PcapThreadVars"); SCReturnInt(TM_ECODE_FAILED); @@ -190,7 +190,7 @@ TmEcode ReceivePcapThreadInit(ThreadVars *tv, void *initdata, void **data) { ptv->pcap_handle = pcap_create((char *)initdata, errbuf); if (ptv->pcap_handle == NULL) { SCLogError(SC_ERR_PCAP_CREATE, "Coudn't create a new pcap handler, error %s", pcap_geterr(ptv->pcap_handle)); - free(ptv); + SCFree(ptv); SCReturnInt(TM_ECODE_FAILED); } @@ -199,7 +199,7 @@ TmEcode ReceivePcapThreadInit(ThreadVars *tv, void *initdata, void **data) { //printf("ReceivePcapThreadInit: pcap_set_snaplen(%p) returned %" PRId32 "\n", ptv->pcap_handle, pcap_set_snaplen_r); if (pcap_set_snaplen_r != 0) { SCLogError(SC_ERR_PCAP_SET_SNAPLEN, "Couldn't set snaplen, error: %s", pcap_geterr(ptv->pcap_handle)); - free(ptv); + SCFree(ptv); SCReturnInt(TM_ECODE_FAILED); } @@ -207,7 +207,7 @@ TmEcode ReceivePcapThreadInit(ThreadVars *tv, void *initdata, void **data) { //printf("ReceivePcapThreadInit: pcap_set_promisc(%p) returned %" PRId32 "\n", ptv->pcap_handle, pcap_set_promisc_r); if (pcap_set_promisc_r != 0) { SCLogError(SC_ERR_PCAP_SET_PROMISC, "Couldn't set promisc mode, error %s", pcap_geterr(ptv->pcap_handle)); - free(ptv); + SCFree(ptv); SCReturnInt(TM_ECODE_FAILED); } @@ -215,7 +215,7 @@ TmEcode ReceivePcapThreadInit(ThreadVars *tv, void *initdata, void **data) { //printf("ReceivePcapThreadInit: pcap_set_timeout(%p) returned %" PRId32 "\n", ptv->pcap_handle, pcap_set_timeout_r); if (pcap_set_timeout_r != 0) { SCLogError(SC_ERR_PCAP_SET_TIMEOUT, "Problems setting timeout, error %s", pcap_geterr(ptv->pcap_handle)); - free(ptv); + SCFree(ptv); SCReturnInt(TM_ECODE_FAILED); } @@ -224,7 +224,7 @@ TmEcode ReceivePcapThreadInit(ThreadVars *tv, void *initdata, void **data) { //printf("ReceivePcapThreadInit: pcap_activate(%p) returned %" PRId32 "\n", ptv->pcap_handle, pcap_activate_r); if (pcap_activate_r != 0) { SCLogError(SC_ERR_PCAP_ACTIVATE_HANDLE, "Couldn't activate the pcap handler, error %s", pcap_geterr(ptv->pcap_handle)); - free(ptv); + SCFree(ptv); SCReturnInt(TM_ECODE_FAILED); } @@ -236,13 +236,13 @@ TmEcode ReceivePcapThreadInit(ThreadVars *tv, void *initdata, void **data) { if(pcap_compile(ptv->pcap_handle,&ptv->filter,tmpbpfstring,1,0) < 0) { SCLogError(SC_ERR_BPF,"bpf compilation error %s",pcap_geterr(ptv->pcap_handle)); - free(ptv); + SCFree(ptv); return TM_ECODE_FAILED; } if(pcap_setfilter(ptv->pcap_handle,&ptv->filter) < 0) { SCLogError(SC_ERR_BPF,"could not set bpf filter %s",pcap_geterr(ptv->pcap_handle)); - free(ptv); + SCFree(ptv); return TM_ECODE_FAILED; } } @@ -263,7 +263,7 @@ TmEcode ReceivePcapThreadInit(ThreadVars *tv, void *initdata, void **data) { SCReturnInt(TM_ECODE_FAILED); } - PcapThreadVars *ptv = malloc(sizeof(PcapThreadVars)); + PcapThreadVars *ptv = SCMalloc(sizeof(PcapThreadVars)); if (ptv == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Couldn't allocate PcapThreadVars"); SCReturnInt(TM_ECODE_FAILED); @@ -404,7 +404,7 @@ TmEcode DecodePcapThreadInit(ThreadVars *tv, void *initdata, void **data) SCEnter(); DecodeThreadVars *dtv = NULL; - if ( (dtv = malloc(sizeof(DecodeThreadVars))) == NULL) { + if ( (dtv = SCMalloc(sizeof(DecodeThreadVars))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error Allocating memory"); SCReturnInt(TM_ECODE_FAILED); } diff --git a/src/source-pfring.c b/src/source-pfring.c index 712f40ed0a..a440f8666b 100644 --- a/src/source-pfring.c +++ b/src/source-pfring.c @@ -206,7 +206,7 @@ TmEcode ReceivePfringThreadInit(ThreadVars *tv, void *initdata, void **data) { u_int32_t version; char *tmpclusterid; - PfringThreadVars *ptv = malloc(sizeof(PfringThreadVars)); + PfringThreadVars *ptv = SCMalloc(sizeof(PfringThreadVars)); if (ptv == NULL) { return TM_ECODE_FAILED; } @@ -337,7 +337,7 @@ TmEcode DecodePfringThreadInit(ThreadVars *tv, void *initdata, void **data) { DecodeThreadVars *dtv = NULL; - if ( (dtv = malloc(sizeof(DecodeThreadVars))) == NULL) { + if ( (dtv = SCMalloc(sizeof(DecodeThreadVars))) == NULL) { printf("Error Allocating memory\n"); return TM_ECODE_FAILED; } diff --git a/src/stream-tcp-reassemble.c b/src/stream-tcp-reassemble.c index 37a4fb9851..7b48fe397b 100644 --- a/src/stream-tcp-reassemble.c +++ b/src/stream-tcp-reassemble.c @@ -60,7 +60,7 @@ void *TcpSegmentPoolAlloc(void *payload_len) { if (StreamTcpCheckMemcap((uint32_t)sizeof(TcpSegment) + *((uint16_t *) payload_len)) == 0) return NULL; - TcpSegment *seg = malloc(sizeof (TcpSegment)); + TcpSegment *seg = SCMalloc(sizeof (TcpSegment)); if (seg == NULL) return NULL; @@ -69,9 +69,9 @@ void *TcpSegmentPoolAlloc(void *payload_len) { seg->pool_size = *((uint16_t *) payload_len); seg->payload_len = seg->pool_size; - seg->payload = malloc(seg->payload_len); + seg->payload = SCMalloc(seg->payload_len); if (seg->payload == NULL) { - free(seg); + SCFree(seg); return NULL; } @@ -104,13 +104,13 @@ void TcpSegmentPoolFree(void *ptr) { SCMutexUnlock(&segment_pool_memuse_mutex); #endif - free(seg->payload); - free(seg); + SCFree(seg->payload); + SCFree(seg); return; } /* We define serveral pools with prealloced segments with fixed size - * payloads. We do this to prevent having to do an malloc call for every + * payloads. We do this to prevent having to do an SCMalloc call for every * data segment we receive, which would be a large performance penalty. * The cost is in memory of course. */ #define segment_pool_num 8 @@ -202,7 +202,7 @@ void StreamTcpReassembleFree(char quiet) TcpReassemblyThreadCtx *StreamTcpReassembleInitThreadCtx(void) { SCEnter(); - TcpReassemblyThreadCtx *ra_ctx = malloc(sizeof(TcpReassemblyThreadCtx)); + TcpReassemblyThreadCtx *ra_ctx = SCMalloc(sizeof(TcpReassemblyThreadCtx)); if (ra_ctx == NULL) { return NULL; } @@ -222,7 +222,7 @@ void StreamTcpReassembleFreeThreadCtx(TcpReassemblyThreadCtx *ra_ctx) ra_ctx->stream_q = NULL; AlpProtoDeFinalize2Thread(&ra_ctx->dp_ctx); - free(ra_ctx); + SCFree(ra_ctx); SCReturn; } @@ -1617,7 +1617,7 @@ void StreamL7DataPtrInit(TcpSession *ssn) { if (StreamTcpCheckMemcap(size) == 0) return; - ssn->aldata = (void **) malloc(size); + ssn->aldata = (void **) SCMalloc(size); if (ssn->aldata != NULL) { StreamTcpIncrMemuse(size); @@ -1635,7 +1635,7 @@ void StreamL7DataPtrFree(TcpSession *ssn) { if (ssn->aldata == NULL) return; - free(ssn->aldata); + SCFree(ssn->aldata); ssn->aldata = NULL; uint32_t size = (uint32_t)(sizeof (void *) * StreamL7GetStorageSize()); @@ -1951,6 +1951,8 @@ static int StreamTcpReassembleStreamTest(TcpStream *stream) { if (StreamTcpReassembleHandleSegment(ra_ctx,&ssn, stream, &p) == -1) return 0; + StreamTcpReassembleFreeThreadCtx(ra_ctx); + return 1; } @@ -2799,10 +2801,12 @@ static int StreamTcpReassembleTest19(void) { StreamTcpInitConfig(TRUE); if (StreamTcpTestStartsAfterListSegment(&stream) == 0) { printf("failed in segments reassembly!!\n"); + StreamTcpFreeConfig(TRUE); return 0; } if (StreamTcpCheckStreamContents(stream_after_solaris, sizeof(stream_after_solaris), &stream) == 0) { printf("failed in stream matching!!\n"); + StreamTcpFreeConfig(TRUE); return 0; } StreamTcpFreeConfig(TRUE); @@ -2824,10 +2828,12 @@ static int StreamTcpReassembleTest20(void) { StreamTcpInitConfig(TRUE); if (StreamTcpReassembleStreamTest(&stream) == 0) { printf("failed in segments reassembly!!\n"); + StreamTcpFreeConfig(TRUE); return 0; } if (StreamTcpCheckStreamContents(stream_solaris, sizeof(stream_solaris), &stream) == 0) { printf("failed in stream matching!!\n"); + StreamTcpFreeConfig(TRUE); return 0; } StreamTcpFreeConfig(TRUE); diff --git a/src/stream-tcp.c b/src/stream-tcp.c index d5be434683..8ba3e5d418 100644 --- a/src/stream-tcp.c +++ b/src/stream-tcp.c @@ -210,7 +210,7 @@ void *StreamTcpSessionPoolAlloc(void *null) if (StreamTcpCheckMemcap((uint32_t)sizeof(TcpSession)) == 0) return NULL; - void *ptr = malloc(sizeof(TcpSession)); + void *ptr = SCMalloc(sizeof(TcpSession)); if (ptr == NULL) return NULL; @@ -234,7 +234,7 @@ void StreamTcpSessionPoolFree(void *s) StreamTcpReturnStreamSegments(&ssn->server); StreamL7DataPtrFree(ssn); - free(ssn); + SCFree(ssn); StreamTcpDecrMemuse((uint32_t)sizeof(TcpSession)); @@ -2503,7 +2503,7 @@ TmEcode StreamTcp (ThreadVars *tv, Packet *p, void *data, PacketQueue *pq) TmEcode StreamTcpThreadInit(ThreadVars *tv, void *initdata, void **data) { SCEnter(); - StreamTcpThread *stt = malloc(sizeof(StreamTcpThread)); + StreamTcpThread *stt = SCMalloc(sizeof(StreamTcpThread)); if (stt == NULL) { SCReturnInt(TM_ECODE_FAILED); } @@ -2546,7 +2546,7 @@ TmEcode StreamTcpThreadDeinit(ThreadVars *tv, void *data) /* clear memory */ memset(stt, 0, sizeof(StreamTcpThread)); - free(stt); + SCFree(stt); SCReturnInt(TM_ECODE_OK); } @@ -3993,7 +3993,7 @@ char *StreamTcpParseOSPolicy (char *conf_var_name) goto end; /* the + 2 is for the '.' and the string termination character '\0' */ - conf_var_full_name = (char *)malloc(strlen(conf_var_type_name) + + conf_var_full_name = (char *)SCMalloc(strlen(conf_var_type_name) + strlen(conf_var_name) + 2); if (conf_var_full_name == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); @@ -4018,7 +4018,7 @@ char *StreamTcpParseOSPolicy (char *conf_var_name) end: if (conf_var_full_name != NULL) - free(conf_var_full_name); + SCFree(conf_var_full_name); SCReturnCharPtr(conf_var_value); diff --git a/src/stream.c b/src/stream.c index dd43b68679..fcf277a792 100644 --- a/src/stream.c +++ b/src/stream.c @@ -23,7 +23,7 @@ static Pool *stream_msg_pool = NULL; static SCMutex stream_msg_pool_mutex = PTHREAD_MUTEX_INITIALIZER; void *StreamMsgAlloc(void *null) { - StreamMsg *s = malloc(sizeof(StreamMsg)); + StreamMsg *s = SCMalloc(sizeof(StreamMsg)); if (s == NULL) return NULL; @@ -41,7 +41,7 @@ void StreamMsgFree(void *ptr) { return; StreamMsg *s = (StreamMsg *)ptr; - free(s); + SCFree(s); return; } @@ -158,7 +158,7 @@ void StreamMsgQueuesDeinit(char quiet) { /** \brief alloc a stream msg queue * \retval smq ptr to the queue or NULL */ StreamMsgQueue *StreamMsgQueueGetNew(void) { - StreamMsgQueue *smq = malloc(sizeof(StreamMsgQueue)); + StreamMsgQueue *smq = SCMalloc(sizeof(StreamMsgQueue)); if (smq == NULL) { return NULL; } @@ -174,7 +174,7 @@ StreamMsgQueue *StreamMsgQueueGetNew(void) { * \todo we may want to consider non empty queue's */ void StreamMsgQueueFree(StreamMsgQueue *q) { - free(q); + SCFree(q); } StreamMsgQueue *StreamMsgQueueGetByPort(uint16_t port) { diff --git a/src/suricata-common.h b/src/suricata-common.h index ebb9249ebd..278a9283c7 100644 --- a/src/suricata-common.h +++ b/src/suricata-common.h @@ -45,7 +45,6 @@ #ifdef OS_WIN32 #include "win32-misc.h" #endif /* OS_WIN32 */ - #if HAVE_CONFIG_H #include #endif @@ -99,11 +98,16 @@ #include #endif -#include "threads.h" - #include #define BUG_ON(x) assert(!(x)) +#include +#include "threads.h" +#include "util-debug.h" +#include "util-error.h" +#include "util-mem.h" + + /** type for the internal signature id. Since it's used in the matching engine * extensively keeping this as small as possible reduces the overall memory * footprint of the engine. Set to uint32_t if the engine needs to support diff --git a/src/suricata.c b/src/suricata.c index 772701fde2..e5d886d8e7 100644 --- a/src/suricata.c +++ b/src/suricata.c @@ -137,6 +137,20 @@ static void SignalHandlerSigint(/*@unused@*/ int sig) { sigint_count = 1; sigfla static void SignalHandlerSigterm(/*@unused@*/ int sig) { sigterm_count = 1; sigflags |= SURICATA_SIGTERM; } static void SignalHandlerSighup(/*@unused@*/ int sig) { sighup_count = 1; sigflags |= SURICATA_SIGHUP; } +#ifdef DBG_MEM_ALLOC +#ifndef _GLOBAL_MEM_ +#define _GLOBAL_MEM_ +/* This counter doesn't complain realloc's(), it's gives + * an aproximation for the startup */ +uint64_t global_mem = 0; +#ifdef DBG_MEM_ALLOC_SKIP_STARTUP +uint8_t print_mem_flag = 0; +#else +uint8_t print_mem_flag = 1; +#endif +#endif +#endif + #ifndef OS_WIN32 static void SignalHandlerSetup(int sig, void (*handler)()) @@ -186,9 +200,9 @@ Packet *SetupPkt (void) if (p == NULL) { TmqDebugList(); - p = malloc(sizeof(Packet)); + p = SCMalloc(sizeof(Packet)); if (p == NULL) { - printf("ERROR: malloc failed: %s\n", strerror(errno)); + printf("ERROR: SCMalloc failed: %s\n", strerror(errno)); exit(EXIT_FAILURE); } @@ -314,7 +328,7 @@ static void SetBpfString(int optind, char *argv[]) { if (bpf_len == 0) return; - bpf_filter = malloc(bpf_len); + bpf_filter = SCMalloc(bpf_len); if (bpf_filter == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "%s", strerror(errno)); exit(EXIT_FAILURE); @@ -680,7 +694,11 @@ int main(int argc, char **argv) AppLayerParsersInitPostProcess(); #ifdef UNITTESTS + if (run_mode == MODE_UNITTEST) { +#ifdef DBG_MEM_ALLOC + SCLogInfo("Memory used at startup: %"PRIu64, global_mem); +#endif /* test and initialize the unittesting subsystem */ if(regex_arg == NULL){ regex_arg = ".*"; @@ -759,6 +777,10 @@ int main(int argc, char **argv) } } +#ifdef DBG_MEM_ALLOC + SCLogInfo("Total memory used (without SCFree()): %"PRIu64, global_mem); +#endif + exit(EXIT_SUCCESS); } #endif /* UNITTESTS */ @@ -777,9 +799,9 @@ int main(int argc, char **argv) int i = 0; for (i = 0; i < max_pending_packets; i++) { /* XXX pkt alloc function */ - Packet *p = malloc(sizeof(Packet)); + Packet *p = SCMalloc(sizeof(Packet)); if (p == NULL) { - printf("ERROR: malloc failed: %s\n", strerror(errno)); + printf("ERROR: SCMalloc failed: %s\n", strerror(errno)); exit(EXIT_FAILURE); } memset(p, 0, sizeof(Packet)); @@ -876,6 +898,13 @@ int main(int argc, char **argv) /* Un-pause all the paused threads */ TmThreadContinueThreads(); +#ifdef DBG_MEM_ALLOC + SCLogInfo("Memory used at startup: %"PRIu64, global_mem); +#ifdef DBG_MEM_ALLOC_SKIP_STARTUP + print_mem_flag = 1; +#endif +#endif + while(1) { if (sigflags) { SCLogInfo("signal received"); @@ -926,12 +955,20 @@ int main(int argc, char **argv) usleep(100); } + FlowShutdown(); FlowPrintQueueInfo(); StreamTcpFreeConfig(STREAM_VERBOSE); HTPFreeConfig(); HTPAtExitPrintStats(); +#ifdef DBG_MEM_ALLOC + SCLogInfo("Total memory used (without SCFree()): %"PRIu64, global_mem); +#ifdef DBG_MEM_ALLOC_SKIP_STARTUP + print_mem_flag = 0; +#endif +#endif + /** \todo review whats needed here */ #ifdef __SC_CUDA_SUPPORT__ if (PatternMatchDefaultMatcher() == MPM_B2G_CUDA) { diff --git a/src/suricata.h b/src/suricata.h index 09f72807f6..4426786a9c 100644 --- a/src/suricata.h +++ b/src/suricata.h @@ -29,6 +29,7 @@ uint32_t dbg_maxpending; SCMutex mutex_pending; SCCondT cond_pending; + /* Run mode */ enum { MODE_UNKNOWN = 0, diff --git a/src/tm-modules.c b/src/tm-modules.c index 98f9da6597..157f0e9fea 100644 --- a/src/tm-modules.c +++ b/src/tm-modules.c @@ -46,11 +46,11 @@ TmModule *TmModuleGetByName(char *name) { LogFileCtx *LogFileNewCtx() { LogFileCtx* lf_ctx; - lf_ctx=(LogFileCtx*)malloc(sizeof(LogFileCtx)); + lf_ctx=(LogFileCtx*)SCMalloc(sizeof(LogFileCtx)); if(lf_ctx == NULL) { - printf("LogFileCtxNew: Couldn't malloc \n"); + printf("LogFileCtxNew: Couldn't SCMalloc \n"); return NULL; } memset(lf_ctx, 0, sizeof(LogFileCtx)); @@ -78,10 +78,10 @@ int LogFileFreeCtx(LogFileCtx *lf_ctx) SCMutexUnlock(&lf_ctx->fp_mutex); } if (lf_ctx->prefix != NULL) - free(lf_ctx->prefix); + SCFree(lf_ctx->prefix); if(lf_ctx->filename != NULL) - free(lf_ctx->filename); - free(lf_ctx); + SCFree(lf_ctx->filename); + SCFree(lf_ctx); ret=1; } diff --git a/src/tm-queues.c b/src/tm-queues.c index 6d04cb9d80..872fdc8bfb 100644 --- a/src/tm-queues.c +++ b/src/tm-queues.c @@ -9,7 +9,7 @@ static uint16_t tmq_id = 0; static Tmq tmqs[TMQ_MAX_QUEUES]; Tmq* TmqAlloc(void) { - Tmq *q = malloc(sizeof(Tmq)); + Tmq *q = SCMalloc(sizeof(Tmq)); if (q == NULL) goto error; diff --git a/src/tm-threads.c b/src/tm-threads.c index bf3799b378..f4233c024c 100644 --- a/src/tm-threads.c +++ b/src/tm-threads.c @@ -525,7 +525,7 @@ TmEcode TmThreadSetSlots(ThreadVars *tv, char *name, void *(*fn_p)(void *)) { goto error; } - tv->tm_slots = malloc(size); + tv->tm_slots = SCMalloc(size); if (tv->tm_slots == NULL) goto error; memset(tv->tm_slots, 0, size); @@ -550,7 +550,7 @@ void Tm1SlotSetFunc(ThreadVars *tv, TmModule *tm, void *data) { void TmVarSlotSetFuncAppend(ThreadVars *tv, TmModule *tm, void *data) { TmVarSlot *s = (TmVarSlot *)tv->tm_slots; - TmSlot *slot = malloc(sizeof(TmSlot)); + TmSlot *slot = SCMalloc(sizeof(TmSlot)); if (slot == NULL) return; @@ -702,7 +702,7 @@ ThreadVars *TmThreadCreate(char *name, char *inq_name, char *inqh_name, SCLogDebug("creating thread \"%s\"...", name); /* XXX create separate function for this: allocate a thread container */ - tv = malloc(sizeof(ThreadVars)); + tv = SCMalloc(sizeof(ThreadVars)); if (tv == NULL) goto error; memset(tv, 0, sizeof(ThreadVars)); @@ -1090,7 +1090,7 @@ void TmThreadSetAOF(ThreadVars *tv, uint8_t aof) */ void TmThreadInitMC(ThreadVars *tv) { - if ( (tv->m = malloc(sizeof(SCMutex))) == NULL) { + if ( (tv->m = SCMalloc(sizeof(SCMutex))) == NULL) { printf("Error allocating memory\n"); exit(0); } @@ -1100,7 +1100,7 @@ void TmThreadInitMC(ThreadVars *tv) exit(0); } - if ( (tv->cond = malloc(sizeof(SCCondT))) == NULL) { + if ( (tv->cond = SCMalloc(sizeof(SCCondT))) == NULL) { printf("Error allocating memory\n"); exit(0); } diff --git a/src/tmqh-flow.c b/src/tmqh-flow.c index 11e5642161..942e0397d3 100644 --- a/src/tmqh-flow.c +++ b/src/tmqh-flow.c @@ -72,7 +72,7 @@ Packet *TmqhInputFlow(ThreadVars *tv) static int StoreQueueId(TmqhFlowCtx *ctx, char *name) { Tmq *tmq = TmqGetQueueByName(name); if (tmq == NULL) { - tmq = TmqCreateQueue(strdup(name)); + tmq = TmqCreateQueue(SCStrdup(name)); if (tmq == NULL) return -1; } @@ -83,10 +83,10 @@ static int StoreQueueId(TmqhFlowCtx *ctx, char *name) { if (ctx->queues == NULL) { ctx->size = 1; - ctx->queues = malloc(ctx->size * sizeof(uint16_t)); + ctx->queues = SCMalloc(ctx->size * sizeof(uint16_t)); } else { ctx->size++; - ctx->queues = realloc(ctx->queues, ctx->size * sizeof(uint16_t)); + ctx->queues = SCRealloc(ctx->queues, ctx->size * sizeof(uint16_t)); } if (ctx->queues == NULL) { return -1; @@ -108,12 +108,12 @@ void *TmqhOutputFlowSetupCtx(char *queue_str) { if (queue_str == NULL || strlen(queue_str) == 0) return NULL; - TmqhFlowCtx *ctx = malloc(sizeof(TmqhFlowCtx)); + TmqhFlowCtx *ctx = SCMalloc(sizeof(TmqhFlowCtx)); if (ctx == NULL) return NULL; memset(ctx,0x00,sizeof(TmqhFlowCtx)); - char *str = strdup(queue_str); + char *str = SCStrdup(queue_str); char *tstr = str; /* parse the comma separated string */ @@ -134,11 +134,11 @@ void *TmqhOutputFlowSetupCtx(char *queue_str) { tstr = comma ? (comma + 1) : comma; } while (tstr != NULL); - free(str); + SCFree(str); return (void *)ctx; error: - free(ctx); - free(str); + SCFree(ctx); + SCFree(str); return NULL; } diff --git a/src/util-bloomfilter-counting.c b/src/util-bloomfilter-counting.c index 6d3fc483f9..65f525553b 100644 --- a/src/util-bloomfilter-counting.c +++ b/src/util-bloomfilter-counting.c @@ -28,7 +28,7 @@ BloomFilterCounting *BloomFilterCountingInit(uint32_t size, uint8_t type, uint8_ } /* setup the filter */ - bf = malloc(sizeof(BloomFilterCounting)); + bf = SCMalloc(sizeof(BloomFilterCounting)); if (bf == NULL) goto error; memset(bf,0,sizeof(BloomFilterCounting)); @@ -38,7 +38,7 @@ BloomFilterCounting *BloomFilterCountingInit(uint32_t size, uint8_t type, uint8_ bf->Hash = Hash; /* setup the bitarray */ - bf->array = malloc(bf->array_size * bf->type); + bf->array = SCMalloc(bf->array_size * bf->type); if (bf->array == NULL) goto error; memset(bf->array,0,bf->array_size * bf->type); @@ -48,9 +48,9 @@ BloomFilterCounting *BloomFilterCountingInit(uint32_t size, uint8_t type, uint8_ error: if (bf != NULL) { if (bf->array != NULL) - free(bf->array); + SCFree(bf->array); - free(bf); + SCFree(bf); } return NULL; } @@ -58,9 +58,9 @@ error: void BloomFilterCountingFree(BloomFilterCounting *bf) { if (bf != NULL) { if (bf->array != NULL) - free(bf->array); + SCFree(bf->array); - free(bf); + SCFree(bf); } } diff --git a/src/util-bloomfilter.c b/src/util-bloomfilter.c index 51df43dfbb..079ab06606 100644 --- a/src/util-bloomfilter.c +++ b/src/util-bloomfilter.c @@ -18,7 +18,7 @@ BloomFilter *BloomFilterInit(uint32_t size, uint8_t iter, uint32_t (*Hash)(void } /* setup the filter */ - bf = malloc(sizeof(BloomFilter)); + bf = SCMalloc(sizeof(BloomFilter)); if (bf == NULL) goto error; memset(bf,0,sizeof(BloomFilter)); @@ -27,7 +27,7 @@ BloomFilter *BloomFilterInit(uint32_t size, uint8_t iter, uint32_t (*Hash)(void bf->Hash = Hash; /* setup the bitarray */ - bf->bitarray = malloc((bf->bitarray_size/8)+1); + bf->bitarray = SCMalloc((bf->bitarray_size/8)+1); if (bf->bitarray == NULL) goto error; memset(bf->bitarray,0,(bf->bitarray_size/8)+1); @@ -37,9 +37,9 @@ BloomFilter *BloomFilterInit(uint32_t size, uint8_t iter, uint32_t (*Hash)(void error: if (bf != NULL) { if (bf->bitarray != NULL) - free(bf->bitarray); + SCFree(bf->bitarray); - free(bf); + SCFree(bf); } return NULL; } @@ -47,9 +47,9 @@ error: void BloomFilterFree(BloomFilter *bf) { if (bf != NULL) { if (bf->bitarray != NULL) - free(bf->bitarray); + SCFree(bf->bitarray); - free(bf); + SCFree(bf); } } diff --git a/src/util-classification-config.c b/src/util-classification-config.c index 663ed7ebd1..b25e03a972 100644 --- a/src/util-classification-config.c +++ b/src/util-classification-config.c @@ -146,7 +146,7 @@ static char *SCClassConfStringToLowercase(const char *str) char *new_str = NULL; char *temp_str = NULL; - if ( (new_str = strdup(str)) == NULL) { + if ( (new_str = SCStrdup(str)) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } @@ -310,7 +310,7 @@ SCClassConfClasstype *SCClassConfAllocClasstype(const char *classtype, if (classtype == NULL) return NULL; - if ( (ct = malloc(sizeof(SCClassConfClasstype))) == NULL) { + if ( (ct = SCMalloc(sizeof(SCClassConfClasstype))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } @@ -322,7 +322,7 @@ SCClassConfClasstype *SCClassConfAllocClasstype(const char *classtype, } if (classtype_desc != NULL && - (ct->classtype_desc = strdup(classtype_desc)) == NULL) { + (ct->classtype_desc = SCStrdup(classtype_desc)) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } @@ -341,12 +341,12 @@ void SCClassConfDeAllocClasstype(SCClassConfClasstype *ct) { if (ct != NULL) { if (ct->classtype != NULL) - free(ct->classtype); + SCFree(ct->classtype); if (ct->classtype_desc != NULL) - free(ct->classtype_desc); + SCFree(ct->classtype_desc); - free(ct); + SCFree(ct); } return; diff --git a/src/util-cuda-handlers.c b/src/util-cuda-handlers.c index 1576df52c3..872923c2fb 100644 --- a/src/util-cuda-handlers.c +++ b/src/util-cuda-handlers.c @@ -310,14 +310,14 @@ int SCCudaHlGetCudaDevicePtr(CUdeviceptr *device_ptr, const char *name, return 0; } - new_module_device_ptr = malloc(sizeof(SCCudaHlModuleDevicePointer)); + new_module_device_ptr = SCMalloc(sizeof(SCCudaHlModuleDevicePointer)); if (new_module_device_ptr == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } memset(new_module_device_ptr, 0, sizeof(SCCudaHlModuleDevicePointer)); - if ( (new_module_device_ptr->name = strdup(name)) == NULL) { + if ( (new_module_device_ptr->name = SCStrdup(name)) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } @@ -350,7 +350,7 @@ int SCCudaHlGetCudaDevicePtr(CUdeviceptr *device_ptr, const char *name, error: if (new_module_device_ptr != NULL) - free(new_module_device_ptr); + SCFree(new_module_device_ptr); return -1; } @@ -462,14 +462,14 @@ int SCCudaHlRegisterModule(const char *name) } /* the module is not already registered. Register the module */ - new_data = malloc(sizeof(SCCudaHlModuleData)); + new_data = SCMalloc(sizeof(SCCudaHlModuleData)); if (new_data == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } memset(new_data, 0, sizeof(SCCudaHlModuleData)); - if ( (new_data->name = strdup(name)) == NULL) { + if ( (new_data->name = SCStrdup(name)) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } @@ -530,13 +530,13 @@ int SCCudaHlDeRegisterModule(const char *name) device_ptr = device_ptr->next; if (SCCudaMemFree(temp_device_ptr->d_ptr) == -1) goto error; - free(temp_device_ptr->name); - free(temp_device_ptr); + SCFree(temp_device_ptr->name); + SCFree(temp_device_ptr); } data->device_ptrs = NULL; if (data->name != NULL) - free((void *)data->name); + SCFree((void *)data->name); /* clean the dispatcher function registered */ data->SCCudaHlDispFunc = NULL; @@ -564,7 +564,7 @@ int SCCudaHlDeRegisterModule(const char *name) } /* delete the module data instance */ - free(data); + SCFree(data); /* mission accomplished. let's go */ return 0; @@ -698,7 +698,7 @@ void SCCudaHlProcessUriWithDispatcher(uint8_t *uri, uint16_t uri_len, { Packet *out_p = NULL; - Packet *p = malloc(sizeof(Packet)); + Packet *p = SCMalloc(sizeof(Packet)); if (p == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); @@ -727,7 +727,7 @@ void SCCudaHlProcessUriWithDispatcher(uint8_t *uri, uint16_t uri_len, * queued the packet and retrieve the results */ *((uint32_t *)result) = p->cuda_matches; - free(p); + SCFree(p); return; } diff --git a/src/util-cuda.c b/src/util-cuda.c index d4ad757816..9746284b9e 100644 --- a/src/util-cuda.c +++ b/src/util-cuda.c @@ -608,7 +608,7 @@ int SCCudaMemAlloc(CUdeviceptr *dptr, unsigned int byte_size) * calls to functions such as cuMemcpy(). Since the memory can be * accessed directly by the device, it can be read or written with * much higher bandwidth than pageable memory obtained with functions - * such as malloc(). Allocating excessive amounts of memory with + * such as SCMalloc(). Allocating excessive amounts of memory with * cuMemAllocHost() may degrade system performance, since it reduces * the amount of memory available to the system for paging. As a result, * this function is best used sparingly to allocate staging areas for @@ -1830,7 +1830,7 @@ int SCCudaMemGetInfo(unsigned int *free, unsigned int *total) * functions such as cuMemcpyHtoD(). Since the memory can be accessed * directly by the device, it can be read or written with much higher * bandwidth than pageable memory obtained with functions such as - * malloc(). Allocating excessive amounts of pinned memory may degrade + * SCMalloc(). Allocating excessive amounts of pinned memory may degrade * system performance, since it reduces the amount of memory available * to the system for paging. As a result, this function is best used * sparingly to allocate staging areas for data exchange between host @@ -3594,7 +3594,7 @@ static int SCCudaDeviceGetAttribute(int *pi, CUdevice_attribute attrib, */ static SCCudaDevice *SCCudaAllocSCCudaDevice(void) { - SCCudaDevice *device = malloc(sizeof(SCCudaDevice)); + SCCudaDevice *device = SCMalloc(sizeof(SCCudaDevice)); if (device == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); @@ -3612,7 +3612,7 @@ static SCCudaDevice *SCCudaAllocSCCudaDevice(void) */ static void SCCudaDeAllocSCCudaDevice(SCCudaDevice *device) { - free(device); + SCFree(device); return; } @@ -3625,7 +3625,7 @@ static void SCCudaDeAllocSCCudaDevice(SCCudaDevice *device) */ static SCCudaDevices *SCCudaAllocSCCudaDevices(void) { - SCCudaDevices *devices = malloc(sizeof(SCCudaDevices)); + SCCudaDevices *devices = SCMalloc(sizeof(SCCudaDevices)); if (devices == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); @@ -3652,10 +3652,10 @@ static void SCCudaDeAllocSCCudaDevices(SCCudaDevices *devices) for (i = 0; i < devices->count; i++) SCCudaDeAllocSCCudaDevice(devices->devices[i]); - free(devices->devices); + SCFree(devices->devices); } - free(devices); + SCFree(devices); return; } @@ -3676,7 +3676,7 @@ static SCCudaDevices *SCCudaGetDevices(void) if (SCCudaDeviceGetCount(&devices->count) == -1) goto error; - devices->devices = malloc(devices->count * sizeof(SCCudaDevice *)); + devices->devices = SCMalloc(devices->count * sizeof(SCCudaDevice *)); if (devices->devices == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); diff --git a/src/util-debug-filters.c b/src/util-debug-filters.c index 2b8b830047..d9906ce48b 100644 --- a/src/util-debug-filters.c +++ b/src/util-debug-filters.c @@ -78,29 +78,29 @@ static inline void SCLogAddToFGFFileList(SCLogFGFilterFile *fgf_file, SCLogFGFilterFunc *fgf_func_temp = NULL; SCLogFGFilterLine *fgf_line_temp = NULL; - if ( (fgf_file_temp = malloc(sizeof(SCLogFGFilterFile))) == NULL) { + if ( (fgf_file_temp = SCMalloc(sizeof(SCLogFGFilterFile))) == NULL) { printf("Error Allocating memory\n"); exit(EXIT_FAILURE); } memset(fgf_file_temp, 0, sizeof(SCLogFGFilterFile)); - if ( file != NULL && (fgf_file_temp->file = strdup(file)) == NULL) { + if ( file != NULL && (fgf_file_temp->file = SCStrdup(file)) == NULL) { printf("Error Allocating memory\n"); exit(EXIT_FAILURE); } - if ( (fgf_func_temp = malloc(sizeof(SCLogFGFilterFunc))) == NULL) { + if ( (fgf_func_temp = SCMalloc(sizeof(SCLogFGFilterFunc))) == NULL) { printf("Error Allocating memory\n"); exit(EXIT_FAILURE); } memset(fgf_func_temp, 0, sizeof(SCLogFGFilterFunc)); - if ( function != NULL && (fgf_func_temp->func = strdup(function)) == NULL) { + if ( function != NULL && (fgf_func_temp->func = SCStrdup(function)) == NULL) { printf("Error Allocating memory\n"); exit(EXIT_FAILURE); } - if ( (fgf_line_temp = malloc(sizeof(SCLogFGFilterLine))) == NULL) { + if ( (fgf_line_temp = SCMalloc(sizeof(SCLogFGFilterLine))) == NULL) { printf("Error Allocating memory\n"); exit(EXIT_FAILURE); } @@ -142,18 +142,18 @@ static inline void SCLogAddToFGFFuncList(SCLogFGFilterFile *fgf_file, SCLogFGFilterFunc *fgf_func_temp = NULL; SCLogFGFilterLine *fgf_line_temp = NULL; - if ( (fgf_func_temp = malloc(sizeof(SCLogFGFilterFunc))) == NULL) { + if ( (fgf_func_temp = SCMalloc(sizeof(SCLogFGFilterFunc))) == NULL) { printf("Error Allocating memory\n"); exit(EXIT_FAILURE); } memset(fgf_func_temp, 0, sizeof(SCLogFGFilterFunc)); - if ( function != NULL && (fgf_func_temp->func = strdup(function)) == NULL) { + if ( function != NULL && (fgf_func_temp->func = SCStrdup(function)) == NULL) { printf("Error Allocating memory\n"); exit(EXIT_FAILURE); } - if ( (fgf_line_temp = malloc(sizeof(SCLogFGFilterLine))) == NULL) { + if ( (fgf_line_temp = SCMalloc(sizeof(SCLogFGFilterLine))) == NULL) { printf("Error Allocating memory\n"); exit(EXIT_FAILURE); } @@ -191,7 +191,7 @@ static inline void SCLogAddToFGFLineList(SCLogFGFilterFunc *fgf_func, { SCLogFGFilterLine *fgf_line_temp = NULL; - if ( (fgf_line_temp = malloc(sizeof(SCLogFGFilterLine))) == NULL) { + if ( (fgf_line_temp = SCMalloc(sizeof(SCLogFGFilterLine))) == NULL) { printf("Error Allocating memory\n"); exit(EXIT_FAILURE); } @@ -518,21 +518,21 @@ void SCLogReleaseFGFilters(void) while(fgf_line != NULL) { temp = fgf_line; fgf_line = fgf_line->next; - free(temp); + SCFree(temp); } if (fgf_func->func != NULL) - free(fgf_func->func); + SCFree(fgf_func->func); temp = fgf_func; fgf_func = fgf_func->next; - free(temp); + SCFree(temp); } if (fgf_file->file != NULL) - free(fgf_file->file); + SCFree(fgf_file->file); temp = fgf_file; fgf_file = fgf_file->next; - free(temp); + SCFree(temp); } SCMutexUnlock(&sc_log_fg_filters_m[i]); @@ -615,8 +615,8 @@ static inline void SCLogReleaseFDFilter(SCLogFDFilter *fdf) { if (fdf != NULL) { if (fdf->func != NULL) - free(fdf->func); - free(fdf); + SCFree(fdf->func); + SCFree(fdf); } return; @@ -739,7 +739,7 @@ int SCLogCheckFDFilterEntry(const char *function) return 1; } - if ( (thread_list_temp = malloc(sizeof(SCLogFDFilterThreadList))) == NULL) { + if ( (thread_list_temp = SCMalloc(sizeof(SCLogFDFilterThreadList))) == NULL) { printf("Error allocating memory\n"); exit(EXIT_FAILURE); } @@ -857,13 +857,13 @@ int SCLogAddFDFilter(const char *function) curr = curr->next; } - if ( (temp = malloc(sizeof(SCLogFDFilter))) == NULL) { + if ( (temp = SCMalloc(sizeof(SCLogFDFilter))) == NULL) { printf("Error allocating memory\n"); exit(EXIT_FAILURE); } memset(temp, 0, sizeof(SCLogFDFilter)); - if ( (temp->func = strdup(function)) == NULL) { + if ( (temp->func = SCStrdup(function)) == NULL) { printf("Error Allocating memory\n"); exit(EXIT_FAILURE); } diff --git a/src/util-fmemopen.c b/src/util-fmemopen.c index 332555df94..0b92013f8f 100644 --- a/src/util-fmemopen.c +++ b/src/util-fmemopen.c @@ -136,7 +136,7 @@ static int WriteFn(void *handler, const char *buf, int size) { * \retval 0 on succesful */ static int CloseFn(void *handler) { - free (handler); + SCFree(handler); return 0; } @@ -148,7 +148,7 @@ static int CloseFn(void *handler) { * \retval pointer to the file; NULL if something is wrong */ FILE *SCFmemopen(void *buf, size_t size, const char *mode) { - SCFmem *mem = (SCFmem *) malloc(sizeof(SCFmem)); + SCFmem *mem = (SCFmem *) SCMalloc(sizeof(SCFmem)); memset(mem, 0, sizeof(SCFmem)); mem->size = size, mem->buffer = buf; diff --git a/src/util-hash.c b/src/util-hash.c index bf0b1259f1..24ad11534f 100644 --- a/src/util-hash.c +++ b/src/util-hash.c @@ -23,7 +23,7 @@ HashTable* HashTableInit(uint32_t size, uint32_t (*Hash)(struct HashTable_ *, vo } /* setup the filter */ - ht = malloc(sizeof(HashTable)); + ht = SCMalloc(sizeof(HashTable)); if (ht == NULL) goto error; memset(ht,0,sizeof(HashTable)); @@ -37,7 +37,7 @@ HashTable* HashTableInit(uint32_t size, uint32_t (*Hash)(struct HashTable_ *, vo ht->Compare = HashTableDefaultCompare; /* setup the bitarray */ - ht->array = malloc(ht->array_size * sizeof(HashTableBucket *)); + ht->array = SCMalloc(ht->array_size * sizeof(HashTableBucket *)); if (ht->array == NULL) goto error; memset(ht->array,0,ht->array_size * sizeof(HashTableBucket *)); @@ -47,9 +47,9 @@ HashTable* HashTableInit(uint32_t size, uint32_t (*Hash)(struct HashTable_ *, vo error: if (ht != NULL) { if (ht->array != NULL) - free(ht->array); + SCFree(ht->array); - free(ht); + SCFree(ht); } return NULL; } @@ -67,16 +67,16 @@ void HashTableFree(HashTable *ht) { HashTableBucket *next_hashbucket = hashbucket->next; if (ht->Free != NULL) ht->Free(hashbucket->data); - free(hashbucket); + SCFree(hashbucket); hashbucket = next_hashbucket; } } /* free the arrray */ if (ht->array != NULL) - free(ht->array); + SCFree(ht->array); - free(ht); + SCFree(ht); } void HashTablePrint(HashTable *ht) { @@ -92,7 +92,7 @@ int HashTableAdd(HashTable *ht, void *data, uint16_t datalen) { uint32_t hash = ht->Hash(ht, data, datalen); - HashTableBucket *hb = malloc(sizeof(HashTableBucket)); + HashTableBucket *hb = SCMalloc(sizeof(HashTableBucket)); if (hb == NULL) { goto error; } @@ -128,7 +128,7 @@ int HashTableRemove(HashTable *ht, void *data, uint16_t datalen) { if (ht->array[hash]->next == NULL) { if (ht->Free != NULL) ht->Free(ht->array[hash]->data); - free(ht->array[hash]); + SCFree(ht->array[hash]); ht->array[hash] = NULL; return 0; } @@ -147,7 +147,7 @@ int HashTableRemove(HashTable *ht, void *data, uint16_t datalen) { /* remove this */ if (ht->Free != NULL) ht->Free(hashbucket->data); - free(hashbucket); + SCFree(hashbucket); return 0; } diff --git a/src/util-hashlist.c b/src/util-hashlist.c index 6301fe4270..72f26ad785 100644 --- a/src/util-hashlist.c +++ b/src/util-hashlist.c @@ -24,7 +24,7 @@ HashListTable* HashListTableInit(uint32_t size, uint32_t (*Hash)(struct HashList } /* setup the filter */ - ht = malloc(sizeof(HashListTable)); + ht = SCMalloc(sizeof(HashListTable)); if (ht == NULL) goto error; memset(ht,0,sizeof(HashListTable)); @@ -38,7 +38,7 @@ HashListTable* HashListTableInit(uint32_t size, uint32_t (*Hash)(struct HashList ht->Compare = HashListTableDefaultCompare; /* setup the bitarray */ - ht->array = malloc(ht->array_size * sizeof(HashListTableBucket *)); + ht->array = SCMalloc(ht->array_size * sizeof(HashListTableBucket *)); if (ht->array == NULL) goto error; memset(ht->array,0,ht->array_size * sizeof(HashListTableBucket *)); @@ -50,9 +50,9 @@ HashListTable* HashListTableInit(uint32_t size, uint32_t (*Hash)(struct HashList error: if (ht != NULL) { if (ht->array != NULL) - free(ht->array); + SCFree(ht->array); - free(ht); + SCFree(ht); } return NULL; } @@ -70,16 +70,16 @@ void HashListTableFree(HashListTable *ht) { HashListTableBucket *next_hashbucket = hashbucket->bucknext; if (ht->Free != NULL) ht->Free(hashbucket->data); - free(hashbucket); + SCFree(hashbucket); hashbucket = next_hashbucket; } } - /* free the arrray */ + /* free the array */ if (ht->array != NULL) - free(ht->array); + SCFree(ht->array); - free(ht); + SCFree(ht); } void HashListTablePrint(HashListTable *ht) { @@ -97,7 +97,7 @@ int HashListTableAdd(HashListTable *ht, void *data, uint16_t datalen) { SCLogDebug("ht %p hash %"PRIu32"", ht, hash); - HashListTableBucket *hb = malloc(sizeof(HashListTableBucket)); + HashListTableBucket *hb = SCMalloc(sizeof(HashListTableBucket)); if (hb == NULL) { goto error; } @@ -160,7 +160,7 @@ int HashListTableRemove(HashListTable *ht, void *data, uint16_t datalen) { if (ht->Free != NULL) ht->Free(hb->data); - free(ht->array[hash]); + SCFree(ht->array[hash]); ht->array[hash] = NULL; return 0; } @@ -197,7 +197,7 @@ int HashListTableRemove(HashListTable *ht, void *data, uint16_t datalen) { /* remove this */ if (ht->Free != NULL) ht->Free(hashbucket->data); - free(hashbucket); + SCFree(hashbucket); return 0; } diff --git a/src/util-host-os-info.c b/src/util-host-os-info.c index 18e545f814..8a8f4669c8 100644 --- a/src/util-host-os-info.c +++ b/src/util-host-os-info.c @@ -49,13 +49,13 @@ static struct in_addr *SCHInfoValidateIPV4Address(const char *addr_str) { struct in_addr *addr = NULL; - if ( (addr = malloc(sizeof(struct in_addr))) == NULL) { + if ( (addr = SCMalloc(sizeof(struct in_addr))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } if (inet_pton(AF_INET, addr_str, addr) <= 0) { - free(addr); + SCFree(addr); return NULL; } @@ -76,13 +76,13 @@ static struct in6_addr *SCHInfoValidateIPV6Address(const char *addr_str) { struct in6_addr *addr = NULL; - if ( (addr = malloc(sizeof(struct in6_addr))) == NULL) { + if ( (addr = SCMalloc(sizeof(struct in6_addr))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } if (inet_pton(AF_INET6, addr_str, addr) <= 0) { - free(addr); + SCFree(addr); return NULL; } @@ -103,7 +103,7 @@ static void *SCHInfoAllocUserDataOSPolicy(const char *host_os) { int *user_data = NULL; - if ( (user_data = malloc(sizeof(int))) == NULL) { + if ( (user_data = SCMalloc(sizeof(int))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } @@ -112,7 +112,7 @@ static void *SCHInfoAllocUserDataOSPolicy(const char *host_os) if ( (*user_data = SCMapEnumNameToValue(host_os, sc_hinfo_os_policy_map)) == -1) { SCLogError(SC_ERR_INVALID_ENUM_MAP, "Invalid enum map inside " "SCHInfoAddHostOSInfo()"); - free(user_data); + SCFree(user_data); return NULL; } @@ -127,7 +127,7 @@ static void *SCHInfoAllocUserDataOSPolicy(const char *host_os) static void SCHInfoFreeUserDataOSPolicy(void *data) { if (data != NULL) - free(data); + SCFree(data); return; } @@ -211,7 +211,7 @@ int SCHInfoAddHostOSInfo(char *host_os, char *host_os_ip_range, int is_ipv4) host_os_ip_range = "::/0"; } - if ( (ip_str = strdup(host_os_ip_range)) == NULL) { + if ( (ip_str = SCStrdup(host_os_ip_range)) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } @@ -243,7 +243,7 @@ int SCHInfoAddHostOSInfo(char *host_os, char *host_os_ip_range, int is_ipv4) netmask_value = atoi(netmask_str); if (netmask_value < 0 || netmask_value > 32) { SCLogError(SC_ERR_INVALID_IP_NETBLOCK, "Invalid IPV4 Netblock"); - free(ipv4_addr); + SCFree(ipv4_addr); return -1; } @@ -265,7 +265,7 @@ int SCHInfoAddHostOSInfo(char *host_os, char *host_os_ip_range, int is_ipv4) netmask_value = atoi(netmask_str); if (netmask_value < 0 || netmask_value > 128) { SCLogError(SC_ERR_INVALID_IP_NETBLOCK, "Invalid IPV6 Netblock"); - free(ipv6_addr); + SCFree(ipv6_addr); return -1; } diff --git a/src/util-mem.h b/src/util-mem.h new file mode 100644 index 0000000000..876c112670 --- /dev/null +++ b/src/util-mem.h @@ -0,0 +1,105 @@ +/** + * Copyright (c) 2009 Open Information Security Foundation + * + * \author Pablo Rincon Crespo + * + * Add wrappers for functions that allocate/free memory here. + * Currently we have malloc, calloc, realloc, strdup and free, + * but there are more. + */ + +//#ifndef __UTIL_MEM_H__ +//#define __UTIL_MEM_H__ + +/* Use this only if you want to debug memory allocation and free() + * It will log a lot of lines more, so think that is a performance killer */ + +/* Uncomment this if you want to print memory allocations and free's() */ +//#define DBG_MEM_ALLOC + +/* Uncomment this if you want to print mallocs at the startup (recommended) */ +//#define DBG_MEM_ALLOC_SKIP_STARTUP + +#ifdef DBG_MEM_ALLOC + +#define SCMalloc(a) ({ \ + void *ptrmem = NULL; \ + extern uint64_t global_mem; \ + extern uint8_t print_mem_flag; \ + ptrmem = malloc(a); \ + if (ptrmem == NULL && a > 0) { \ + SCLogError(SC_ERR_MEM_ALLOC, "Malloc of size %"PRIu64" failed! exiting.", (uint64_t)a); \ + exit(EXIT_FAILURE); \ + } \ + global_mem += a; \ + if (print_mem_flag == 1) \ + SCLogInfo("SCMalloc return at %p of size %"PRIu64, ptrmem, (uint64_t)a); \ + (void*)ptrmem; \ +}) + +#define SCRealloc(x, a) ({ \ + void *ptrmem = NULL; \ + extern uint64_t global_mem; \ + extern uint8_t print_mem_flag; \ + ptrmem = realloc(x, a); \ + if (ptrmem == NULL && a > 0) { \ + SCLogError(SC_ERR_MEM_ALLOC, "Realloc of size %"PRIu64" failed! exiting.", (uint64_t)a); \ + exit(EXIT_FAILURE); \ + } \ + global_mem += a; \ + if (print_mem_flag == 1) \ + SCLogInfo("SCRealloc return at %p (old:%p) of size %"PRIu64, ptrmem, x, (uint64_t)a); \ + (void*)ptrmem; \ +}) + +#define SCCalloc(nm, a) ({ \ + void *ptrmem = NULL; \ + extern uint64_t global_mem; \ + extern uint8_t print_mem_flag; \ + ptrmem = calloc(nm, a); \ + if (ptrmem == NULL && a > 0) { \ + SCLogError(SC_ERR_MEM_ALLOC, "Calloc of size %"PRIu64" failed! exiting.", (uint64_t)a); \ + exit(EXIT_FAILURE); \ + } \ + global_mem += a*nm; \ + if (print_mem_flag == 1) \ + SCLogInfo("SCCalloc return at %p of size %"PRIu64" nm %"PRIu64, ptrmem, (uint64_t)a, (uint64_t)nm); \ + (void*)ptrmem; \ +}) + +#define SCStrdup(a) ({ \ + char *ptrmem = NULL; \ + extern uint64_t global_mem; \ + extern uint8_t print_mem_flag; \ + size_t len = strlen(a); \ + ptrmem = strdup(a); \ + if (ptrmem == NULL && len > 0) { \ + SCLogError(SC_ERR_MEM_ALLOC, "Strdup of size %"PRIu64" failed! exiting.", (uint64_t)len); \ + exit(EXIT_FAILURE); \ + } \ + global_mem += len; \ + if (print_mem_flag == 1) \ + SCLogInfo("SCStrdup return at %p of size %"PRIu64, ptrmem, (uint64_t)len); \ + (void*)ptrmem; \ +}) + +#define SCFree(a) ({ \ + extern uint8_t print_mem_flag; \ + if (print_mem_flag == 1) \ + SCLogInfo("SCFree at %p", a); \ + free(a); \ +}) + +#else + +/* Replace them with the normal calls, so we get no performance penalty */ +#define SCMalloc(a) malloc(a) +#define SCCalloc(nm,a) calloc(nm,a) +#define SCRealloc(x,a) realloc(x,a) +#define SCStrdup(a) strdup(a) +#define SCFree(a) free(a) + +#endif + +//#endif /* __UTIL_MEM_H__ */ + diff --git a/src/util-mpm-b2g-cuda.c b/src/util-mpm-b2g-cuda.c index 14bb16d557..ff8dc3ab65 100644 --- a/src/util-mpm-b2g-cuda.c +++ b/src/util-mpm-b2g-cuda.c @@ -550,7 +550,7 @@ void B2gCudaPrintInfo(MpmCtx *mpm_ctx) static inline B2gCudaPattern *B2gCudaAllocPattern(MpmCtx *mpm_ctx) { - B2gCudaPattern *p = malloc(sizeof(B2gCudaPattern)); + B2gCudaPattern *p = SCMalloc(sizeof(B2gCudaPattern)); if (p == NULL) { printf("ERROR: B2gAllocPattern: malloc failed\n"); exit(EXIT_FAILURE); @@ -565,7 +565,7 @@ static inline B2gCudaPattern *B2gCudaAllocPattern(MpmCtx *mpm_ctx) static inline B2gCudaHashItem *B2gCudaAllocHashItem(MpmCtx *mpm_ctx) { - B2gCudaHashItem *hi = malloc(sizeof(B2gCudaHashItem)); + B2gCudaHashItem *hi = SCMalloc(sizeof(B2gCudaHashItem)); if (hi == NULL) { printf("ERROR: B2gCudaAllocHashItem: malloc failed\n"); exit(EXIT_FAILURE); @@ -588,7 +588,7 @@ static void B2gCudaHashFree(MpmCtx *mpm_ctx, B2gCudaHashItem *hi) mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= sizeof(B2gCudaHashItem); - free(hi); + SCFree(hi); return; } @@ -686,19 +686,19 @@ void B2gCudaFreePattern(MpmCtx *mpm_ctx, B2gCudaPattern *p) MpmEndMatchFreeAll(mpm_ctx, p->em); if (p && p->cs && p->cs != p->ci) { - free(p->cs); + SCFree(p->cs); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= p->len; } if (p && p->ci) { - free(p->ci); + SCFree(p->ci); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= p->len; } if (p) { - free(p); + SCFree(p); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= sizeof(B2gCudaPattern); } @@ -735,7 +735,7 @@ static inline int B2gCudaAddPattern(MpmCtx *mpm_ctx, uint8_t *pat, p->flags |= B2G_CUDA_NOCASE; /* setup the case insensitive part of the pattern */ - p->ci = malloc(patlen); + p->ci = SCMalloc(patlen); if (p->ci == NULL) goto error; mpm_ctx->memory_cnt++; @@ -751,7 +751,7 @@ static inline int B2gCudaAddPattern(MpmCtx *mpm_ctx, uint8_t *pat, /* no diff between cs and ci: pat is lowercase */ p->cs = p->ci; } else { - p->cs = malloc(patlen); + p->cs = SCMalloc(patlen); if (p->cs == NULL) goto error; mpm_ctx->memory_cnt++; @@ -875,7 +875,7 @@ static void B2gCudaPrepareScanHash(MpmCtx *mpm_ctx) uint16_t idx = 0; uint8_t idx8 = 0; - ctx->scan_hash = (B2gCudaHashItem **)malloc(sizeof(B2gCudaHashItem *) * + ctx->scan_hash = (B2gCudaHashItem **)SCMalloc(sizeof(B2gCudaHashItem *) * ctx->scan_hash_size); if (ctx->scan_hash == NULL) goto error; @@ -885,7 +885,7 @@ static void B2gCudaPrepareScanHash(MpmCtx *mpm_ctx) mpm_ctx->memory_size += (sizeof(B2gCudaHashItem *) * ctx->scan_hash_size); #ifdef B2G_CUDA_SCAN2 - ctx->scan_hash2 = (B2gCudaHashItem **)malloc(sizeof(B2gCudaHashItem *) * + ctx->scan_hash2 = (B2gCudaHashItem **)SCMalloc(sizeof(B2gCudaHashItem *) * ctx->scan_hash_size); if (ctx->scan_hash2 == NULL) goto error; @@ -896,7 +896,7 @@ static void B2gCudaPrepareScanHash(MpmCtx *mpm_ctx) #endif /* alloc the pminlen array */ - ctx->scan_pminlen = (uint8_t *)malloc(sizeof(uint8_t) * ctx->scan_hash_size); + ctx->scan_pminlen = (uint8_t *)SCMalloc(sizeof(uint8_t) * ctx->scan_hash_size); if (ctx->scan_pminlen == NULL) goto error; memset(ctx->scan_pminlen, 0, sizeof(uint8_t) * ctx->scan_hash_size); @@ -979,7 +979,7 @@ static void B2gCudaPrepareScanHash(MpmCtx *mpm_ctx) } /* alloc the bloom array */ - ctx->scan_bloom = (BloomFilter **)malloc(sizeof(BloomFilter *) * ctx->scan_hash_size); + ctx->scan_bloom = (BloomFilter **)SCMalloc(sizeof(BloomFilter *) * ctx->scan_hash_size); if (ctx->scan_bloom == NULL) goto error; memset(ctx->scan_bloom, 0, sizeof(BloomFilter *) * ctx->scan_hash_size); @@ -1026,7 +1026,7 @@ static void B2gCudaPrepareSearchHash(MpmCtx *mpm_ctx) uint16_t idx = 0; uint8_t idx8 = 0; - ctx->search_hash = (B2gCudaHashItem **)malloc(sizeof(B2gCudaHashItem *) * + ctx->search_hash = (B2gCudaHashItem **)SCMalloc(sizeof(B2gCudaHashItem *) * ctx->search_hash_size); if (ctx->search_hash == NULL) goto error; memset(ctx->search_hash, 0, sizeof(B2gCudaHashItem *) * ctx->search_hash_size); @@ -1035,7 +1035,7 @@ static void B2gCudaPrepareSearchHash(MpmCtx *mpm_ctx) mpm_ctx->memory_size += (sizeof(B2gCudaHashItem *) * ctx->search_hash_size); /* alloc the pminlen array */ - ctx->search_pminlen = (uint8_t *)malloc(sizeof(uint8_t) * ctx->search_hash_size); + ctx->search_pminlen = (uint8_t *)SCMalloc(sizeof(uint8_t) * ctx->search_hash_size); if (ctx->search_pminlen == NULL) goto error; memset(ctx->search_pminlen, 0, sizeof(uint8_t) * ctx->search_hash_size); @@ -1094,7 +1094,7 @@ static void B2gCudaPrepareSearchHash(MpmCtx *mpm_ctx) } /* alloc the bloom array */ - ctx->search_bloom = (BloomFilter **)malloc(sizeof(BloomFilter *) * ctx->search_hash_size); + ctx->search_bloom = (BloomFilter **)SCMalloc(sizeof(BloomFilter *) * ctx->search_hash_size); if (ctx->search_bloom == NULL) goto error; memset(ctx->search_bloom, 0, sizeof(BloomFilter *) * ctx->search_hash_size); @@ -1134,7 +1134,7 @@ int B2gCudaBuildScanMatchArray(MpmCtx *mpm_ctx) { B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx->ctx; - ctx->scan_B2G = malloc(sizeof(B2G_CUDA_TYPE) * ctx->scan_hash_size); + ctx->scan_B2G = SCMalloc(sizeof(B2G_CUDA_TYPE) * ctx->scan_hash_size); if (ctx->scan_B2G == NULL) return -1; @@ -1173,7 +1173,7 @@ int B2gCudaBuildSearchMatchArray(MpmCtx *mpm_ctx) { B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx->ctx; - ctx->search_B2G = malloc(sizeof(B2G_CUDA_TYPE) * ctx->search_hash_size); + ctx->search_B2G = SCMalloc(sizeof(B2G_CUDA_TYPE) * ctx->search_hash_size); if (ctx->search_B2G == NULL) return -1; @@ -1279,7 +1279,7 @@ int B2gCudaPreparePatterns(MpmCtx *mpm_ctx) B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx->ctx; /* alloc the pattern array */ - ctx->parray = (B2gCudaPattern **)malloc(mpm_ctx->pattern_cnt * + ctx->parray = (B2gCudaPattern **)SCMalloc(mpm_ctx->pattern_cnt * sizeof(B2gCudaPattern *)); if (ctx->parray == NULL) goto error; @@ -1303,7 +1303,7 @@ int B2gCudaPreparePatterns(MpmCtx *mpm_ctx) } } /* we no longer need the hash, so free it's memory */ - free(ctx->init_hash); + SCFree(ctx->init_hash); ctx->init_hash = NULL; /* set 'm' to the smallest pattern size */ @@ -1432,7 +1432,7 @@ void B2gCudaInitCtx(MpmCtx *mpm_ctx, int module_handle) BUG_ON(mpm_ctx->ctx != NULL); - mpm_ctx->ctx = malloc(sizeof(B2gCudaCtx)); + mpm_ctx->ctx = SCMalloc(sizeof(B2gCudaCtx)); if (mpm_ctx->ctx == NULL) return; @@ -1443,7 +1443,7 @@ void B2gCudaInitCtx(MpmCtx *mpm_ctx, int module_handle) /* initialize the hash we use to speed up pattern insertions */ B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx->ctx; - ctx->init_hash = malloc(sizeof(B2gCudaPattern *) * INIT_HASH_SIZE); + ctx->init_hash = SCMalloc(sizeof(B2gCudaPattern *) * INIT_HASH_SIZE); if (ctx->init_hash == NULL) return; @@ -1527,7 +1527,7 @@ void B2gCudaDestroyCtx(MpmCtx *mpm_ctx) return; if (ctx->init_hash) { - free(ctx->init_hash); + SCFree(ctx->init_hash); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (INIT_HASH_SIZE * sizeof(B2gCudaPattern *)); } @@ -1540,19 +1540,19 @@ void B2gCudaDestroyCtx(MpmCtx *mpm_ctx) } } - free(ctx->parray); + SCFree(ctx->parray); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (mpm_ctx->pattern_cnt * sizeof(B2gCudaPattern)); } if (ctx->scan_B2G) { - free(ctx->scan_B2G); + SCFree(ctx->scan_B2G); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(B2G_CUDA_TYPE) * ctx->scan_hash_size); } if (ctx->search_B2G) { - free(ctx->search_B2G); + SCFree(ctx->search_B2G); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(B2G_CUDA_TYPE) * ctx->search_hash_size); } @@ -1569,7 +1569,7 @@ void B2gCudaDestroyCtx(MpmCtx *mpm_ctx) BloomFilterFree(ctx->scan_bloom[h]); } - free(ctx->scan_bloom); + SCFree(ctx->scan_bloom); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(BloomFilter *) * ctx->scan_hash_size); @@ -1584,7 +1584,7 @@ void B2gCudaDestroyCtx(MpmCtx *mpm_ctx) B2gCudaHashFree(mpm_ctx, ctx->scan_hash[h]); } - free(ctx->scan_hash); + SCFree(ctx->scan_hash); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(B2gCudaHashItem) * ctx->scan_hash_size); } @@ -1601,7 +1601,7 @@ void B2gCudaDestroyCtx(MpmCtx *mpm_ctx) BloomFilterFree(ctx->search_bloom[h]); } - free(ctx->search_bloom); + SCFree(ctx->search_bloom); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(BloomFilter *) * ctx->search_hash_size); @@ -1616,19 +1616,19 @@ void B2gCudaDestroyCtx(MpmCtx *mpm_ctx) B2gCudaHashFree(mpm_ctx, ctx->search_hash[h]); } - free(ctx->search_hash); + SCFree(ctx->search_hash); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(B2gCudaHashItem) * ctx->search_hash_size); } if (ctx->scan_pminlen) { - free(ctx->scan_pminlen); + SCFree(ctx->scan_pminlen); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(uint8_t) * ctx->scan_hash_size); } if (ctx->search_pminlen) { - free(ctx->search_pminlen); + SCFree(ctx->search_pminlen); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(uint8_t) * ctx->search_hash_size); } @@ -1645,7 +1645,7 @@ void B2gCudaDestroyCtx(MpmCtx *mpm_ctx) ctx->cuda_scan_B2G = 0; } - free(mpm_ctx->ctx); + SCFree(mpm_ctx->ctx); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= sizeof(B2gCudaCtx); @@ -1659,7 +1659,7 @@ void B2gCudaThreadInitCtx(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, /* size can be null when optimized */ if (sizeof(B2gCudaThreadCtx) > 0) { - mpm_thread_ctx->ctx = malloc(sizeof(B2gCudaThreadCtx)); + mpm_thread_ctx->ctx = SCMalloc(sizeof(B2gCudaThreadCtx)); if (mpm_thread_ctx->ctx == NULL) return; @@ -1675,7 +1675,7 @@ void B2gCudaThreadInitCtx(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, * unique id and is the array lookup key at the same time */ uint32_t keys = matchsize + 1; if (keys > 0) { - mpm_thread_ctx->match = malloc(keys * sizeof(MpmMatchBucket)); + mpm_thread_ctx->match = SCMalloc(keys * sizeof(MpmMatchBucket)); if (mpm_thread_ctx->match == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Could not setup memory for " "pattern matcher: %s", strerror(errno)); @@ -1702,14 +1702,14 @@ void B2gCudaThreadDestroyCtx(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx) if (ctx != NULL) { mpm_thread_ctx->memory_cnt--; mpm_thread_ctx->memory_size -= sizeof(B2gCudaThreadCtx); - free(mpm_thread_ctx->ctx); + SCFree(mpm_thread_ctx->ctx); } if (mpm_thread_ctx->match != NULL) { mpm_thread_ctx->memory_cnt--; mpm_thread_ctx->memory_size -= ((mpm_thread_ctx->matchsize + 1) * sizeof(MpmMatchBucket)); - free(mpm_thread_ctx->match); + SCFree(mpm_thread_ctx->match); } MpmMatchFreeSpares(mpm_thread_ctx, mpm_thread_ctx->sparelist); @@ -2636,7 +2636,7 @@ void B2gCudaKillDispatcherThreadRC(void) TmThreadKillThread(tv_CMB2_RC); TmThreadRemove(tv_CMB2_RC, tv_CMB2_RC->type); - free(tv_CMB2_RC); + SCFree(tv_CMB2_RC); tv_CMB2_RC = NULL; return; @@ -2654,7 +2654,7 @@ void B2gCudaKillDispatcherThreadAPC(void) TmThreadKillThread(tv_CMB2_APC); TmThreadRemove(tv_CMB2_APC, tv_CMB2_APC->type); - free(tv_CMB2_APC); + SCFree(tv_CMB2_APC); tv_CMB2_APC = NULL; return; diff --git a/src/util-mpm-b2g.c b/src/util-mpm-b2g.c index 8e97babef2..ddb6506593 100644 --- a/src/util-mpm-b2g.c +++ b/src/util-mpm-b2g.c @@ -161,9 +161,9 @@ void B2gPrintInfo(MpmCtx *mpm_ctx) { } static inline B2gPattern *B2gAllocPattern(MpmCtx *mpm_ctx) { - B2gPattern *p = malloc(sizeof(B2gPattern)); + B2gPattern *p = SCMalloc(sizeof(B2gPattern)); if (p == NULL) { - printf("ERROR: B2gAllocPattern: malloc failed\n"); + printf("ERROR: B2gAllocPattern: SCMalloc failed\n"); exit(EXIT_FAILURE); } memset(p,0,sizeof(B2gPattern)); @@ -175,9 +175,9 @@ static inline B2gPattern *B2gAllocPattern(MpmCtx *mpm_ctx) { static inline B2gHashItem * B2gAllocHashItem(MpmCtx *mpm_ctx) { - B2gHashItem *hi = malloc(sizeof(B2gHashItem)); + B2gHashItem *hi = SCMalloc(sizeof(B2gHashItem)); if (hi == NULL) { - printf("ERROR: B2gAllocHashItem: malloc failed\n"); + printf("ERROR: B2gAllocHashItem: SCMalloc failed\n"); exit(EXIT_FAILURE); } memset(hi,0,sizeof(B2gHashItem)); @@ -196,7 +196,7 @@ static void B2gHashFree(MpmCtx *mpm_ctx, B2gHashItem *hi) { mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= sizeof(B2gHashItem); - free(hi); + SCFree(hi); } static inline void memcpy_tolower(uint8_t *d, uint8_t *s, uint16_t len) { @@ -294,19 +294,19 @@ void B2gFreePattern(MpmCtx *mpm_ctx, B2gPattern *p) { } if (p && p->cs && p->cs != p->ci) { - free(p->cs); + SCFree(p->cs); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= p->len; } if (p && p->ci) { - free(p->ci); + SCFree(p->ci); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= p->len; } if (p) { - free(p); + SCFree(p); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= sizeof(B2gPattern); } @@ -342,7 +342,7 @@ static inline int B2gAddPattern(MpmCtx *mpm_ctx, uint8_t *pat, uint16_t patlen, if (nocase) p->flags |= B2G_NOCASE; /* setup the case insensitive part of the pattern */ - p->ci = malloc(patlen); + p->ci = SCMalloc(patlen); if (p->ci == NULL) goto error; mpm_ctx->memory_cnt++; mpm_ctx->memory_size += patlen; @@ -357,7 +357,7 @@ static inline int B2gAddPattern(MpmCtx *mpm_ctx, uint8_t *pat, uint16_t patlen, /* no diff between cs and ci: pat is lowercase */ p->cs = p->ci; } else { - p->cs = malloc(patlen); + p->cs = SCMalloc(patlen); if (p->cs == NULL) goto error; mpm_ctx->memory_cnt++; mpm_ctx->memory_size += patlen; @@ -463,7 +463,7 @@ static void B2gPrepareScanHash(MpmCtx *mpm_ctx) { uint16_t idx = 0; uint8_t idx8 = 0; - ctx->scan_hash = (B2gHashItem **)malloc(sizeof(B2gHashItem *) * ctx->scan_hash_size); + ctx->scan_hash = (B2gHashItem **)SCMalloc(sizeof(B2gHashItem *) * ctx->scan_hash_size); if (ctx->scan_hash == NULL) goto error; memset(ctx->scan_hash, 0, sizeof(B2gHashItem *) * ctx->scan_hash_size); @@ -471,7 +471,7 @@ static void B2gPrepareScanHash(MpmCtx *mpm_ctx) { mpm_ctx->memory_size += (sizeof(B2gHashItem *) * ctx->scan_hash_size); #ifdef B2G_SCAN2 - ctx->scan_hash2 = (B2gHashItem **)malloc(sizeof(B2gHashItem *) * ctx->scan_hash_size); + ctx->scan_hash2 = (B2gHashItem **)SCMalloc(sizeof(B2gHashItem *) * ctx->scan_hash_size); if (ctx->scan_hash2 == NULL) goto error; memset(ctx->scan_hash2, 0, sizeof(B2gHashItem *) * ctx->scan_hash_size); @@ -480,7 +480,7 @@ static void B2gPrepareScanHash(MpmCtx *mpm_ctx) { #endif /* alloc the pminlen array */ - ctx->scan_pminlen = (uint8_t *)malloc(sizeof(uint8_t) * ctx->scan_hash_size); + ctx->scan_pminlen = (uint8_t *)SCMalloc(sizeof(uint8_t) * ctx->scan_hash_size); if (ctx->scan_pminlen == NULL) goto error; memset(ctx->scan_pminlen, 0, sizeof(uint8_t) * ctx->scan_hash_size); @@ -559,7 +559,7 @@ static void B2gPrepareScanHash(MpmCtx *mpm_ctx) { } /* alloc the bloom array */ - ctx->scan_bloom = (BloomFilter **)malloc(sizeof(BloomFilter *) * ctx->scan_hash_size); + ctx->scan_bloom = (BloomFilter **)SCMalloc(sizeof(BloomFilter *) * ctx->scan_hash_size); if (ctx->scan_bloom == NULL) goto error; memset(ctx->scan_bloom, 0, sizeof(BloomFilter *) * ctx->scan_hash_size); @@ -601,7 +601,7 @@ static void B2gPrepareSearchHash(MpmCtx *mpm_ctx) { uint16_t idx = 0; uint8_t idx8 = 0; - ctx->search_hash = (B2gHashItem **)malloc(sizeof(B2gHashItem *) * ctx->search_hash_size); + ctx->search_hash = (B2gHashItem **)SCMalloc(sizeof(B2gHashItem *) * ctx->search_hash_size); if (ctx->search_hash == NULL) goto error; memset(ctx->search_hash, 0, sizeof(B2gHashItem *) * ctx->search_hash_size); @@ -609,7 +609,7 @@ static void B2gPrepareSearchHash(MpmCtx *mpm_ctx) { mpm_ctx->memory_size += (sizeof(B2gHashItem *) * ctx->search_hash_size); /* alloc the pminlen array */ - ctx->search_pminlen = (uint8_t *)malloc(sizeof(uint8_t) * ctx->search_hash_size); + ctx->search_pminlen = (uint8_t *)SCMalloc(sizeof(uint8_t) * ctx->search_hash_size); if (ctx->search_pminlen == NULL) goto error; memset(ctx->search_pminlen, 0, sizeof(uint8_t) * ctx->search_hash_size); @@ -665,7 +665,7 @@ static void B2gPrepareSearchHash(MpmCtx *mpm_ctx) { } /* alloc the bloom array */ - ctx->search_bloom = (BloomFilter **)malloc(sizeof(BloomFilter *) * ctx->search_hash_size); + ctx->search_bloom = (BloomFilter **)SCMalloc(sizeof(BloomFilter *) * ctx->search_hash_size); if (ctx->search_bloom == NULL) goto error; memset(ctx->search_bloom, 0, sizeof(BloomFilter *) * ctx->search_hash_size); @@ -703,7 +703,7 @@ int B2gBuildScanMatchArray(MpmCtx *mpm_ctx) { SCEnter(); B2gCtx *ctx = (B2gCtx *)mpm_ctx->ctx; - ctx->scan_B2G = malloc(sizeof(B2G_TYPE) * ctx->scan_hash_size); + ctx->scan_B2G = SCMalloc(sizeof(B2G_TYPE) * ctx->scan_hash_size); if (ctx->scan_B2G == NULL) return -1; @@ -738,7 +738,7 @@ int B2gBuildScanMatchArray(MpmCtx *mpm_ctx) { int B2gBuildSearchMatchArray(MpmCtx *mpm_ctx) { B2gCtx *ctx = (B2gCtx *)mpm_ctx->ctx; - ctx->search_B2G = malloc(sizeof(B2G_TYPE) * ctx->search_hash_size); + ctx->search_B2G = SCMalloc(sizeof(B2G_TYPE) * ctx->search_hash_size); if (ctx->search_B2G == NULL) return -1; @@ -806,7 +806,7 @@ int B2gPreparePatterns(MpmCtx *mpm_ctx) { B2gCtx *ctx = (B2gCtx *)mpm_ctx->ctx; /* alloc the pattern array */ - ctx->parray = (B2gPattern **)malloc(mpm_ctx->pattern_cnt * sizeof(B2gPattern *)); + ctx->parray = (B2gPattern **)SCMalloc(mpm_ctx->pattern_cnt * sizeof(B2gPattern *)); if (ctx->parray == NULL) goto error; memset(ctx->parray, 0, mpm_ctx->pattern_cnt * sizeof(B2gPattern *)); //printf("mpm_ctx %p, parray %p\n", mpm_ctx,ctx->parray); @@ -828,7 +828,7 @@ int B2gPreparePatterns(MpmCtx *mpm_ctx) { } } /* we no longer need the hash, so free it's memory */ - free(ctx->init_hash); + SCFree(ctx->init_hash); ctx->init_hash = NULL; /* set 'm' to the smallest pattern size */ @@ -992,7 +992,7 @@ void B2gInitCtx (MpmCtx *mpm_ctx, int module_handle) { BUG_ON(mpm_ctx->ctx != NULL); - mpm_ctx->ctx = malloc(sizeof(B2gCtx)); + mpm_ctx->ctx = SCMalloc(sizeof(B2gCtx)); if (mpm_ctx->ctx == NULL) return; @@ -1003,7 +1003,7 @@ void B2gInitCtx (MpmCtx *mpm_ctx, int module_handle) { /* initialize the hash we use to speed up pattern insertions */ B2gCtx *ctx = (B2gCtx *)mpm_ctx->ctx; - ctx->init_hash = malloc(sizeof(B2gPattern *) * INIT_HASH_SIZE); + ctx->init_hash = SCMalloc(sizeof(B2gPattern *) * INIT_HASH_SIZE); if (ctx->init_hash == NULL) return; @@ -1029,7 +1029,7 @@ void B2gDestroyCtx(MpmCtx *mpm_ctx) { return; if (ctx->init_hash) { - free(ctx->init_hash); + SCFree(ctx->init_hash); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (INIT_HASH_SIZE * sizeof(B2gPattern *)); } @@ -1042,19 +1042,19 @@ void B2gDestroyCtx(MpmCtx *mpm_ctx) { } } - free(ctx->parray); + SCFree(ctx->parray); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (mpm_ctx->pattern_cnt * sizeof(B2gPattern)); } if (ctx->scan_B2G) { - free(ctx->scan_B2G); + SCFree(ctx->scan_B2G); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(B2G_TYPE) * ctx->scan_hash_size); } if (ctx->search_B2G) { - free(ctx->search_B2G); + SCFree(ctx->search_B2G); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(B2G_TYPE) * ctx->search_hash_size); } @@ -1071,7 +1071,7 @@ void B2gDestroyCtx(MpmCtx *mpm_ctx) { BloomFilterFree(ctx->scan_bloom[h]); } - free(ctx->scan_bloom); + SCFree(ctx->scan_bloom); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(BloomFilter *) * ctx->scan_hash_size); @@ -1086,7 +1086,7 @@ void B2gDestroyCtx(MpmCtx *mpm_ctx) { B2gHashFree(mpm_ctx, ctx->scan_hash[h]); } - free(ctx->scan_hash); + SCFree(ctx->scan_hash); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(B2gHashItem) * ctx->scan_hash_size); } @@ -1103,7 +1103,7 @@ void B2gDestroyCtx(MpmCtx *mpm_ctx) { BloomFilterFree(ctx->search_bloom[h]); } - free(ctx->search_bloom); + SCFree(ctx->search_bloom); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(BloomFilter *) * ctx->search_hash_size); @@ -1118,24 +1118,24 @@ void B2gDestroyCtx(MpmCtx *mpm_ctx) { B2gHashFree(mpm_ctx, ctx->search_hash[h]); } - free(ctx->search_hash); + SCFree(ctx->search_hash); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(B2gHashItem) * ctx->search_hash_size); } if (ctx->scan_pminlen) { - free(ctx->scan_pminlen); + SCFree(ctx->scan_pminlen); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(uint8_t) * ctx->scan_hash_size); } if (ctx->search_pminlen) { - free(ctx->search_pminlen); + SCFree(ctx->search_pminlen); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(uint8_t) * ctx->search_hash_size); } - free(mpm_ctx->ctx); + SCFree(mpm_ctx->ctx); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= sizeof(B2gCtx); } @@ -1144,7 +1144,7 @@ void B2gThreadInitCtx(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, uint32_t ma memset(mpm_thread_ctx, 0, sizeof(MpmThreadCtx)); if (sizeof(B2gThreadCtx) > 0) { /* size can be null when optimized */ - mpm_thread_ctx->ctx = malloc(sizeof(B2gThreadCtx)); + mpm_thread_ctx->ctx = SCMalloc(sizeof(B2gThreadCtx)); if (mpm_thread_ctx->ctx == NULL) return; @@ -1160,7 +1160,7 @@ void B2gThreadInitCtx(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, uint32_t ma * unique id and is the array lookup key at the same time */ uint32_t keys = matchsize + 1; if (keys > 0) { - mpm_thread_ctx->match = malloc(keys * sizeof(MpmMatchBucket)); + mpm_thread_ctx->match = SCMalloc(keys * sizeof(MpmMatchBucket)); if (mpm_thread_ctx->match == NULL) { printf("ERROR: could not setup memory for pattern matcher: %s\n", strerror(errno)); exit(1); @@ -1182,13 +1182,13 @@ void B2gThreadDestroyCtx(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx) { if (ctx != NULL) { /* can be NULL if B2gThreadCtx is optimized to 0 */ mpm_thread_ctx->memory_cnt--; mpm_thread_ctx->memory_size -= sizeof(B2gThreadCtx); - free(mpm_thread_ctx->ctx); + SCFree(mpm_thread_ctx->ctx); } if (mpm_thread_ctx->match != NULL) { mpm_thread_ctx->memory_cnt--; mpm_thread_ctx->memory_size -= ((mpm_thread_ctx->matchsize + 1) * sizeof(MpmMatchBucket)); - free(mpm_thread_ctx->match); + SCFree(mpm_thread_ctx->match); } MpmMatchFreeSpares(mpm_thread_ctx, mpm_thread_ctx->sparelist); diff --git a/src/util-mpm-b3g.c b/src/util-mpm-b3g.c index 397aed6162..2d532d45bd 100644 --- a/src/util-mpm-b3g.c +++ b/src/util-mpm-b3g.c @@ -146,9 +146,9 @@ void B3gPrintInfo(MpmCtx *mpm_ctx) { } static inline B3gPattern *B3gAllocPattern(MpmCtx *mpm_ctx) { - B3gPattern *p = malloc(sizeof(B3gPattern)); + B3gPattern *p = SCMalloc(sizeof(B3gPattern)); if (p == NULL) { - printf("ERROR: B3gAllocPattern: malloc failed\n"); + printf("ERROR: B3gAllocPattern: SCMalloc failed\n"); exit(EXIT_FAILURE); } memset(p,0,sizeof(B3gPattern)); @@ -160,9 +160,9 @@ static inline B3gPattern *B3gAllocPattern(MpmCtx *mpm_ctx) { static inline B3gHashItem * B3gAllocHashItem(MpmCtx *mpm_ctx) { - B3gHashItem *hi = malloc(sizeof(B3gHashItem)); + B3gHashItem *hi = SCMalloc(sizeof(B3gHashItem)); if (hi == NULL) { - printf("ERROR: B3gAllocHashItem: malloc failed\n"); + printf("ERROR: B3gAllocHashItem: SCMalloc failed\n"); exit(EXIT_FAILURE); } memset(hi,0,sizeof(B3gHashItem)); @@ -181,7 +181,7 @@ static void B3gHashFree(MpmCtx *mpm_ctx, B3gHashItem *hi) { mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= sizeof(B3gHashItem); - free(hi); + SCFree(hi); } static inline void memcpy_tolower(uint8_t *d, uint8_t *s, uint16_t len) { @@ -279,19 +279,19 @@ void B3gFreePattern(MpmCtx *mpm_ctx, B3gPattern *p) { } if (p && p->cs && p->cs != p->ci) { - free(p->cs); + SCFree(p->cs); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= p->len; } if (p && p->ci) { - free(p->ci); + SCFree(p->ci); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= p->len; } if (p) { - free(p); + SCFree(p); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= sizeof(B3gPattern); } @@ -327,7 +327,7 @@ static inline int B3gAddPattern(MpmCtx *mpm_ctx, uint8_t *pat, uint16_t patlen, if (nocase) p->flags |= B3G_NOCASE; /* setup the case insensitive part of the pattern */ - p->ci = malloc(patlen); + p->ci = SCMalloc(patlen); if (p->ci == NULL) goto error; mpm_ctx->memory_cnt++; mpm_ctx->memory_size += patlen; @@ -342,7 +342,7 @@ static inline int B3gAddPattern(MpmCtx *mpm_ctx, uint8_t *pat, uint16_t patlen, /* no diff between cs and ci: pat is lowercase */ p->cs = p->ci; } else { - p->cs = malloc(patlen); + p->cs = SCMalloc(patlen); if (p->cs == NULL) goto error; mpm_ctx->memory_cnt++; mpm_ctx->memory_size += patlen; @@ -443,7 +443,7 @@ static void B3gPrepareScanHash(MpmCtx *mpm_ctx) { uint16_t idx = 0; uint8_t idx8 = 0; - ctx->scan_hash = (B3gHashItem **)malloc(sizeof(B3gHashItem *) * ctx->scan_hash_size); + ctx->scan_hash = (B3gHashItem **)SCMalloc(sizeof(B3gHashItem *) * ctx->scan_hash_size); if (ctx->scan_hash == NULL) goto error; memset(ctx->scan_hash, 0, sizeof(B3gHashItem *) * ctx->scan_hash_size); @@ -451,7 +451,7 @@ static void B3gPrepareScanHash(MpmCtx *mpm_ctx) { mpm_ctx->memory_size += (sizeof(B3gHashItem *) * ctx->scan_hash_size); /* 2 byte pattern hash */ - ctx->scan_hash2 = (B3gHashItem **)malloc(sizeof(B3gHashItem *) * ctx->scan_hash_size); + ctx->scan_hash2 = (B3gHashItem **)SCMalloc(sizeof(B3gHashItem *) * ctx->scan_hash_size); if (ctx->scan_hash2 == NULL) goto error; memset(ctx->scan_hash2, 0, sizeof(B3gHashItem *) * ctx->scan_hash_size); @@ -459,7 +459,7 @@ static void B3gPrepareScanHash(MpmCtx *mpm_ctx) { mpm_ctx->memory_size += (sizeof(B3gHashItem *) * ctx->scan_hash_size); /* alloc the pminlen array */ - ctx->scan_pminlen = (uint8_t *)malloc(sizeof(uint8_t) * ctx->scan_hash_size); + ctx->scan_pminlen = (uint8_t *)SCMalloc(sizeof(uint8_t) * ctx->scan_hash_size); if (ctx->scan_pminlen == NULL) goto error; memset(ctx->scan_pminlen, 0, sizeof(uint8_t) * ctx->scan_hash_size); @@ -536,7 +536,7 @@ static void B3gPrepareScanHash(MpmCtx *mpm_ctx) { } /* alloc the bloom array */ - ctx->scan_bloom = (BloomFilter **)malloc(sizeof(BloomFilter *) * ctx->scan_hash_size); + ctx->scan_bloom = (BloomFilter **)SCMalloc(sizeof(BloomFilter *) * ctx->scan_hash_size); if (ctx->scan_bloom == NULL) goto error; memset(ctx->scan_bloom, 0, sizeof(BloomFilter *) * ctx->scan_hash_size); @@ -577,7 +577,7 @@ static void B3gPrepareSearchHash(MpmCtx *mpm_ctx) { uint16_t idx = 0; uint8_t idx8 = 0; - ctx->search_hash = (B3gHashItem **)malloc(sizeof(B3gHashItem *) * ctx->search_hash_size); + ctx->search_hash = (B3gHashItem **)SCMalloc(sizeof(B3gHashItem *) * ctx->search_hash_size); if (ctx->search_hash == NULL) goto error; memset(ctx->search_hash, 0, sizeof(B3gHashItem *) * ctx->search_hash_size); @@ -585,7 +585,7 @@ static void B3gPrepareSearchHash(MpmCtx *mpm_ctx) { mpm_ctx->memory_size += (sizeof(B3gHashItem *) * ctx->search_hash_size); /* 2 byte pattern hash */ - ctx->search_hash2 = (B3gHashItem **)malloc(sizeof(B3gHashItem *) * ctx->search_hash_size); + ctx->search_hash2 = (B3gHashItem **)SCMalloc(sizeof(B3gHashItem *) * ctx->search_hash_size); if (ctx->search_hash2 == NULL) goto error; memset(ctx->search_hash2, 0, sizeof(B3gHashItem *) * ctx->search_hash_size); @@ -593,7 +593,7 @@ static void B3gPrepareSearchHash(MpmCtx *mpm_ctx) { mpm_ctx->memory_size += (sizeof(B3gHashItem *) * ctx->scan_hash_size); /* alloc the pminlen array */ - ctx->search_pminlen = (uint8_t *)malloc(sizeof(uint8_t) * ctx->search_hash_size); + ctx->search_pminlen = (uint8_t *)SCMalloc(sizeof(uint8_t) * ctx->search_hash_size); if (ctx->search_pminlen == NULL) goto error; memset(ctx->search_pminlen, 0, sizeof(uint8_t) * ctx->search_hash_size); @@ -670,7 +670,7 @@ static void B3gPrepareSearchHash(MpmCtx *mpm_ctx) { } /* alloc the bloom array */ - ctx->search_bloom = (BloomFilter **)malloc(sizeof(BloomFilter *) * ctx->search_hash_size); + ctx->search_bloom = (BloomFilter **)SCMalloc(sizeof(BloomFilter *) * ctx->search_hash_size); if (ctx->search_bloom == NULL) goto error; memset(ctx->search_bloom, 0, sizeof(BloomFilter *) * ctx->search_hash_size); @@ -708,7 +708,7 @@ error: int B3gBuildScanMatchArray(MpmCtx *mpm_ctx) { B3gCtx *ctx = (B3gCtx *)mpm_ctx->ctx; - ctx->scan_B3G = malloc(sizeof(B3G_TYPE) * ctx->scan_hash_size); + ctx->scan_B3G = SCMalloc(sizeof(B3G_TYPE) * ctx->scan_hash_size); if (ctx->scan_B3G == NULL) return -1; @@ -742,7 +742,7 @@ int B3gBuildScanMatchArray(MpmCtx *mpm_ctx) { int B3gBuildSearchMatchArray(MpmCtx *mpm_ctx) { B3gCtx *ctx = (B3gCtx *)mpm_ctx->ctx; - ctx->search_B3G = malloc(sizeof(B3G_TYPE) * ctx->search_hash_size); + ctx->search_B3G = SCMalloc(sizeof(B3G_TYPE) * ctx->search_hash_size); if (ctx->search_B3G == NULL) return -1; @@ -810,7 +810,7 @@ int B3gPreparePatterns(MpmCtx *mpm_ctx) { B3gCtx *ctx = (B3gCtx *)mpm_ctx->ctx; /* alloc the pattern array */ - ctx->parray = (B3gPattern **)malloc(mpm_ctx->pattern_cnt * sizeof(B3gPattern *)); + ctx->parray = (B3gPattern **)SCMalloc(mpm_ctx->pattern_cnt * sizeof(B3gPattern *)); if (ctx->parray == NULL) goto error; memset(ctx->parray, 0, mpm_ctx->pattern_cnt * sizeof(B3gPattern *)); //printf("mpm_ctx %p, parray %p\n", mpm_ctx,ctx->parray); @@ -832,7 +832,7 @@ int B3gPreparePatterns(MpmCtx *mpm_ctx) { } } /* we no longer need the hash, so free it's memory */ - free(ctx->init_hash); + SCFree(ctx->init_hash); ctx->init_hash = NULL; /* set 'm' to the smallest pattern size */ @@ -996,7 +996,7 @@ void B3gGetConfig() void B3gInitCtx (MpmCtx *mpm_ctx, int module_handle) { //printf("B3gInitCtx: mpm_ctx %p\n", mpm_ctx); - mpm_ctx->ctx = malloc(sizeof(B3gCtx)); + mpm_ctx->ctx = SCMalloc(sizeof(B3gCtx)); if (mpm_ctx->ctx == NULL) return; @@ -1007,7 +1007,7 @@ void B3gInitCtx (MpmCtx *mpm_ctx, int module_handle) { /* initialize the hash we use to speed up pattern insertions */ B3gCtx *ctx = (B3gCtx *)mpm_ctx->ctx; - ctx->init_hash = malloc(sizeof(B3gPattern *) * INIT_HASH_SIZE); + ctx->init_hash = SCMalloc(sizeof(B3gPattern *) * INIT_HASH_SIZE); if (ctx->init_hash == NULL) return; @@ -1029,7 +1029,7 @@ void B3gDestroyCtx(MpmCtx *mpm_ctx) { return; if (ctx->init_hash) { - free(ctx->init_hash); + SCFree(ctx->init_hash); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (INIT_HASH_SIZE * sizeof(B3gPattern *)); } @@ -1042,19 +1042,19 @@ void B3gDestroyCtx(MpmCtx *mpm_ctx) { } } - free(ctx->parray); + SCFree(ctx->parray); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (mpm_ctx->pattern_cnt * sizeof(B3gPattern)); } if (ctx->scan_B3G) { - free(ctx->scan_B3G); + SCFree(ctx->scan_B3G); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(B3G_TYPE) * ctx->scan_hash_size); } if (ctx->search_B3G) { - free(ctx->search_B3G); + SCFree(ctx->search_B3G); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(B3G_TYPE) * ctx->search_hash_size); } @@ -1071,7 +1071,7 @@ void B3gDestroyCtx(MpmCtx *mpm_ctx) { BloomFilterFree(ctx->scan_bloom[h]); } - free(ctx->scan_bloom); + SCFree(ctx->scan_bloom); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(BloomFilter *) * ctx->scan_hash_size); @@ -1086,7 +1086,7 @@ void B3gDestroyCtx(MpmCtx *mpm_ctx) { B3gHashFree(mpm_ctx, ctx->scan_hash[h]); } - free(ctx->scan_hash); + SCFree(ctx->scan_hash); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(B3gHashItem) * ctx->scan_hash_size); } @@ -1099,7 +1099,7 @@ void B3gDestroyCtx(MpmCtx *mpm_ctx) { B3gHashFree(mpm_ctx, ctx->scan_hash2[h]); } - free(ctx->scan_hash2); + SCFree(ctx->scan_hash2); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(B3gHashItem) * ctx->scan_hash_size); } @@ -1116,7 +1116,7 @@ void B3gDestroyCtx(MpmCtx *mpm_ctx) { BloomFilterFree(ctx->search_bloom[h]); } - free(ctx->search_bloom); + SCFree(ctx->search_bloom); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(BloomFilter *) * ctx->search_hash_size); @@ -1131,7 +1131,7 @@ void B3gDestroyCtx(MpmCtx *mpm_ctx) { B3gHashFree(mpm_ctx, ctx->search_hash[h]); } - free(ctx->search_hash); + SCFree(ctx->search_hash); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(B3gHashItem) * ctx->search_hash_size); } @@ -1144,24 +1144,24 @@ void B3gDestroyCtx(MpmCtx *mpm_ctx) { B3gHashFree(mpm_ctx, ctx->search_hash2[h]); } - free(ctx->search_hash2); + SCFree(ctx->search_hash2); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(B3gHashItem) * ctx->search_hash_size); } if (ctx->scan_pminlen) { - free(ctx->scan_pminlen); + SCFree(ctx->scan_pminlen); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(uint8_t) * ctx->scan_hash_size); } if (ctx->search_pminlen) { - free(ctx->search_pminlen); + SCFree(ctx->search_pminlen); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(uint8_t) * ctx->search_hash_size); } - free(mpm_ctx->ctx); + SCFree(mpm_ctx->ctx); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= sizeof(B3gCtx); } @@ -1170,7 +1170,7 @@ void B3gThreadInitCtx(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, uint32_t ma memset(mpm_thread_ctx, 0, sizeof(MpmThreadCtx)); if (sizeof(B3gThreadCtx) > 0) { /* size can be 0 when optimized */ - mpm_thread_ctx->ctx = malloc(sizeof(B3gThreadCtx)); + mpm_thread_ctx->ctx = SCMalloc(sizeof(B3gThreadCtx)); if (mpm_thread_ctx->ctx == NULL) return; @@ -1186,7 +1186,7 @@ void B3gThreadInitCtx(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, uint32_t ma * unique id and is the array lookup key at the same time */ uint32_t keys = matchsize + 1; if (keys > 0) { - mpm_thread_ctx->match = malloc(keys * sizeof(MpmMatchBucket)); + mpm_thread_ctx->match = SCMalloc(keys * sizeof(MpmMatchBucket)); if (mpm_thread_ctx->match == NULL) { printf("ERROR: could not setup memory for pattern matcher: %s\n", strerror(errno)); exit(1); @@ -1208,13 +1208,13 @@ void B3gThreadDestroyCtx(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx) { if (ctx != NULL) { /* can be NULL when optimized */ mpm_thread_ctx->memory_cnt--; mpm_thread_ctx->memory_size -= sizeof(B3gThreadCtx); - free(mpm_thread_ctx->ctx); + SCFree(mpm_thread_ctx->ctx); } if (mpm_thread_ctx->match != NULL) { mpm_thread_ctx->memory_cnt--; mpm_thread_ctx->memory_size -= ((mpm_thread_ctx->matchsize + 1) * sizeof(MpmMatchBucket)); - free(mpm_thread_ctx->match); + SCFree(mpm_thread_ctx->match); } MpmMatchFreeSpares(mpm_thread_ctx, mpm_thread_ctx->sparelist); diff --git a/src/util-mpm-wumanber.c b/src/util-mpm-wumanber.c index 88b24d420e..b735bdcd66 100644 --- a/src/util-mpm-wumanber.c +++ b/src/util-mpm-wumanber.c @@ -209,9 +209,9 @@ void WmPrintInfo(MpmCtx *mpm_ctx) { } static inline WmPattern *WmAllocPattern(MpmCtx *mpm_ctx) { - WmPattern *p = malloc(sizeof(WmPattern)); + WmPattern *p = SCMalloc(sizeof(WmPattern)); if (p == NULL) { - printf("ERROR: WmAllocPattern: malloc failed\n"); + printf("ERROR: WmAllocPattern: SCMalloc failed\n"); exit(EXIT_FAILURE); } memset(p,0,sizeof(WmPattern)); @@ -223,9 +223,9 @@ static inline WmPattern *WmAllocPattern(MpmCtx *mpm_ctx) { static inline WmHashItem * WmAllocHashItem(MpmCtx *mpm_ctx) { - WmHashItem *hi = malloc(sizeof(WmHashItem)); + WmHashItem *hi = SCMalloc(sizeof(WmHashItem)); if (hi == NULL) { - printf("ERROR: WmAllocHashItem: malloc failed\n"); + printf("ERROR: WmAllocHashItem: SCMalloc failed\n"); exit(EXIT_FAILURE); } memset(hi,0,sizeof(WmHashItem)); @@ -244,7 +244,7 @@ static void WmHashFree(MpmCtx *mpm_ctx, WmHashItem *hi) { mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= sizeof(WmHashItem); - free(hi); + SCFree(hi); } static inline void memcpy_tolower(uint8_t *d, uint8_t *s, uint16_t len) { @@ -342,19 +342,19 @@ void WmFreePattern(MpmCtx *mpm_ctx, WmPattern *p) { } if (p && p->cs && p->cs != p->ci) { - free(p->cs); + SCFree(p->cs); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= p->len; } if (p && p->ci) { - free(p->ci); + SCFree(p->ci); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= p->len; } if (p) { - free(p); + SCFree(p); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= sizeof(WmPattern); } @@ -390,7 +390,7 @@ static inline int WmAddPattern(MpmCtx *mpm_ctx, uint8_t *pat, uint16_t patlen, u if (nocase) p->flags |= WUMANBER_NOCASE; /* setup the case insensitive part of the pattern */ - p->ci = malloc(patlen); + p->ci = SCMalloc(patlen); if (p->ci == NULL) goto error; mpm_ctx->memory_cnt++; mpm_ctx->memory_size += patlen; @@ -405,7 +405,7 @@ static inline int WmAddPattern(MpmCtx *mpm_ctx, uint8_t *pat, uint16_t patlen, u /* no diff between cs and ci: pat is lowercase */ p->cs = p->ci; } else { - p->cs = malloc(patlen); + p->cs = SCMalloc(patlen); if (p->cs == NULL) goto error; mpm_ctx->memory_cnt++; mpm_ctx->memory_size += patlen; @@ -524,7 +524,7 @@ static void WmScanPrepareHash(MpmCtx *mpm_ctx) { uint16_t idx = 0; uint8_t idx8 = 0; - ctx->scan_hash = (WmHashItem **)malloc(sizeof(WmHashItem *) * ctx->scan_hash_size); + ctx->scan_hash = (WmHashItem **)SCMalloc(sizeof(WmHashItem *) * ctx->scan_hash_size); if (ctx->scan_hash == NULL) goto error; memset(ctx->scan_hash, 0, sizeof(WmHashItem *) * ctx->scan_hash_size); @@ -532,7 +532,7 @@ static void WmScanPrepareHash(MpmCtx *mpm_ctx) { mpm_ctx->memory_size += (sizeof(WmHashItem *) * ctx->scan_hash_size); /* alloc the pminlen array */ - ctx->scan_pminlen = (uint8_t *)malloc(sizeof(uint8_t) * ctx->scan_hash_size); + ctx->scan_pminlen = (uint8_t *)SCMalloc(sizeof(uint8_t) * ctx->scan_hash_size); if (ctx->scan_pminlen == NULL) goto error; memset(ctx->scan_pminlen, 0, sizeof(uint8_t) * ctx->scan_hash_size); @@ -595,7 +595,7 @@ static void WmScanPrepareHash(MpmCtx *mpm_ctx) { } /* alloc the bloom array */ - ctx->scan_bloom = (BloomFilter **)malloc(sizeof(BloomFilter *) * ctx->scan_hash_size); + ctx->scan_bloom = (BloomFilter **)SCMalloc(sizeof(BloomFilter *) * ctx->scan_hash_size); if (ctx->scan_bloom == NULL) goto error; memset(ctx->scan_bloom, 0, sizeof(BloomFilter *) * ctx->scan_hash_size); @@ -634,7 +634,7 @@ static void WmPrepareHash(MpmCtx *mpm_ctx) { uint16_t idx = 0; uint8_t idx8 = 0; - ctx->search_hash = (WmHashItem **)malloc(sizeof(WmHashItem *) * ctx->search_hash_size); + ctx->search_hash = (WmHashItem **)SCMalloc(sizeof(WmHashItem *) * ctx->search_hash_size); if (ctx->search_hash == NULL) goto error; memset(ctx->search_hash, 0, sizeof(WmHashItem *) * ctx->search_hash_size); @@ -712,7 +712,7 @@ static void WmScanPrepareShiftTable(MpmCtx *mpm_ctx) ctx->scan_shiftlen = smallest; - ctx->scan_shifttable = malloc(sizeof(uint16_t) * ctx->scan_hash_size); + ctx->scan_shifttable = SCMalloc(sizeof(uint16_t) * ctx->scan_hash_size); if (ctx->scan_shifttable == NULL) return; @@ -806,7 +806,7 @@ static void WmPrepareShiftTable(MpmCtx *mpm_ctx) ctx->search_shiftlen = smallest; - ctx->search_shifttable = malloc(sizeof(uint16_t) * ctx->search_hash_size); + ctx->search_shifttable = SCMalloc(sizeof(uint16_t) * ctx->search_hash_size); if (ctx->search_shifttable == NULL) return; @@ -891,7 +891,7 @@ int WmPreparePatterns(MpmCtx *mpm_ctx) { WmCtx *ctx = (WmCtx *)mpm_ctx->ctx; /* alloc the pattern array */ - ctx->parray = (WmPattern **)malloc(mpm_ctx->pattern_cnt * sizeof(WmPattern *)); + ctx->parray = (WmPattern **)SCMalloc(mpm_ctx->pattern_cnt * sizeof(WmPattern *)); if (ctx->parray == NULL) goto error; memset(ctx->parray, 0, mpm_ctx->pattern_cnt * sizeof(WmPattern *)); //printf("mpm_ctx %p, parray %p\n", mpm_ctx,ctx->parray); @@ -913,7 +913,7 @@ int WmPreparePatterns(MpmCtx *mpm_ctx) { } } /* we no longer need the hash, so free it's memory */ - free(ctx->init_hash); + SCFree(ctx->init_hash); ctx->init_hash = NULL; /* TODO VJ these values are chosen pretty much randomly, so @@ -2233,7 +2233,7 @@ void WmGetConfig() void WmInitCtx (MpmCtx *mpm_ctx, int module_handle) { SCLogDebug("mpm_ctx %p", mpm_ctx); - mpm_ctx->ctx = malloc(sizeof(WmCtx)); + mpm_ctx->ctx = SCMalloc(sizeof(WmCtx)); if (mpm_ctx->ctx == NULL) return; @@ -2244,7 +2244,7 @@ void WmInitCtx (MpmCtx *mpm_ctx, int module_handle) { /* initialize the hash we use to speed up pattern insertions */ WmCtx *ctx = (WmCtx *)mpm_ctx->ctx; - ctx->init_hash = malloc(sizeof(WmPattern *) * INIT_HASH_SIZE); + ctx->init_hash = SCMalloc(sizeof(WmPattern *) * INIT_HASH_SIZE); if (ctx->init_hash == NULL) return; @@ -2263,7 +2263,7 @@ void WmDestroyCtx(MpmCtx *mpm_ctx) { return; if (ctx->init_hash) { - free(ctx->init_hash); + SCFree(ctx->init_hash); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (INIT_HASH_SIZE * sizeof(WmPattern *)); } @@ -2276,7 +2276,7 @@ void WmDestroyCtx(MpmCtx *mpm_ctx) { } } - free(ctx->parray); + SCFree(ctx->parray); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (mpm_ctx->pattern_cnt * sizeof(WmPattern)); } @@ -2293,7 +2293,7 @@ void WmDestroyCtx(MpmCtx *mpm_ctx) { BloomFilterFree(ctx->scan_bloom[h]); } - free(ctx->scan_bloom); + SCFree(ctx->scan_bloom); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(BloomFilter *) * ctx->scan_hash_size); @@ -2308,13 +2308,13 @@ void WmDestroyCtx(MpmCtx *mpm_ctx) { WmHashFree(mpm_ctx, ctx->scan_hash[h]); } - free(ctx->scan_hash); + SCFree(ctx->scan_hash); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(WmHashItem) * ctx->scan_hash_size); } if (ctx->scan_shifttable) { - free(ctx->scan_shifttable); + SCFree(ctx->scan_shifttable); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(uint16_t) * ctx->scan_hash_size); } @@ -2331,7 +2331,7 @@ void WmDestroyCtx(MpmCtx *mpm_ctx) { BloomFilterFree(ctx->search_bloom[h]); } - free(ctx->search_bloom); + SCFree(ctx->search_bloom); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(BloomFilter *) * ctx->search_hash_size); @@ -2346,30 +2346,30 @@ void WmDestroyCtx(MpmCtx *mpm_ctx) { WmHashFree(mpm_ctx, ctx->search_hash[h]); } - free(ctx->search_hash); + SCFree(ctx->search_hash); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(WmHashItem) * ctx->search_hash_size); } if (ctx->search_shifttable) { - free(ctx->search_shifttable); + SCFree(ctx->search_shifttable); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(uint16_t) * ctx->search_hash_size); } if (ctx->scan_pminlen) { - free(ctx->scan_pminlen); + SCFree(ctx->scan_pminlen); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(uint8_t) * ctx->scan_hash_size); } #if 0 if (ctx->search_pminlen) { - free(ctx->search_pminlen); + SCFree(ctx->search_pminlen); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= (sizeof(uint8_t) * ctx->search_hash_size); } #endif - free(mpm_ctx->ctx); + SCFree(mpm_ctx->ctx); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= sizeof(WmCtx); } @@ -2378,7 +2378,7 @@ void WmThreadInitCtx(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, uint32_t mat memset(mpm_thread_ctx, 0, sizeof(MpmThreadCtx)); if (sizeof(WmThreadCtx) > 0) { /* size can be 0 when optimized */ - mpm_thread_ctx->ctx = malloc(sizeof(WmThreadCtx)); + mpm_thread_ctx->ctx = SCMalloc(sizeof(WmThreadCtx)); if (mpm_thread_ctx->ctx == NULL) return; @@ -2394,7 +2394,7 @@ void WmThreadInitCtx(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, uint32_t mat * unique id and is the array lookup key at the same time */ uint32_t keys = matchsize + 1; if (keys > 0) { - mpm_thread_ctx->match = malloc(keys * sizeof(MpmMatchBucket)); + mpm_thread_ctx->match = SCMalloc(keys * sizeof(MpmMatchBucket)); if (mpm_thread_ctx->match == NULL) { printf("ERROR: could not setup memory for pattern matcher: %s\n", strerror(errno)); exit(1); @@ -2413,13 +2413,13 @@ void WmThreadDestroyCtx(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx) { if (ctx != NULL) { /* size can be 0 when optimized */ mpm_thread_ctx->memory_cnt--; mpm_thread_ctx->memory_size -= sizeof(WmThreadCtx); - free(mpm_thread_ctx->ctx); + SCFree(mpm_thread_ctx->ctx); } if (mpm_thread_ctx->match != NULL) { mpm_thread_ctx->memory_cnt--; mpm_thread_ctx->memory_size -= ((mpm_thread_ctx->matchsize + 1) * sizeof(MpmMatchBucket)); - free(mpm_thread_ctx->match); + SCFree(mpm_thread_ctx->match); } MpmMatchFreeSpares(mpm_thread_ctx, mpm_thread_ctx->sparelist); diff --git a/src/util-mpm.c b/src/util-mpm.c index 81cf72117b..a3f49db0d4 100644 --- a/src/util-mpm.c +++ b/src/util-mpm.c @@ -21,7 +21,11 @@ int PmqSetup(PatternMatcherQueue *pmq, uint32_t maxid) { memset(pmq, 0, sizeof(PatternMatcherQueue)); - pmq->sig_id_array = malloc(maxid * sizeof(uint32_t)); + if (maxid > 0) + pmq->sig_id_array = SCMalloc(maxid * sizeof(uint32_t)); + else + pmq->sig_id_array = NULL; + if (pmq->sig_id_array == NULL) { printf("ERROR: could not setup memory for pattern matcher: %s\n", strerror(errno)); return -1; @@ -30,7 +34,7 @@ int PmqSetup(PatternMatcherQueue *pmq, uint32_t maxid) { pmq->sig_id_array_cnt = 0; /* lookup bitarray */ - pmq->sig_bitarray = malloc(maxid / 8 + 1); + pmq->sig_bitarray = SCMalloc(maxid / 8 + 1); if (pmq->sig_bitarray == NULL) { printf("ERROR: could not setup memory for pattern matcher: %s\n", strerror(errno)); return -1; @@ -59,12 +63,12 @@ void PmqCleanup(PatternMatcherQueue *pmq) { return; if (pmq->sig_id_array != NULL) { - free(pmq->sig_id_array); + SCFree(pmq->sig_id_array); pmq->sig_id_array = NULL; } if (pmq->sig_bitarray != NULL) { - free(pmq->sig_bitarray); + SCFree(pmq->sig_bitarray); pmq->sig_bitarray = NULL; } @@ -79,7 +83,7 @@ void PmqFree(PatternMatcherQueue *pmq) { return; PmqCleanup(pmq); - free(pmq); + SCFree(pmq); } /* cleanup list with all matches @@ -121,7 +125,7 @@ MpmMatchCleanup(MpmThreadCtx *thread_ctx) { * used at search runtime */ inline MpmMatch * MpmMatchAlloc(MpmThreadCtx *thread_ctx) { - MpmMatch *m = malloc(sizeof(MpmMatch)); + MpmMatch *m = SCMalloc(sizeof(MpmMatch)); if (m == NULL) return NULL; @@ -224,7 +228,7 @@ MpmMatchAppend(MpmThreadCtx *thread_ctx, PatternMatcherQueue *pmq, MpmEndMatch * void MpmMatchFree(MpmThreadCtx *ctx, MpmMatch *m) { ctx->memory_cnt--; ctx->memory_size -= sizeof(MpmMatch); - free(m); + SCFree(m); } void MpmMatchFreeSpares(MpmThreadCtx *mpm_ctx, MpmMatch *m) { @@ -240,7 +244,7 @@ void MpmMatchFreeSpares(MpmThreadCtx *mpm_ctx, MpmMatch *m) { * Only used in the initialization phase */ MpmEndMatch *MpmAllocEndMatch (MpmCtx *ctx) { - MpmEndMatch *e = malloc(sizeof(MpmEndMatch)); + MpmEndMatch *e = SCMalloc(sizeof(MpmEndMatch)); if (e == NULL) return NULL; @@ -268,7 +272,7 @@ int32_t MpmMatcherGetMaxPatternLength(uint16_t matcher) { void MpmEndMatchFree(MpmCtx *ctx, MpmEndMatch *em) { ctx->memory_cnt--; ctx->memory_size -= sizeof(MpmEndMatch); - free(em); + SCFree(em); } void MpmEndMatchFreeAll(MpmCtx *mpm_ctx, MpmEndMatch *em) { diff --git a/src/util-pool.c b/src/util-pool.c index 6c2a9957b5..441446a67a 100644 --- a/src/util-pool.c +++ b/src/util-pool.c @@ -18,7 +18,7 @@ Pool *PoolInit(uint32_t size, uint32_t prealloc_size, void *(*Alloc)(void *), vo goto error; /* setup the filter */ - p = malloc(sizeof(Pool)); + p = SCMalloc(sizeof(Pool)); if (p == NULL) goto error; @@ -33,7 +33,7 @@ Pool *PoolInit(uint32_t size, uint32_t prealloc_size, void *(*Alloc)(void *), vo uint32_t u32 = 0; for (u32 = 0; u32 < size; u32++) { /* populate pool */ - PoolBucket *pb = malloc(sizeof(PoolBucket)); + PoolBucket *pb = SCMalloc(sizeof(PoolBucket)); if (pb == NULL) goto error; @@ -46,7 +46,7 @@ Pool *PoolInit(uint32_t size, uint32_t prealloc_size, void *(*Alloc)(void *), vo /* prealloc the buckets and requeue them to the alloc list */ for (u32 = 0; u32 < prealloc_size; u32++) { if (size == 0) { /* unlimited */ - PoolBucket *pb = malloc(sizeof(PoolBucket)); + PoolBucket *pb = SCMalloc(sizeof(PoolBucket)); if (pb == NULL) goto error; @@ -91,16 +91,20 @@ void PoolFree(Pool *p) { p->alloc_list = pb->next; p->Free(pb->data); pb->data = NULL; - free(pb); + SCFree(pb); } while (p->empty_list != NULL) { PoolBucket *pb = p->empty_list; p->empty_list = pb->next; - free(pb); + if (pb->data!= NULL) { + p->Free(pb->data); + pb->data = NULL; + } + SCFree(pb); } - free(p); + SCFree(p); } void PoolPrint(Pool *p) { @@ -186,17 +190,17 @@ void PoolPrintSaturation(Pool *p) { */ void *PoolTestAlloc(void *allocdata) { - return malloc(10); + return SCMalloc(10); } void *PoolTestAllocArg(void *allocdata) { size_t len = strlen((char *)allocdata) + 1; - char *str = malloc(len); + char *str = SCMalloc(len); strlcpy(str,(char *)allocdata,len); return (void *)str; } void PoolTestFree(void *ptr) { - free(ptr); + SCFree(ptr); } #ifdef UNITTESTS @@ -274,7 +278,7 @@ static int PoolTestInit03 (void) { retval = 1; end: if (data != NULL) - free(data); + SCFree(data); if (p != NULL) PoolFree(p); return retval; @@ -316,7 +320,7 @@ static int PoolTestInit04 (void) { retval = 1; end: if (str != NULL) - free(str); + SCFree(str); if (p != NULL) PoolFree(p); return retval; @@ -367,7 +371,7 @@ static int PoolTestInit05 (void) { retval = 1; end: if (data != NULL) - free(data); + SCFree(data); if (p != NULL) PoolFree(p); return retval; @@ -426,9 +430,9 @@ static int PoolTestInit06 (void) { retval = 1; end: if (data != NULL) - free(data); + SCFree(data); if (data2 != NULL) - free(data2); + SCFree(data2); if (p != NULL) PoolFree(p); return retval; @@ -509,9 +513,9 @@ static int PoolTestInit07 (void) { retval = 1; end: if (data != NULL) - free(data); + SCFree(data); if (data2 != NULL) - free(data2); + SCFree(data2); if (p != NULL) PoolFree(p); return retval; diff --git a/src/util-radix-tree.c b/src/util-radix-tree.c index fbb75e5398..61849b3ed5 100644 --- a/src/util-radix-tree.c +++ b/src/util-radix-tree.c @@ -8,6 +8,7 @@ #include #include "suricata-common.h" +#include "suricata-common.h" #ifdef OS_FREEBSD #include #endif /* OS_FREEBSD */ @@ -36,13 +37,13 @@ struct in_addr *SCRadixValidateIPV4Address(const char *addr_str) { struct in_addr *addr = NULL; - if ( (addr = malloc(sizeof(struct in_addr))) == NULL) { + if ( (addr = SCMalloc(sizeof(struct in_addr))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } if (inet_pton(AF_INET, addr_str, addr) <= 0) { - free(addr); + SCFree(addr); return NULL; } @@ -63,13 +64,13 @@ struct in6_addr *SCRadixValidateIPV6Address(const char *addr_str) { struct in6_addr *addr = NULL; - if ( (addr = malloc(sizeof(struct in6_addr))) == NULL) { + if ( (addr = SCMalloc(sizeof(struct in6_addr))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } if (inet_pton(AF_INET6, addr_str, addr) <= 0) { - free(addr); + SCFree(addr); return NULL; } @@ -117,7 +118,7 @@ void SCRadixChopIPAddressAgainstNetmask(uint8_t *stream, uint8_t netmask, */ static SCRadixUserData *SCRadixAllocSCRadixUserData(uint8_t netmask, void *user) { - SCRadixUserData *user_data = malloc(sizeof(SCRadixUserData)); + SCRadixUserData *user_data = SCMalloc(sizeof(SCRadixUserData)); if (user_data == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); @@ -138,7 +139,7 @@ static SCRadixUserData *SCRadixAllocSCRadixUserData(uint8_t netmask, void *user) */ static void SCRadixDeAllocSCRadixUserData(SCRadixUserData *user_data) { - free(user_data); + SCFree(user_data); return; } @@ -214,13 +215,13 @@ static SCRadixPrefix *SCRadixCreatePrefix(uint8_t *key_stream, return NULL; } - if ( (prefix = malloc(sizeof(SCRadixPrefix))) == NULL) { + if ( (prefix = SCMalloc(sizeof(SCRadixPrefix))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } memset(prefix, 0, sizeof(SCRadixPrefix)); - if ( (prefix->stream = malloc(key_bitlen / 8)) == NULL) { + if ( (prefix->stream = SCMalloc(key_bitlen / 8)) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } @@ -424,7 +425,7 @@ static void SCRadixReleasePrefix(SCRadixPrefix *prefix, SCRadixTree *tree) if (prefix != NULL) { if (prefix->stream != NULL) - free(prefix->stream); + SCFree(prefix->stream); user_data_temp1 = prefix->user_data; user_data_temp2 = user_data_temp1; @@ -437,7 +438,7 @@ static void SCRadixReleasePrefix(SCRadixPrefix *prefix, SCRadixTree *tree) } } - free(prefix); + SCFree(prefix); } return; @@ -452,7 +453,7 @@ static inline SCRadixNode *SCRadixCreateNode() { SCRadixNode *node = NULL; - if ( (node = malloc(sizeof(SCRadixNode))) == NULL) { + if ( (node = SCMalloc(sizeof(SCRadixNode))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } @@ -471,7 +472,7 @@ static inline void SCRadixReleaseNode(SCRadixNode *node, SCRadixTree *tree) { if (node != NULL) { SCRadixReleasePrefix(node->prefix, tree); - free(node); + SCFree(node); } return; @@ -489,7 +490,7 @@ SCRadixTree *SCRadixCreateRadixTree(void (*Free)(void*)) { SCRadixTree *tree = NULL; - if ( (tree = malloc(sizeof(SCRadixTree))) == NULL) { + if ( (tree = SCMalloc(sizeof(SCRadixTree))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); } @@ -599,7 +600,7 @@ static SCRadixNode *SCRadixAddKey(uint8_t *key_stream, uint16_t key_bitlen, * chopping the incoming search ip key using the netmask values added * into the tree and then verify for a match */ node->netmask_cnt++; - if ( (node->netmasks = realloc(node->netmasks, (node->netmask_cnt * + if ( (node->netmasks = SCRealloc(node->netmasks, (node->netmask_cnt * sizeof(uint8_t)))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); @@ -717,7 +718,7 @@ static SCRadixNode *SCRadixAddKey(uint8_t *key_stream, uint16_t key_bitlen, } node->netmask_cnt++; - if ( (node->netmasks = realloc(node->netmasks, (node->netmask_cnt * + if ( (node->netmasks = SCRealloc(node->netmasks, (node->netmask_cnt * sizeof(uint8_t)))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); @@ -789,7 +790,7 @@ static SCRadixNode *SCRadixAddKey(uint8_t *key_stream, uint16_t key_bitlen, break; } - if ( (inter_node->netmasks = malloc((node->netmask_cnt - i) * + if ( (inter_node->netmasks = SCMalloc((node->netmask_cnt - i) * sizeof(uint8_t))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); @@ -802,7 +803,7 @@ static SCRadixNode *SCRadixAddKey(uint8_t *key_stream, uint16_t key_bitlen, node->netmask_cnt = i; if (node->netmask_cnt == 0) { - free(node->netmasks); + SCFree(node->netmasks); node->netmasks = NULL; } } @@ -837,7 +838,7 @@ static SCRadixNode *SCRadixAddKey(uint8_t *key_stream, uint16_t key_bitlen, } node->netmask_cnt++; - if ( (node->netmasks = realloc(node->netmasks, (node->netmask_cnt * + if ( (node->netmasks = SCRealloc(node->netmasks, (node->netmask_cnt * sizeof(uint8_t)))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); exit(EXIT_FAILURE); @@ -975,7 +976,7 @@ static void SCRadixTransferNetmasksBWNodes(SCRadixNode *dest, SCRadixNode *src) if (src->netmasks == NULL) return; - if ( (dest->netmasks = realloc(dest->netmasks, + if ( (dest->netmasks = SCRealloc(dest->netmasks, (src->netmask_cnt + dest->netmask_cnt) * sizeof(uint8_t))) == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); @@ -1036,12 +1037,12 @@ static void SCRadixRemoveNetblockEntry(SCRadixNode *node, uint8_t netmask) node->netmask_cnt--; if (node->netmask_cnt == 0) { - free(node->netmasks); + SCFree(node->netmasks); node->netmasks = NULL; return; } - node->netmasks = realloc(node->netmasks, node->netmask_cnt * sizeof(uint8_t)); + node->netmasks = SCRealloc(node->netmasks, node->netmask_cnt * sizeof(uint8_t)); if (node->netmasks == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory using realloc"); exit(EXIT_FAILURE); @@ -1126,7 +1127,7 @@ static void SCRadixRemoveKey(uint8_t *key_stream, uint16_t key_bitlen, /* we are deleting the root of the tree. This would be the only node left * in the tree */ if (tree->head == node) { - free(node); + SCFree(node); tree->head = NULL; return; } diff --git a/src/util-rule-vars.c b/src/util-rule-vars.c index 23ce4d24ab..b6e7dd4815 100644 --- a/src/util-rule-vars.c +++ b/src/util-rule-vars.c @@ -59,7 +59,7 @@ char *SCRuleVarsGetConfVar(const char *conf_var_name, goto end; /* the + 2 is for the '.' and the string termination character '\0' */ - conf_var_full_name = (char *)malloc(strlen(conf_var_type_name) + + conf_var_full_name = (char *)SCMalloc(strlen(conf_var_type_name) + strlen(conf_var_name) + 2); if (conf_var_full_name == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); @@ -83,7 +83,7 @@ char *SCRuleVarsGetConfVar(const char *conf_var_name, end: if (conf_var_full_name != NULL) - free(conf_var_full_name); + SCFree(conf_var_full_name); SCReturnCharPtr(conf_var_full_name_value); } diff --git a/src/util-spm-bm.c b/src/util-spm-bm.c index d9875d3eaa..70e1af04af 100644 --- a/src/util-spm-bm.c +++ b/src/util-spm-bm.c @@ -73,7 +73,7 @@ inline void PreBmGs(const uint8_t *x, int32_t m, int32_t *bmGs) { int32_t i, j; int32_t *suff; - suff = malloc(sizeof(int32_t) * (m + 1)); + suff = SCMalloc(sizeof(int32_t) * (m + 1)); BoyerMooreSuffixes(x, m, suff); @@ -90,7 +90,7 @@ inline void PreBmGs(const uint8_t *x, int32_t m, int32_t *bmGs) { for (i = 0; i <= m - 2; ++i) bmGs[m - 1 - suff[i]] = m - 1 - i; - free(suff); + SCFree(suff); } /** @@ -145,7 +145,7 @@ inline void PreBmGsNocase(const uint8_t *x, int32_t m, int32_t *bmGs) { int32_t i, j; int32_t* suff; - suff = malloc(sizeof(int32_t) * (m + 1)); + suff = SCMalloc(sizeof(int32_t) * (m + 1)); BoyerMooreSuffixesNocase(x, m, suff); @@ -166,7 +166,7 @@ inline void PreBmGsNocase(const uint8_t *x, int32_t m, int32_t *bmGs) { bmGs[m - 1 - suff[i]] = m - 1 - i; } - free(suff); + SCFree(suff); } /** diff --git a/src/util-spm.c b/src/util-spm.c index 13e98ebb2d..8e45e31226 100644 --- a/src/util-spm.c +++ b/src/util-spm.c @@ -88,13 +88,13 @@ inline uint8_t *Bs2bmNocaseSearch(uint8_t *text, uint32_t textlen, uint8_t *need */ inline uint8_t *BoyerMooreSearch(uint8_t *text, uint32_t textlen, uint8_t *needle, uint32_t needlelen) { int32_t bmBc[ALPHABET_SIZE]; - int32_t *bmGs = malloc(sizeof(int32_t)*(needlelen + 1)); + int32_t *bmGs = SCMalloc(sizeof(int32_t)*(needlelen + 1)); PreBmGs(needle, needlelen, bmGs); PreBmBc(needle, needlelen, bmBc); uint8_t *ret = BoyerMoore(needle, needlelen, text, textlen, bmGs, bmBc); - free(bmGs); + SCFree(bmGs); return ret; } @@ -110,13 +110,13 @@ inline uint8_t *BoyerMooreSearch(uint8_t *text, uint32_t textlen, uint8_t *needl */ inline uint8_t *BoyerMooreNocaseSearch(uint8_t *text, uint32_t textlen, uint8_t *needle, uint32_t needlelen) { int32_t bmBc[ALPHABET_SIZE]; - int32_t *bmGs = malloc(sizeof(int32_t)*(needlelen + 1)); + int32_t *bmGs = SCMalloc(sizeof(int32_t)*(needlelen + 1)); PreBmGsNocase(needle, needlelen, bmGs); PreBmBcNocase(needle, needlelen, bmBc); uint8_t *ret = BoyerMooreNocase(needle, needlelen, text, textlen, bmGs, bmBc); - free(bmGs); + SCFree(bmGs); return ret; } @@ -216,7 +216,7 @@ inline uint8_t *BoyerMooreWrapper(uint8_t *text, uint8_t *needle, int times) { uint32_t needlelen = strlen((char *)needle); int32_t bmBc[ALPHABET_SIZE]; - int32_t *bmGs = malloc(sizeof(int32_t)*(needlelen + 1)); + int32_t *bmGs = SCMalloc(sizeof(int32_t)*(needlelen + 1)); uint8_t *ret = NULL; int i = 0; @@ -230,7 +230,7 @@ inline uint8_t *BoyerMooreWrapper(uint8_t *text, uint8_t *needle, int times) { ret = BoyerMoore(needle, needlelen, text, textlen, bmGs, bmBc); } if (times > 1) { CLOCK_END; CLOCK_PRINT_SEC; }; - free(bmGs); + SCFree(bmGs); return ret; } @@ -239,7 +239,7 @@ inline uint8_t *BoyerMooreNocaseWrapper(uint8_t *text, uint8_t *needle, int time uint32_t needlelen = strlen((char *)needle); int32_t bmBc[ALPHABET_SIZE]; - int32_t *bmGs = malloc(sizeof(int32_t)*(needlelen + 1)); + int32_t *bmGs = SCMalloc(sizeof(int32_t)*(needlelen + 1)); uint8_t *ret = NULL; int i = 0; @@ -253,7 +253,7 @@ inline uint8_t *BoyerMooreNocaseWrapper(uint8_t *text, uint8_t *needle, int time ret = BoyerMooreNocase(needle, needlelen, text, textlen, bmGs, bmBc); } if (times > 1) { CLOCK_END; CLOCK_PRINT_SEC; }; - free(bmGs); + SCFree(bmGs); return ret; } @@ -344,7 +344,7 @@ inline uint8_t *BoyerMooreCtxWrapper(uint8_t *text, uint8_t *needle, int times) uint32_t needlelen = strlen((char *)needle); int32_t bmBc[ALPHABET_SIZE]; - int32_t *bmGs = malloc(sizeof(int32_t)*(needlelen + 1)); + int32_t *bmGs = SCMalloc(sizeof(int32_t)*(needlelen + 1)); uint8_t *ret = NULL; int i = 0; @@ -359,7 +359,7 @@ inline uint8_t *BoyerMooreCtxWrapper(uint8_t *text, uint8_t *needle, int times) ret = BoyerMoore(needle, needlelen, text, textlen, bmGs, bmBc); } if (times > 1) { CLOCK_END; CLOCK_PRINT_SEC; }; - free(bmGs); + SCFree(bmGs); return ret; } @@ -384,7 +384,7 @@ inline uint8_t *BoyerMooreNocaseCtxWrapper(uint8_t *text, uint8_t *needle, int t uint32_t needlelen = strlen((char *)needle); int32_t bmBc[ALPHABET_SIZE]; - int32_t *bmGs = malloc(sizeof(int32_t)*(needlelen + 1)); + int32_t *bmGs = SCMalloc(sizeof(int32_t)*(needlelen + 1)); uint8_t *ret = NULL; int i = 0; @@ -398,7 +398,7 @@ inline uint8_t *BoyerMooreNocaseCtxWrapper(uint8_t *text, uint8_t *needle, int t ret = BoyerMooreNocase(needle, needlelen, text, textlen, bmGs, bmBc); } if (times > 1) { CLOCK_END; CLOCK_PRINT_SEC; }; - free(bmGs); + SCFree(bmGs); return ret; } diff --git a/src/util-unittest-helper.c b/src/util-unittest-helper.c index b51b2a66ee..ec4865aa06 100644 --- a/src/util-unittest-helper.c +++ b/src/util-unittest-helper.c @@ -36,7 +36,7 @@ Packet *UTHBuildPacketReal(uint8_t *payload, uint16_t payload_len, uint16_t sport, uint16_t dport) { struct in_addr in; - Packet *p = malloc(sizeof(Packet)); + Packet *p = SCMalloc(sizeof(Packet)); if (p == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating packet"); exit(EXIT_FAILURE); @@ -59,12 +59,12 @@ Packet *UTHBuildPacketReal(uint8_t *payload, uint16_t payload_len, switch (ipproto) { case IPPROTO_UDP: - p->ip4h = malloc(sizeof(IPV4Hdr)); + p->ip4h = SCMalloc(sizeof(IPV4Hdr)); if (p->ip4h == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating packet ip4h"); exit(EXIT_FAILURE); } - p->udph = malloc(sizeof(UDPHdr)); + p->udph = SCMalloc(sizeof(UDPHdr)); if (p->udph == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating packet udph"); exit(EXIT_FAILURE); @@ -73,12 +73,12 @@ Packet *UTHBuildPacketReal(uint8_t *payload, uint16_t payload_len, p->udph->uh_dport = dport; break; case IPPROTO_TCP: - p->ip4h = malloc(sizeof(IPV4Hdr)); + p->ip4h = SCMalloc(sizeof(IPV4Hdr)); if (p->ip4h == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating packet ip4h"); exit(EXIT_FAILURE); } - p->tcph = malloc(sizeof(TCPHdr)); + p->tcph = SCMalloc(sizeof(TCPHdr)); if (p->tcph == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating packet tcph"); exit(EXIT_FAILURE); @@ -87,7 +87,7 @@ Packet *UTHBuildPacketReal(uint8_t *payload, uint16_t payload_len, p->tcph->th_dport = dport; break; case IPPROTO_ICMP: - p->ip4h = malloc(sizeof(IPV4Hdr)); + p->ip4h = SCMalloc(sizeof(IPV4Hdr)); if (p->ip4h == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating packet ip4h"); exit(EXIT_FAILURE); @@ -135,7 +135,7 @@ Packet **UTHBuildPacketArrayFromEth(uint8_t *raw_eth[], int *pktsize, int numpkt return NULL; } Packet **p = NULL; - p = malloc(sizeof(Packet *) * numpkts); + p = SCMalloc(sizeof(Packet *) * numpkts); if (p == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory for the packet array"); return NULL; @@ -146,10 +146,10 @@ Packet **UTHBuildPacketArrayFromEth(uint8_t *raw_eth[], int *pktsize, int numpkt int i = 0; for (; i < numpkts; i++) { - p[i] = malloc(sizeof(Packet)); + p[i] = SCMalloc(sizeof(Packet)); if (p[i] == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory for a packet of the array"); - free(p); + SCFree(p); return NULL; } memset(p[i], 0, sizeof(Packet)); @@ -170,7 +170,7 @@ Packet **UTHBuildPacketArrayFromEth(uint8_t *raw_eth[], int *pktsize, int numpkt Packet *UTHBuildPacketFromEth(uint8_t *raw_eth, uint16_t pktsize) { DecodeThreadVars dtv; ThreadVars th_v; - Packet *p = malloc(sizeof(Packet)); + Packet *p = SCMalloc(sizeof(Packet)); if (p == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory for the packet"); return NULL; @@ -246,23 +246,23 @@ void UTHFreePacket(Packet *p) { switch (p->proto) { case IPPROTO_UDP: if (p->udph != NULL) - free(p->udph); + SCFree(p->udph); if (p->ip4h != NULL) - free(p->ip4h); + SCFree(p->ip4h); break; case IPPROTO_TCP: if (p->tcph != NULL) - free(p->tcph); + SCFree(p->tcph); if (p->ip4h != NULL) - free(p->ip4h); + SCFree(p->ip4h); break; case IPPROTO_ICMP: if (p->ip4h != NULL) - free(p->ip4h); + SCFree(p->ip4h); break; /* TODO: Add more protocols */ } - free(p); + SCFree(p); } /** @@ -529,8 +529,12 @@ int UTHPacketMatchSigMpm(Packet *p, char *sig, uint16_t mpm_type) { } end: + SigGroupCleanup(de_ctx); + SigCleanSignatures(de_ctx); + if (det_ctx != NULL) DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx); + if (de_ctx != NULL) DetectEngineCtxFree(de_ctx); @@ -582,6 +586,11 @@ int UTHPacketMatchSig(Packet *p, char *sig) { } end: + if (de_ctx) { + SigGroupCleanup(de_ctx); + SigCleanSignatures(de_ctx); + } + if (det_ctx != NULL) DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx); if (de_ctx != NULL) @@ -648,7 +657,10 @@ int UTHBuildPacketRealTest01(void) { Packet *p = UTHBuildPacketReal(payload, sizeof(payload), IPPROTO_TCP, "192.168.1.5", "192.168.1.1", 41424, 80); - return CheckUTHTestPacket(p, IPPROTO_TCP); + int ret = CheckUTHTestPacket(p, IPPROTO_TCP); + UTHFreePacket(p); + + return ret; } /** @@ -660,7 +672,9 @@ int UTHBuildPacketRealTest02(void) { Packet *p = UTHBuildPacketReal(payload, sizeof(payload), IPPROTO_UDP, "192.168.1.5", "192.168.1.1", 41424, 80); - return CheckUTHTestPacket(p, IPPROTO_UDP); + int ret = CheckUTHTestPacket(p, IPPROTO_UDP); + UTHFreePacket(p); + return ret; } /** @@ -671,7 +685,10 @@ int UTHBuildPacketTest01(void) { Packet *p = UTHBuildPacket(payload, sizeof(payload), IPPROTO_TCP); - return CheckUTHTestPacket(p, IPPROTO_TCP); + int ret = CheckUTHTestPacket(p, IPPROTO_TCP); + UTHFreePacket(p); + + return ret; } /** @@ -682,7 +699,10 @@ int UTHBuildPacketTest02(void) { Packet *p = UTHBuildPacket(payload, sizeof(payload), IPPROTO_UDP); - return CheckUTHTestPacket(p, IPPROTO_UDP); + int ret = CheckUTHTestPacket(p, IPPROTO_UDP); + UTHFreePacket(p); + + return ret; } /** @@ -694,7 +714,10 @@ int UTHBuildPacketSrcDstTest01(void) { Packet *p = UTHBuildPacketSrcDst(payload, sizeof(payload), IPPROTO_TCP, "192.168.1.5", "192.168.1.1"); - return CheckUTHTestPacket(p, IPPROTO_TCP); + int ret = CheckUTHTestPacket(p, IPPROTO_TCP); + UTHFreePacket(p); + + return ret; } /** @@ -706,7 +729,10 @@ int UTHBuildPacketSrcDstTest02(void) { Packet *p = UTHBuildPacketSrcDst(payload, sizeof(payload), IPPROTO_UDP, "192.168.1.5", "192.168.1.1"); - return CheckUTHTestPacket(p, IPPROTO_UDP); + int ret = CheckUTHTestPacket(p, IPPROTO_UDP); + UTHFreePacket(p); + + return ret; } /** @@ -718,7 +744,10 @@ int UTHBuildPacketSrcDstPortsTest01(void) { Packet *p = UTHBuildPacketSrcDstPorts(payload, sizeof(payload), IPPROTO_TCP, 41424, 80); - return CheckUTHTestPacket(p, IPPROTO_TCP); + int ret = CheckUTHTestPacket(p, IPPROTO_TCP); + UTHFreePacket(p); + + return ret; } /** @@ -730,7 +759,10 @@ int UTHBuildPacketSrcDstPortsTest02(void) { Packet *p = UTHBuildPacketSrcDstPorts(payload, sizeof(payload), IPPROTO_UDP, 41424, 80); - return CheckUTHTestPacket(p, IPPROTO_UDP); + int ret = CheckUTHTestPacket(p, IPPROTO_UDP); + UTHFreePacket(p); + + return ret; } #endif /* UNITTESTS */ diff --git a/src/util-unittest.c b/src/util-unittest.c index 9a08c90d02..f3d5f29ad2 100644 --- a/src/util-unittest.c +++ b/src/util-unittest.c @@ -23,9 +23,9 @@ static UtTest *ut_list; */ static UtTest *UtAllocTest(void) { - UtTest *ut = malloc(sizeof(UtTest)); + UtTest *ut = SCMalloc(sizeof(UtTest)); if (ut == NULL) { - printf("ERROR: UtTest *ut = malloc(sizeof(UtTest)); failed\n"); + printf("ERROR: UtTest *ut = SCMalloc(sizeof(UtTest)); failed\n"); return NULL; } @@ -223,7 +223,7 @@ void UtCleanup(void) { while (tmp != NULL) { otmp = tmp->next; - free(tmp); + SCFree(tmp); tmp = otmp; } diff --git a/src/util-var-name.c b/src/util-var-name.c index b218022d59..a18c0504ce 100644 --- a/src/util-var-name.c +++ b/src/util-var-name.c @@ -42,11 +42,11 @@ static void VariableNameFree(void *data) { return; if (fn->name != NULL) { - free(fn->name); + SCFree(fn->name); fn->name = NULL; } - free(fn); + SCFree(fn); } /** \brief Initialize the Name idx hash. @@ -76,14 +76,14 @@ void VariableNameFreeHash(DetectEngineCtx *de_ctx) { uint16_t VariableNameGetIdx(DetectEngineCtx *de_ctx, char *name, uint8_t type) { uint16_t idx = 0; - VariableName *fn = malloc(sizeof(VariableName)); + VariableName *fn = SCMalloc(sizeof(VariableName)); if (fn == NULL) goto error; memset(fn, 0, sizeof(VariableName)); fn->type = type; - fn->name = strdup(name); + fn->name = SCStrdup(name); if (fn->name == NULL) goto error; diff --git a/src/win32-misc.c b/src/win32-misc.c index 0cd0264d1b..27e28b9355 100644 --- a/src/win32-misc.c +++ b/src/win32-misc.c @@ -6,19 +6,19 @@ int setenv(const char *name, const char *value, int overwrite) { if (overwrite || NULL == getenv(name)) { - char *str = malloc(strlen(name) + strlen(value) + 2); + char *str = SCMalloc(strlen(name) + strlen(value) + 2); sprintf(str, "%s=%s", name, value); putenv(str); - free(str); + SCFree(str); } } int unsetenv(const char *name) { - char *str = malloc(strlen(name) + 2); + char *str = SCMalloc(strlen(name) + 2); sprintf(str, "%s=", name); putenv(str); - free(str); + SCFree(str); } const char* inet_ntop(int af, const void *src, char *dst, uint32_t cnt)