about summary refs log tree commit diff
path: root/posix
diff options
context:
space:
mode:
Diffstat (limited to 'posix')
-rw-r--r--posix/Makefile9
-rw-r--r--posix/bug-regex2.c53
-rw-r--r--posix/regex.c62
3 files changed, 90 insertions, 34 deletions
diff --git a/posix/Makefile b/posix/Makefile
index 3b72aa2617..9b2bd10e8e 100644
--- a/posix/Makefile
+++ b/posix/Makefile
@@ -69,7 +69,7 @@ tests		:= tstgetopt testfnm runtests runptests	     \
 		   tst-preadwrite tst-preadwrite64 test-vfork regexbug1 \
 		   tst-getlogin tst-mmap tst-getaddrinfo tst-truncate \
 		   tst-truncate64 tst-fork tst-fnmatch tst-regexloc tst-dir \
-		   tst-chmod bug-regex1
+		   tst-chmod bug-regex1 bug-regex2
 ifeq (yes,$(build-shared))
 test-srcs	:= globtest
 tests           += wordexp-test tst-exec tst-spawn
@@ -137,10 +137,15 @@ endif
 
 # Run a test on the header files we use.
 # XXX Please note that for now we ignore the result of this test.
-tests: $(objpfx)annexc.out
+tests: $(objpfx)annexc.out $(objpfx)bug-regex2-mem
 $(objpfx)annexc.out: $(objpfx)annexc
 	-$(dir $<)$(notdir $<) '$(CC)' \
 			       '-I../include -I.. $(+sysdep-includes) $(sysincludes)' > $@
 
 $(objpfx)annexc: annexc.c
 	$(native-compile)
+
+bug-regex2-ENV = MALLOC_TRACE=$(objpfx)bug-regex2.mtrace
+
+$(objpfx)bug-regex2-mem: $(objpfx)bug-regex2.out
+	$(common-objpfx)malloc/mtrace $(objpfx)bug-regex2.mtrace > $@
diff --git a/posix/bug-regex2.c b/posix/bug-regex2.c
new file mode 100644
index 0000000000..8b0dca1426
--- /dev/null
+++ b/posix/bug-regex2.c
@@ -0,0 +1,53 @@
+/* Test for memory handling in regex.
+   Copyright (C) 2001 Free Software Foundation, Inc.
+   Contributed by Ulrich Drepper <drepper@redhat.com>, 2001.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+#include <sys/types.h>
+#include <mcheck.h>
+#include <regex.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+
+static const char text[] = "This is a test; this is a test";
+
+int
+main (void)
+{
+  regex_t re;
+  regmatch_t rm[2];
+  int n;
+
+  mtrace ();
+
+  n = regcomp (&re, "a test", REG_EXTENDED);
+  if (n != 0)
+    {
+      char buf[500];
+      regerror (n, &re, buf, sizeof (buf));
+      printf ("regcomp failed: %s\n", buf);
+      exit (1);
+    }
+
+  for (n = 0; n < 20; ++n)
+    regexec (&re, text, 2, rm, 0);
+
+  regfree (&re);
+
+  return 0;
+}
diff --git a/posix/regex.c b/posix/regex.c
index 63f072afac..2b1a62b799 100644
--- a/posix/regex.c
+++ b/posix/regex.c
@@ -1174,7 +1174,7 @@ printchar (c)
 
 static size_t convert_mbs_to_wcs (CHAR_TYPE *dest, const unsigned char* src,
 				  size_t len, int *offset_buffer,
-				  int *is_binary);
+				  char *is_binary);
 static size_t
 convert_mbs_to_wcs (dest, src, len, offset_buffer, is_binary)
      CHAR_TYPE *dest;
@@ -1191,7 +1191,7 @@ convert_mbs_to_wcs (dest, src, len, offset_buffer, is_binary)
 	  	        = {0, 3, 4, 6}
      */
      int *offset_buffer;
-     int *is_binary;
+     char *is_binary;
 {
   wchar_t *pdest = dest;
   const unsigned char *psrc = src;
@@ -2283,9 +2283,9 @@ regex_compile (pattern, size, syntax, bufp)
   /* offset buffer for optimizatoin. See convert_mbs_to_wc.  */
   int *mbs_offset = NULL;
   /* It hold whether each wchar_t is binary data or not.  */
-  int *is_binary = NULL;
+  char *is_binary = NULL;
   /* A flag whether exactn is handling binary data or not.  */
-  int is_exactn_bin = FALSE;
+  char is_exactn_bin = FALSE;
 #endif /* MBS_SUPPORT */
 
   /* A random temporary spot in PATTERN.  */
@@ -2345,7 +2345,7 @@ regex_compile (pattern, size, syntax, bufp)
   /* Initialize the wchar_t PATTERN and offset_buffer.  */
   p = pend = pattern = TALLOC(csize, CHAR_TYPE);
   mbs_offset = TALLOC(csize + 1, int);
-  is_binary = TALLOC(csize + 1, int);
+  is_binary = TALLOC(csize + 1, char);
   if (pattern == NULL || mbs_offset == NULL || is_binary == NULL)
     {
       if (pattern) free(pattern);
@@ -5194,8 +5194,6 @@ weak_alias (__re_search_2, re_search_2)
     FREE_VAR (string2);							\
     FREE_VAR (mbs_offset1);						\
     FREE_VAR (mbs_offset2);						\
-    FREE_VAR (is_binary1);						\
-    FREE_VAR (is_binary2);						\
   } while (0)
 # else /* not MBS_SUPPORT */
 #  define FREE_VARIABLES()						\
@@ -5213,17 +5211,16 @@ weak_alias (__re_search_2, re_search_2)
   } while (0)
 # endif /* MBS_SUPPORT */
 #else
