bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: "Tim Rühsen" <tim.ruehsen@gmx.de>
Cc: bug-gnulib@gnu.org
Subject: Fix memleak in regex.m4
Date: Thu, 21 May 2020 20:31:14 +0200	[thread overview]
Message-ID: <3584836.D8lSGNcFip@omega> (raw)
In-Reply-To: <e97c6577-7167-2b02-8b67-8e1a63701811@gmx.de>

Tim Rühsen wrote:
> configure:94107: checking for working re_compile_pattern
> SUMMARY: AddressSanitizer: 20072 byte(s) leaked in 116 allocation(s).

This patch should fix it.


2020-05-21  Bruno Haible  <bruno@clisp.org>

	regex: Avoid wrong configure results with "clang -fsanitize=leak".
	Reported by Tim Rühsen in
	<https://lists.gnu.org/archive/html/bug-gnulib/2020-05/msg00207.html>.
	* m4/regex.m4 (gl_REGEX): Free compiled regexes and allocated registers
	before returning with status 0.

diff --git a/m4/regex.m4 b/m4/regex.m4
index 65f5185..e723f59 100644
--- a/m4/regex.m4
+++ b/m4/regex.m4
@@ -1,4 +1,4 @@
-# serial 69
+# serial 70
 
 # Copyright (C) 1996-2001, 2003-2020 Free Software Foundation, Inc.
 #
@@ -90,11 +90,14 @@ AC_DEFUN([gl_REGEX],
                   s = re_compile_pattern (pat, sizeof pat - 1, &regex);
                   if (s)
                     result |= 1;
-                  else if (re_search (&regex, data, sizeof data - 1,
-                                      0, sizeof data - 1, &regs)
-                           != -1)
-                    result |= 1;
-                  regfree (&regex);
+                  else
+                    {
+                      if (re_search (&regex, data, sizeof data - 1,
+                                     0, sizeof data - 1, &regs)
+                          != -1)
+                        result |= 1;
+                      regfree (&regex);
+                    }
                 }
 
                 {
@@ -125,8 +128,8 @@ AC_DEFUN([gl_REGEX],
                                      0, sizeof data - 1, 0);
                       if (i != 0 && i != 21)
                         result |= 1;
+                      regfree (&regex);
                     }
-                  regfree (&regex);
                 }
 
                 if (! setlocale (LC_ALL, "C"))
