From de6a37f17bf6be9628564486c1625a7aa55c22ca Mon Sep 17 00:00:00 2001 From: Eric Leblond Date: Fri, 4 Mar 2016 19:13:43 +0100 Subject: [PATCH] tm-queue: unify queue name handling Queue name was sometimes allocated and sometimes not. This patch updates the behavior of creation function so it is always allocated. This way we can free it at exit and fix memory leak. This fixes: 900 bytes in 110 blocks are definitely lost in loss record 322 of 329 at 0x4C29C0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x803E0A9: strdup (in /lib/x86_64-linux-gnu/libc-2.21.so) by 0x90090E: StoreQueueId (tmqh-flow.c:112) by 0x8FFEA8: TmqhOutputFlowSetupCtx (tmqh-flow.c:180) by 0x908C7F: TmThreadCreate (tm-threads.c:1095) by 0x909982: TmThreadCreatePacketHandler (tm-threads.c:1154) by 0x87906F: RunModeFilePcapAutoFp (runmode-pcap-file.c:188) by 0x88376B: RunModeDispatch (runmodes.c:372) by 0x87F245: UnixSocketPcapFilesCheck (runmode-unix-socket.c:393) by 0x9102B0: UnixCommandBackgroundTasks (unix-manager.c:430) by 0x91405D: UnixManager (unix-manager.c:980) by 0x907773: TmThreadsManagement (tm-threads.c:600) tm-queue: fix logic WIP --- src/tm-queues.c | 11 ++++++++++- src/tmqh-flow.c | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/tm-queues.c b/src/tm-queues.c index e549a975c7..b956c71498 100644 --- a/src/tm-queues.c +++ b/src/tm-queues.c @@ -52,7 +52,10 @@ Tmq* TmqCreateQueue(char *name) goto error; Tmq *q = &tmqs[tmq_id]; - q->name = name; + q->name = SCStrdup(name); + if (q->name == NULL) + goto error; + q->id = tmq_id++; /* for cuda purposes */ q->q_type = 0; @@ -90,6 +93,12 @@ void TmqDebugList(void) void TmqResetQueues(void) { + uint16_t i; + for (i = 0; i < TMQ_MAX_QUEUES; i++) { + if (tmqs[i].name) { + SCFree(tmqs[i].name); + } + } memset(&tmqs, 0x00, sizeof(tmqs)); tmq_id = 0; } diff --git a/src/tmqh-flow.c b/src/tmqh-flow.c index c0898ef0af..6ea8917bdb 100644 --- a/src/tmqh-flow.c +++ b/src/tmqh-flow.c @@ -109,7 +109,7 @@ static int StoreQueueId(TmqhFlowCtx *ctx, char *name) void *ptmp; Tmq *tmq = TmqGetQueueByName(name); if (tmq == NULL) { - tmq = TmqCreateQueue(SCStrdup(name)); + tmq = TmqCreateQueue(name); if (tmq == NULL) return -1; }