On 6/23/22 00:37, Jim Meyering wrote:
On Mon, Jun 6, 2022 at 3:06 PM Paul Eggert <egg...@cs.ucla.edu> wrote:
On 6/6/22 12:37, Jim Meyering wrote:
Once you push that (and assuming you have nothing else pending), I'll
prepare another pre-release snapshot.
Thanks, I pushed it into grep master, after fixing the commentary issue
Bruno noted.
I was going to make a snapshot, but figured I should first run the
usual *SAN check:
i.e., configure && make as usual, but then "make clean" followed by
this on a linux system:

san='-fsanitize-address-use-after-scope -fsanitize=address
-static-libasan';
ASAN_OPTIONS=detect_leaks=1,allocator_may_return_null=1 make
CFLAGS='-O0 -ggdb3' AM_CFLAGS="$san" AM_LDFLAGS="$san" check

and it failed nearly every test, all due to leaks.

You can see a few leaks with an even simpler test using valgrind and a
no-ASAN binary:
(this was with the patch attached below, which fixed the first one I found)

Thanks for catching that. These are minor memory leaks in grep, not in Gnulib, and I fixed it by installing the attached into Grep. With it, all the Grep tests pass with your recipe. There are a few Gnulib failures in the memory-management modules but I expect these are merely the usual suspects.
From e2aec8c91e9d6ed3fc76f9f145dec8a456ce623a Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Fri, 24 Jun 2022 17:53:34 -0500
Subject: [PATCH] grep: fix regex compilation memory leaks

Problem reported by Jim Meyering in:
https://lists.gnu.org/r/grep-devel/2022-06/msg00012.html
* src/dfasearch.c (regex_compile): Fix memory leaks when SYNTAX_ONLY.
---
 src/dfasearch.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/src/dfasearch.c b/src/dfasearch.c
index 8d832f0..2720b3a 100644
--- a/src/dfasearch.c
+++ b/src/dfasearch.c
@@ -144,26 +144,34 @@ regex_compile (struct dfa_comp *dc, char const *p, idx_t len,
                idx_t pcount, idx_t lineno, reg_syntax_t syntax_bits,
                bool syntax_only)
 {
-  struct re_pattern_buffer pat0;
-  struct re_pattern_buffer *pat = syntax_only ? &pat0 : &dc->patterns[pcount];
-  pat->buffer = NULL;
-  pat->allocated = 0;
+  struct re_pattern_buffer pat;
+  pat.buffer = NULL;
+  pat.allocated = 0;
 
   /* Do not use a fastmap with -i, to work around glibc Bug#20381.  */
   verify (UCHAR_MAX < IDX_MAX);
   idx_t uchar_max = UCHAR_MAX;
-  pat->fastmap = (syntax_only | match_icase) ? NULL : ximalloc (uchar_max + 1);
+  pat.fastmap = syntax_only | match_icase ? NULL : ximalloc (uchar_max + 1);
 
-  pat->translate = NULL;
+  pat.translate = NULL;
 
   if (syntax_only)
     re_set_syntax (syntax_bits | RE_NO_SUB);
   else
     re_set_syntax (syntax_bits);
 
-  char const *err = re_compile_pattern (p, len, pat);
+  char const *err = re_compile_pattern (p, len, &pat);
   if (!err)
-    return true;
+    {
+      if (syntax_only)
+        regfree (&pat);
+      else
+        dc->patterns[pcount] = pat;
+
+      return true;
+    }
+
+  free (pat.fastmap);
 
   /* Emit a filename:lineno: prefix for patterns taken from files.  */
   idx_t pat_lineno;
-- 
2.25.1

Reply via email to