Blame SOURCES/popt-1.16-nextarg-memleak.patch

2fee6c
From 6fcb24d785a2c2d626bac6999aee6b3ab368be15 Mon Sep 17 00:00:00 2001
2fee6c
From: Peter Jones <pjones@redhat.com>
2fee6c
Date: Fri, 28 Jul 2017 16:11:40 -0400
2fee6c
Subject: [PATCH] Don't leak the last argument expanded by expandNextArg()
2fee6c
2fee6c
While using POPT_ARG_ARGV, I noticed this in valgrind's leak checker:
2fee6c
2fee6c
==1738== HEAP SUMMARY:
2fee6c
==1738==     in use at exit: 8 bytes in 1 blocks
2fee6c
==1738==   total heap usage: 94 allocs, 93 frees, 42,319 bytes allocated
2fee6c
==1738==
2fee6c
==1738== 8 bytes in 1 blocks are definitely lost in loss record 1 of 1
2fee6c
==1738==    at 0x4C2EB6B: malloc (vg_replace_malloc.c:299)
2fee6c
==1738==    by 0x4E3DF47: expandNextArg (popt.c:699)
2fee6c
==1738==    by 0x4E3F681: poptGetNextOpt (popt.c:1501)
2fee6c
==1738==    by 0x401F72: main (bingrep.c:433)
2fee6c
==1738==
2fee6c
==1738== LEAK SUMMARY:
2fee6c
==1738==    definitely lost: 8 bytes in 1 blocks
2fee6c
==1738==    indirectly lost: 0 bytes in 0 blocks
2fee6c
==1738==      possibly lost: 0 bytes in 0 blocks
2fee6c
==1738==    still reachable: 0 bytes in 0 blocks
2fee6c
==1738==         suppressed: 0 bytes in 0 blocks
2fee6c
2fee6c
My command line argument is a 7-byte string, and on first glance, it
2fee6c
appears this is because both expandNextArg() and poptSaveString()
2fee6c
duplicate the string.  The copy from poptSaveString() is the consuming
2fee6c
program's responsibility to free, but the intermediate pointer is popt's
2fee6c
responsibility.
2fee6c
2fee6c
Upon further examination, it appears popt normally does free this
2fee6c
string, but it only does it on the next entry to poptGetNextOpt(), and
2fee6c
on cleanOSE() in the case if we're not already at the bottom of
2fee6c
con->OptionStack.
2fee6c
2fee6c
This patch modifies poptResetContext() to ensure we'll always attempt to
2fee6c
free con->os->nextArg regardless of our position in the OptionStack, and
2fee6c
removes the duplicate free of con->os->argb in poptFreeContext(), as
2fee6c
it's called unconditionally by the poptResetContext() call on the
2fee6c
previous line.
2fee6c
2fee6c
This ensures that if poptGetNextOpt() isn't re-intered, poptFreeContext()
2fee6c
will free the memory that was allocated.  Now valgrind tells me:
2fee6c
2fee6c
==31734== HEAP SUMMARY:
2fee6c
==31734==     in use at exit: 0 bytes in 0 blocks
2fee6c
==31734==   total heap usage: 94 allocs, 94 frees, 42,319 bytes allocated
2fee6c
==31734==
2fee6c
==31734== All heap blocks were freed -- no leaks are possible
2fee6c
2fee6c
Signed-off-by: Peter Jones <pjones@redhat.com>
2fee6c
---
2fee6c
 popt.c | 3 +--
2fee6c
 1 file changed, 1 insertion(+), 2 deletions(-)
2fee6c
2fee6c
diff --git a/popt.c b/popt.c
2fee6c
index 1a53f40..72fbf5c 100644
2fee6c
--- a/popt.c
2fee6c
+++ b/popt.c
2fee6c
@@ -230,7 +230,7 @@ void poptResetContext(poptContext con)
2fee6c
     con->os->argb = PBM_FREE(con->os->argb);
2fee6c
     con->os->currAlias = NULL;
2fee6c
     con->os->nextCharArg = NULL;
2fee6c
-    con->os->nextArg = NULL;
2fee6c
+    con->os->nextArg = _free(con->os->nextArg);
2fee6c
     con->os->next = 1;			/* skip argv[0] */
2fee6c
 
2fee6c
     con->numLeftovers = 0;
2fee6c
@@ -1617,7 +1617,6 @@ poptContext poptFreeContext(poptContext con)
2fee6c
 {
2fee6c
     if (con == NULL) return con;
2fee6c
     poptResetContext(con);
2fee6c
-    con->os->argb = _free(con->os->argb);
2fee6c
 
2fee6c
     con->aliases = poptFreeItems(con->aliases, con->numAliases);
2fee6c
     con->numAliases = 0;
2fee6c
-- 
2fee6c
2.13.3
2fee6c