@@ -139,9 +142,13 @@ AC_DEFUN([gl_REGEX],
             s = re_compile_pattern ("a[^x]b", 6, &regex);
             if (s)
               result |= 2;
-            /* This should fail, but succeeds for glibc-2.5.  */
-            else if (re_search (&regex, "a\nb", 3, 0, 3, &regs) != -1)
-              result |= 2;
+            else
+              {
+                /* This should fail, but succeeds for glibc-2.5.  */
+                if (re_search (&regex, "a\nb", 3, 0, 3, &regs) != -1)
+                  result |= 2;
+                regfree (&regex);
+              }
 
             /* This regular expression is from Spencer ere test number 75
                in grep-2.3.  */
@@ -153,7 +160,10 @@ AC_DEFUN([gl_REGEX],
             s = re_compile_pattern ("a[[:@:>@:]]b\n", 11, &regex);
             /* This should fail with _Invalid character class name_ error.  */
             if (!s)
-              result |= 4;
+              {
+                result |= 4;
+                regfree (&regex);
+              }
 
             /* Ensure that [b-a] is diagnosed as invalid, when
                using RE_NO_EMPTY_RANGES. */
@@ -161,13 +171,18 @@ AC_DEFUN([gl_REGEX],
             memset (&regex, 0, sizeof regex);
             s = re_compile_pattern ("a[b-a]", 6, &regex);
             if (s == 0)
-              result |= 8;
+              {
+                result |= 8;
+                regfree (&regex);
+              }
 
             /* This should succeed, but does not for glibc-2.1.3.  */
             memset (&regex, 0, sizeof regex);
             s = re_compile_pattern ("{1", 2, &regex);
             if (s)
               result |= 8;
+            else
+              regfree (&regex);
 
             /* The following example is derived from a problem report
                against gawk from Jorge Stolfi <stolfi@ic.unicamp.br>.  */
@@ -175,17 +190,35 @@ AC_DEFUN([gl_REGEX],
             s = re_compile_pattern ("[an\371]*n", 7, &regex);
             if (s)
               result |= 8;
-            /* This should match, but does not for glibc-2.2.1.  */
-            else if (re_match (&regex, "an", 2, 0, &regs) != 2)
-              result |= 8;
+            else
+              {
+                /* This should match, but does not for glibc-2.2.1.  */
+                if (re_match (&regex, "an", 2, 0, &regs) != 2)
+                  result |= 8;
+                else
+                  {
+                    free (regs.start);
+                    free (regs.end);
+                  }
+                regfree (&regex);
+              }
 
             memset (&regex, 0, sizeof regex);
             s = re_compile_pattern ("x", 1, &regex);
             if (s)
               result |= 8;
-            /* glibc-2.2.93 does not work with a negative RANGE argument.  */
-            else if (re_search (&regex, "wxy", 3, 2, -2, &regs) != 1)
-              result |= 8;
+            else
+              {
+                /* glibc-2.2.93 does not work with a negative RANGE argument.  */
+                if (re_search (&regex, "wxy", 3, 2, -2, &regs) != 1)
+                  result |= 8;
+                else
+                  {
+                    free (regs.start);
+                    free (regs.end);
+                  }
+                regfree (&regex);
+              }
 
             /* The version of regex.c in older versions of gnulib
                ignored RE_ICASE.  Detect that problem too.  */
@@ -194,8 +227,17 @@ AC_DEFUN([gl_REGEX],
             s = re_compile_pattern ("x", 1, &regex);
             if (s)
               result |= 16;
-            else if (re_search (&regex, "WXY", 3, 0, 3, &regs) < 0)
-              result |= 16;
+            else
+              {
+                if (re_search (&regex, "WXY", 3, 0, 3, &regs) < 0)
+                  result |= 16;
+                else
+                  {
+                    free (regs.start);
+                    free (regs.end);
+                  }
+                regfree (&regex);
+              }
 
             /* Catch a bug reported by Vin Shelton in
                https://lists.gnu.org/r/bug-coreutils/2007-06/msg00089.html
@@ -207,6 +249,8 @@ AC_DEFUN([gl_REGEX],
             s = re_compile_pattern ("[[:alnum:]_-]\\\\+$", 16, &regex);
             if (s)
               result |= 32;
+            else
+              regfree (&regex);
 
             /* REG_STARTEND was added to glibc on 2004-01-15.
                Reject older versions.  */
@@ -221,8 +265,14 @@ AC_DEFUN([gl_REGEX],
             re_set_syntax (RE_SYNTAX_POSIX_EGREP);
             memset (&regex, 0, sizeof regex);
             s = re_compile_pattern ("0|()0|\\1|0", 10, &regex);
-            if (!s || strcmp (s, "Invalid back reference"))
+            if (!s)
               result |= 64;
+            else
+              {
+                if (strcmp (s, "Invalid back reference"))
+                  result |= 64;
+                regfree (&regex);
+              }
 
 #if 0
             /* It would be nice to reject hosts whose regoff_t values are too



  parent reply	other threads:[~2020-05-21 18:31 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-18 10:43 Fix memleak in getdelim.m4 Tim Rühsen
2020-05-18 11:50 ` Bruno Haible
2020-05-18 18:21   ` Tim Rühsen
2020-05-18 19:44     ` Bruno Haible
2020-05-19 21:47       ` Tim Rühsen
2020-05-19 22:46         ` Bruno Haible
2020-05-20 18:45           ` Tim Rühsen
2020-05-21 14:22             ` relicense module 'group-member' Bruno Haible
2020-05-21 14:44               ` Eric Blake
2020-05-21 15:10                 ` Jim Meyering
2020-05-21 19:46                   ` Bruno Haible
2020-05-21 19:27               ` Paul Eggert
2020-05-21 14:23             ` Fix memleak in getdelim.m4 Bruno Haible
2020-05-20 21:59       ` Tim Rühsen
2020-05-21 14:26         ` Bruno Haible
2020-05-21 16:11           ` Tim Rühsen
2020-05-21 19:31             ` Bruno Haible
2020-05-22 14:03               ` Tim Rühsen
2020-05-22 15:25                 ` Bruno Haible
2020-05-22 20:46                   ` Tim Rühsen
2020-05-23 17:51                     ` Fix exponentl.m4 test Bruno Haible
2020-05-23 18:48                     ` Fix calloc.m4 test Bruno Haible
2020-05-23 20:26                       ` Paul Eggert
2020-05-23 21:53                         ` Bruno Haible
2020-05-24  0:51                           ` Paul Eggert
2020-05-24  7:53                             ` Bruno Haible
2020-06-06  8:19                       ` Bruno Haible
2020-05-23 19:18                     ` Fix invalid use of __builtin_isnanf and __builtin_isnanl Bruno Haible
2020-05-23 20:18                     ` Fix calloc-gnu configure results Bruno Haible
2020-05-23 20:47                     ` Fix memleak in getdelim.m4 Bruno Haible
2020-05-24  8:39                       ` Tim Rühsen
2020-05-21 15:15         ` SA_RESETHAND Bruno Haible
2020-05-21 20:10           ` SA_RESETHAND Paul Eggert
2020-05-21 20:59             ` SA_RESETHAND Bruno Haible
2020-05-21 15:22         ` Fix sanitizer error in fchownat.m4 Bruno Haible
2020-05-21 17:42         ` Fix memleak in glob.m4 Bruno Haible
2020-05-21 18:31         ` Bruno Haible [this message]
2020-05-21 18:40         ` Fix memleak in getdelim.m4 Bruno Haible
2020-05-21 19:30           ` Paul Eggert
2020-05-21 19:38             ` Bruno Haible
2020-05-21 14:32 ` Bruno Haible

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3584836.D8lSGNcFip@omega \
    --to=bruno@clisp.org \
    --cc=bug-gnulib@gnu.org \
    --cc=tim.ruehsen@gmx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).