From a7abc8348801f8bc2f802447c109083b9e199806 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bryon=20Gloden=2C=20CISSP=C2=AE?= Date: Tue, 2 Aug 2016 17:19:19 -0400 Subject: [PATCH] Update acism_create.c Passing one pointer into realloc() and assigning the result directly into that same pointer variable can cause a memory leak if the reallocation fails, because the original allocation will still exist. The correct way to do this is to use a temporary pointer variable. Credit: http://stackoverflow.com/a/11548901 Found by https://github.com/bryongloden/cppcheck --- contrib/aho-corasick/acism_create.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/contrib/aho-corasick/acism_create.c b/contrib/aho-corasick/acism_create.c index b1d9b51e3..a6d007fd7 100644 --- a/contrib/aho-corasick/acism_create.c +++ b/contrib/aho-corasick/acism_create.c @@ -323,9 +323,15 @@ interleave(TNODE *troot, int nnodes, int nsyms, TNODE **v1, TNODE **v2) if (last_trans < last) { last_trans = last; if (last + nsyms >= usev_size) { - usev = realloc(usev, usev_size << 1); - memset(usev + usev_size, 0, usev_size); - usev_size <<= 1; + char *tmp = realloc(usev, usev_size << 1); + if (tmp != NULL) { + usev = tmp; + memset(usev + usev_size, 0, usev_size); + usev_size <<= 1; + } else { + free(usev); + /* And handle error */ + } } } } -- 2.39.5