+# define FREE_VAR(var) if (var) free (var); var = NULL
 # ifdef MBS_SUPPORT
 #  define FREE_VARIABLES()						\
   do {									\
-    if (string1) free (string1);					\
-    if (string2) free (string2);					\
-    if (mbs_offset1) free (mbs_offset1);				\
-    if (mbs_offset2) free (mbs_offset2);				\
-    if (is_binary1) free (is_binary1);					\
-    if (is_binary2) free (is_binary2);					\
+    FREE_VAR (string1);							\
+    FREE_VAR (string2);							\
+    FREE_VAR (mbs_offset1);						\
+    FREE_VAR (mbs_offset2);						\
   } while (0)
-# eles
+# else
 #  define FREE_VARIABLES() ((void)0) /* Do nothing!  But inhibit gcc warning. */
 # endif /* MBS_SUPPORT */
 #endif /* not MATCH_MAY_ALLOCATE */
@@ -5373,7 +5370,7 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
   /* offset buffer for optimizatoin. See convert_mbs_to_wc.  */
   int *mbs_offset1 = NULL, *mbs_offset2 = NULL;
   /* They hold whether each wchar_t is binary data or not.  */
-  int *is_binary1 = NULL, *is_binary2 = NULL;
+  char *is_binary = NULL;
 #endif /* MBS_SUPPORT */
 
   /* Just past the end of the corresponding string.  */
@@ -5552,38 +5549,39 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
      fill them with converted string.  */
   if (csize1 != 0)
     {
-      string1 = TALLOC (csize1 + 1, CHAR_TYPE);
-      mbs_offset1 = TALLOC (csize1 + 1, int);
-      is_binary1 = TALLOC (csize1 + 1, int);
-      if (!string1 || !mbs_offset1 || !is_binary1)
+      string1 = REGEX_TALLOC (csize1 + 1, CHAR_TYPE);
+      mbs_offset1 = REGEX_TALLOC (csize1 + 1, int);
+      is_binary = REGEX_TALLOC (csize1 + 1, char);
+      if (!string1 || !mbs_offset1 || !is_binary)
 	{
-	  if (string1) free(string1);
-	  if (mbs_offset1) free(mbs_offset1);
-	  if (is_binary1) free(is_binary1);
+	  FREE_VAR (string1);
+	  FREE_VAR (mbs_offset1);
+	  FREE_VAR (is_binary);
 	  return -2;
 	}
       size1 = convert_mbs_to_wcs(string1, cstring1, csize1,
-				 mbs_offset1, is_binary1);
+				 mbs_offset1, is_binary);
       string1[size1] = L'\0'; /* for a sentinel  */
+      FREE_VAR (is_binary);
     }
   if (csize2 != 0)
     {
       string2 = REGEX_TALLOC (csize2 + 1, CHAR_TYPE);
       mbs_offset2 = REGEX_TALLOC (csize2 + 1, int);
-      is_binary2 = TALLOC (csize2 + 1, int);
-      if (!string2 || !mbs_offset2 || !is_binary2)
+      is_binary = REGEX_TALLOC (csize2 + 1, char);
+      if (!string2 || !mbs_offset2 || !is_binary)
 	{
-	  if (string1) free(string1);
-	  if (mbs_offset1) free(mbs_offset1);
-	  if (is_binary1) free(is_binary1);
-	  if (string2) free(string2);
-	  if (mbs_offset2) free(mbs_offset2);
-	  if (is_binary2) free(is_binary2);
+	  FREE_VAR (string1);
+	  FREE_VAR (mbs_offset1);
+	  FREE_VAR (string2);
+	  FREE_VAR (mbs_offset2);
+	  FREE_VAR (is_binary);
 	  return -2;
 	}
       size2 = convert_mbs_to_wcs(string2, cstring2, csize2,
-				 mbs_offset2, is_binary2);
+				 mbs_offset2, is_binary);
       string2[size2] = L'\0'; /* for a sentinel  */
+      FREE_VAR (is_binary);
     }
 
   /* We need to cast pattern to (wchar_t*), because we casted this compiled