Subject: Re: bug#25479: memory leaks in dfa

From: Norihiro Tanaka <[email-masked]>
Date: Thu, 19 Jan 2017 07:56:40 +0900

On Wed, 18 Jan 2017 23:11:28 +0200
Arnold Robbins <[email-masked]> wrote:

> Hi Grep Guys.
> 
> Please:
> 
> 	clone gawk repo
> 	cd gawk
> 	./bootstrap.sh && ./configure && make -j
> 	make valgrind
> 
> Look at the 'definitely lost' lines.  For example:
> 
> ==30472== 240 bytes in 2 blocks are definitely lost in loss record 128 of 170
> ==30472==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64
> -linux.so)
> ==30472==    by 0x44D42A: emalloc_real (awk.h:1923)
> ==30472==    by 0x44D42A: xmalloc (gawkmisc.c:57)
> ==30472==    by 0x468806: xnmalloc (xalloc.h:134)
> ==30472==    by 0x468806: alloc_position_set (dfa.c:2057)
> ==30472==    by 0x468D93: epsclosure (dfa.c:2272)
> ==30472==    by 0x468D93: dfaanalyze (dfa.c:2601)
> ==30472==    by 0x46CA84: dfacomp (dfa.c:3531)
> ==30472==    by 0x463284: make_regexp (re.c:231)
> ==30472==    by 0x414548: make_regnode (awkgram.y:5005)
> ==30472==    by 0x416A33: yyparse (awkgram.y:505)
> ==30472==    by 0x41C9E3: parse_program (awkgram.y:2580)
> ==30472==    by 0x40D373: main (main.c:459)
> 
> There may be other paths as well.
> 
> Can y'all track this down and fix?
> 
> Thanks,
> 
> Arnold

Thanks for the report.  It is caused by temporarily allocated memory not
freed.
From 3479bce8542f75c11e6b0b9907e22b26d91865ca Mon Sep 17 00:00:00 2001
From: Norihiro Tanaka <[email-masked]>
Date: Thu, 19 Jan 2017 07:44:13 +0900
Subject: [PATCH] dfa: fix memory leak in parse

Problem reported by Arnold Robbins in:
http://lists.gnu.org/archive/html/bug-grep/2017-01/msg00006.html

* lib/dfa.c (epsclosure): Do it.
---
 lib/dfa.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

diff --git a/lib/dfa.c b/lib/dfa.c
index f6c3017..5bac288 100644
--- a/lib/dfa.c
+++ b/lib/dfa.c
@@ -2262,6 +2262,7 @@ epsclosure (position_set *initial, struct dfa const *d)
 
         replace (initial, i, &d->follows[i], constraint, &tmp);
       }
+  free (tmp.elems);
 }
 
 /* Returns the set of contexts for which there is at least one
-- 
1.7.1