]> source.dussan.org Git - rspamd.git/commitdiff
Update acism_create.c 794/head
authorBryon Gloden, CISSPĀ® <cissp@bryongloden.com>
Tue, 2 Aug 2016 21:19:19 +0000 (17:19 -0400)
committerGitHub <noreply@github.com>
Tue, 2 Aug 2016 21:19:19 +0000 (17:19 -0400)
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

index b1d9b51e3eb464fd93fcbcaca6e896a5caad4831..a6d007fd79b7b3150c7f13460b6f481c1e2d1fba 100644 (file)
@@ -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 */
+                    }
                 }
             }
         }