[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Fix memleak in regex.m4
From: |
Bruno Haible |
Subject: |
Fix memleak in regex.m4 |
Date: |
Thu, 21 May 2020 20:31:14 +0200 |
User-agent: |
KMail/5.1.3 (Linux/4.4.0-177-generic; KDE/5.18.0; x86_64; ; ) |
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 <address@hidden>
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, ®ex);
if (s)
result |= 1;
- else if (re_search (®ex, data, sizeof data - 1,
- 0, sizeof data - 1, ®s)
- != -1)
- result |= 1;
- regfree (®ex);
+ else
+ {
+ if (re_search (®ex, data, sizeof data - 1,
+ 0, sizeof data - 1, ®s)
+ != -1)
+ result |= 1;
+ regfree (®ex);
+ }
}
{
@@ -125,8 +128,8 @@ AC_DEFUN([gl_REGEX],
0, sizeof data - 1, 0);
if (i != 0 && i != 21)
result |= 1;
+ regfree (®ex);
}
- regfree (®ex);
}
if (! setlocale (LC_ALL, "C"))
@@ -139,9 +142,13 @@ AC_DEFUN([gl_REGEX],
s = re_compile_pattern ("a[^x]b", 6, ®ex);
if (s)
result |= 2;
- /* This should fail, but succeeds for glibc-2.5. */
- else if (re_search (®ex, "a\nb", 3, 0, 3, ®s) != -1)
- result |= 2;
+ else
+ {
+ /* This should fail, but succeeds for glibc-2.5. */
+ if (re_search (®ex, "a\nb", 3, 0, 3, ®s) != -1)
+ result |= 2;
+ regfree (®ex);
+ }
/* 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, ®ex);
/* This should fail with _Invalid character class name_ error. */
if (!s)
- result |= 4;
+ {
+ result |= 4;
+ regfree (®ex);
+ }
/* Ensure that [b-a] is diagnosed as invalid, when
using RE_NO_EMPTY_RANGES. */
@@ -161,13 +171,18 @@ AC_DEFUN([gl_REGEX],
memset (®ex, 0, sizeof regex);
s = re_compile_pattern ("a[b-a]", 6, ®ex);
if (s == 0)
- result |= 8;
+ {
+ result |= 8;
+ regfree (®ex);
+ }
/* This should succeed, but does not for glibc-2.1.3. */
memset (®ex, 0, sizeof regex);
s = re_compile_pattern ("{1", 2, ®ex);
if (s)
result |= 8;
+ else
+ regfree (®ex);
/* The following example is derived from a problem report
against gawk from Jorge Stolfi <address@hidden>. */
@@ -175,17 +190,35 @@ AC_DEFUN([gl_REGEX],
s = re_compile_pattern ("[an\371]*n", 7, ®ex);
if (s)
result |= 8;
- /* This should match, but does not for glibc-2.2.1. */
- else if (re_match (®ex, "an", 2, 0, ®s) != 2)
- result |= 8;
+ else
+ {
+ /* This should match, but does not for glibc-2.2.1. */
+ if (re_match (®ex, "an", 2, 0, ®s) != 2)
+ result |= 8;
+ else
+ {
+ free (regs.start);
+ free (regs.end);
+ }
+ regfree (®ex);
+ }
memset (®ex, 0, sizeof regex);
s = re_compile_pattern ("x", 1, ®ex);
if (s)
result |= 8;
- /* glibc-2.2.93 does not work with a negative RANGE argument. */
- else if (re_search (®ex, "wxy", 3, 2, -2, ®s) != 1)
- result |= 8;
+ else
+ {
+ /* glibc-2.2.93 does not work with a negative RANGE argument.
*/
+ if (re_search (®ex, "wxy", 3, 2, -2, ®s) != 1)
+ result |= 8;
+ else
+ {
+ free (regs.start);
+ free (regs.end);
+ }
+ regfree (®ex);
+ }
/* 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, ®ex);
if (s)
result |= 16;
- else if (re_search (®ex, "WXY", 3, 0, 3, ®s) < 0)
- result |= 16;
+ else
+ {
+ if (re_search (®ex, "WXY", 3, 0, 3, ®s) < 0)
+ result |= 16;
+ else
+ {
+ free (regs.start);
+ free (regs.end);
+ }
+ regfree (®ex);
+ }
/* 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, ®ex);
if (s)
result |= 32;
+ else
+ regfree (®ex);
/* 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 (®ex, 0, sizeof regex);
s = re_compile_pattern ("0|()0|\\1|0", 10, ®ex);
- if (!s || strcmp (s, "Invalid back reference"))
+ if (!s)
result |= 64;
+ else
+ {
+ if (strcmp (s, "Invalid back reference"))
+ result |= 64;
+ regfree (®ex);
+ }
#if 0
/* It would be nice to reject hosts whose regoff_t values are too
- Re: Fix calloc.m4 test, (continued)
- Re: Fix calloc.m4 test, Bruno Haible, 2020/05/24
- Fix invalid use of __builtin_isnanf and __builtin_isnanl, Bruno Haible, 2020/05/23
- Fix calloc-gnu configure results, Bruno Haible, 2020/05/23
- Re: Fix memleak in getdelim.m4, Bruno Haible, 2020/05/23
- Re: Fix memleak in getdelim.m4, Tim Rühsen, 2020/05/24
- SA_RESETHAND, Bruno Haible, 2020/05/21
- Re: SA_RESETHAND, Paul Eggert, 2020/05/21
- Re: SA_RESETHAND, Bruno Haible, 2020/05/21
- Re: Fix sanitizer error in fchownat.m4, Bruno Haible, 2020/05/21
- Fix memleak in glob.m4, Bruno Haible, 2020/05/21
- Fix memleak in regex.m4,
Bruno Haible <=
- Re: Fix memleak in getdelim.m4, Bruno Haible, 2020/05/21
- Re: Fix memleak in getdelim.m4, Paul Eggert, 2020/05/21
- Re: Fix memleak in getdelim.m4, Bruno Haible, 2020/05/21
Re: Fix memleak in getdelim.m4, Bruno Haible, 2020/05/21