]> git.ipfire.org Git - thirdparty/gnulib.git/commitdiff
regex-tests: minor memory cleanup
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 6 May 2026 17:18:48 +0000 (10:18 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Wed, 6 May 2026 17:18:48 +0000 (10:18 -0700)
* tests/test-regex.c (main): No need for regex to be static,
and making it auto is more likely to catch memory leaks in
debugging platforms.  Clean up after its last use.
Omit unnecessary setting/freeing of regs that doesn’t test anything.

ChangeLog
tests/test-regex.c

index 2ab56a2f592844c775748556dbdbb0459f822163..0aef212c361a60ee45fc0521d333f2ed3580a7b7 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2026-05-06  Paul Eggert  <eggert@cs.ucla.edu>
+
+       regex-tests: minor memory cleanup
+       * tests/test-regex.c (main): No need for regex to be static,
+       and making it auto is more likely to catch memory leaks in
+       debugging platforms.  Clean up after its last use.
+       Omit unnecessary setting/freeing of regs that doesn’t test anything.
+
 2026-05-06  Bruno Haible  <bruno@clisp.org>
 
        pthread-h: Fix compilation error in C++ mode on mingw.
index ae4b34a30055a2704eb20886715b5ab1caae7037..7b61895744e9f8055f822ecec4e415532cbe8f0b 100644 (file)
@@ -166,7 +166,7 @@ bug_regex11 (void)
 int
 main (void)
 {
-  static struct re_pattern_buffer regex;
+  struct re_pattern_buffer regex;
   unsigned char folded_chars[UCHAR_MAX + 1];
   const char *s;
   struct re_registers regs;
@@ -237,15 +237,12 @@ main (void)
             report_error ("%s: %s", pat, s);
           else
             {
-              memset (&regs, 0, sizeof regs);
               int ret = re_search (&regex, data, sizeof data - 1,
                                    0, sizeof data - 1, NULL);
               if (ret != 0 && ret != 21)
                 report_error ("re_search '%s' on '%s' returned %d",
                               pat, data, ret);
               regfree (&regex);
-              free (regs.start);
-              free (regs.end);
             }
         }
 
@@ -515,8 +512,15 @@ main (void)
   memset (&regex, 0, sizeof regex);
   static char const pat_badback[] = "0|()0|\\1|0";
   s = re_compile_pattern (pat_badback, sizeof pat_badback, &regex);
-  if (!s && re_search (&regex, "x", 1, 0, 1, &regs) != -1)
-    s = "mishandled invalid back reference";
+  if (!s)
+    {
+      memset (&regs, 0, sizeof regs);
+      if (re_search (&regex, "x", 1, 0, 1, &regs) != -1)
+        s = "mishandled invalid back reference";
+      regfree (&regex);
+      free (regs.start);
+      free (regs.end);
+    }
   if (s && !streq (s, "Invalid back reference"))
     report_error ("%s: %s", pat_badback, s);