From 9f21bac8fcc1395713e119652da3d5e901eb1cf8 Mon Sep 17 00:00:00 2001
From: aj <aj@138bc75d-0d04-0410-961f-82ee72b054a4>
Date: Mon, 30 Jul 2001 11:54:36 +0000
Subject: [PATCH] Merge from glibc:         * regex.c: Declare wcs functions
 only if compiling with         MBS_SUPPORT.         Don't use #elif for
 traditional C.

        * regex.c: Revamp memory allocation for WCHAR functions to
        not use too much stack.


git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@44471 138bc75d-0d04-0410-961f-82ee72b054a4
---
 libiberty/ChangeLog | 117 ++++++++++++++++++---------------
 libiberty/regex.c   | 153 +++++++++++++++++++++++++++++++-------------
 2 files changed, 174 insertions(+), 96 deletions(-)

diff --git a/libiberty/ChangeLog b/libiberty/ChangeLog
index a911e05357c8..9d5e08588210 100644
--- a/libiberty/ChangeLog
+++ b/libiberty/ChangeLog
@@ -1,3 +1,20 @@
+2001-07-30  Andreas Jaeger  <aj@suse.de>
+
+	* regex.c: Declare wcs functions only if compiling with
+	MBS_SUPPORT.
+	Don't use #elif for traditional C.
+
+2001-07-23  Ulrich Drepper  <drepper@redhat.com>
+
+	* regex.c: Revamp memory allocation for WCHAR functions to
+	not use too much stack.
+
+2001-07-30  Andreas Jaeger  <aj@suse.de>
+
+	* regex.c: Declare wcs functions only if compiling with
+	MBS_SUPPORT.
+	Don't use #elif for traditional C.
+
 2001-07-25  Daniel Jacobowitz  <drow@mvista.com>
 
 	* Makefile.in (regex.o): Add dependency on config.h.
@@ -299,12 +316,12 @@
 2000-11-04  Hans-Peter Nilsson  <hp@bitrange.com>
 
 	* hashtab.c (htab_expand): Change to return int.  Use calloc or
- 	xcalloc depending on htab->return_allocation_failure.  Return zero
- 	if calloc fails.
+	xcalloc depending on htab->return_allocation_failure.  Return zero
+	if calloc fails.
 	(htab_create): Update comment to cover memory allocation.
 	(htab_try_create): New.
 	(htab_find_slot_with_hash): Return NULL if htab_expand fails.
-  	Update comment to cover this.
+	Update comment to cover this.
 
 2000-11-03  Hans-Peter Nilsson  <hp@bitrange.com>
 
@@ -807,7 +824,7 @@ Tue May 30 15:07:52 2000  Jeffrey A Law  (law@cygnus.com)
 2000-05-23  Mike Stump  <mrs@wrs.com>
 
 	* Makefile.in (xmalloc.o): Add dependency for config.h, fixes make
- 	-j3.
+	-j3.
 
 2000-05-18  J. David Anglin  <dave@hiauly1.hia.nrc.ca>
 
@@ -1440,9 +1457,9 @@ Tue Dec 22 09:43:35 1998  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>
 	* choose-temp.c: Include stdlib.h.
 
 	* cplus-dem.c (demangle_arm_pt): Remove unused prototype.
-  	(snarf_numeric_literal): Constify first parameter.
-  	(code_for_qualifier): Avoid a gcc extension, make the parameter an
- 	int, not a char.
+	(snarf_numeric_literal): Constify first parameter.
+	(code_for_qualifier): Avoid a gcc extension, make the parameter an
+	int, not a char.
 	(demangle_qualifier): Likewise.
 	(demangle_signature): Cast the argument of a ctype function to
 	unsigned char.
@@ -1455,11 +1472,11 @@ Tue Dec 22 09:43:35 1998  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>
 	Cast the argument of a ctype function to unsigned char.
 
 	* floatformat.c (floatformat_to_double): Add explicit braces to
- 	avoid ambiguous `else'.
+	avoid ambiguous `else'.
 
 	* fnmatch.c (fnmatch): Change type of variables `c', `c1',
- 	`cstart' and `cend' to unsigned char.  Cast the argument of macro
- 	`FOLD', which uses ctype functions, to unsigned char.
+	`cstart' and `cend' to unsigned char.  Cast the argument of macro
+	`FOLD', which uses ctype functions, to unsigned char.
 
 	* objalloc.c (free): Add prototype.
 
@@ -1470,7 +1487,7 @@ Sun Dec 20 16:03:46 1998  Hans-Peter Nilsson  <hp@axis.se>
 Fri Dec 18 17:50:18 1998  David Taylor  <taylor@texas.cygnus.com>
 
 	* cplus-dem.c (demangle_arm_pt): remove declaration -- function
- 	doesn't exist.
+	doesn't exist.
 	(do_hpacc_template_literal): remove unused variable `i'.
 
 Fri Dec 18 16:11:43 EST 1998  Andrew MacLeod  <amacleod@cygnus.com>
@@ -1514,48 +1531,48 @@ Fri Dec  4 13:51:04 1998  David Taylor   <taylor@texas.cygnus.com>
 	* HP aCC demangling support.
 	* cplus-dem.c
 	(main): Remove default to HP style demangling, set to EDG
- 	demangling correctly when -edg specified; set the demangling style
- 	when user specifies 'edg'. Set strip_underscore to
- 	prepends_underscore, if not HPUXHPPA.  Set
- 	current_demangling_style to hp_demangling if HPUXHPPA.  Set
- 	current demangling style correctly if the switch is hp.  Read
- 	label correctly also in the HP style case.
+	demangling correctly when -edg specified; set the demangling style
+	when user specifies 'edg'. Set strip_underscore to
+	prepends_underscore, if not HPUXHPPA.  Set
+	current_demangling_style to hp_demangling if HPUXHPPA.  Set
+	current demangling style correctly if the switch is hp.  Read
+	label correctly also in the HP style case.
 	(work_stuff): add temp_start field; add field for volatile member
- 	function.
+	function.
 	(arm_pt): handle ARM_DEMANGLING and EDG_DEMANGLING styles; HP
- 	style for this case is the same as ARM.
+	style for this case is the same as ARM.
 	(demangle_args): handle EDG_DEMANGLING style; support HP style.
 	(demangle_arm_hp_template): new function. (It was
- 	demangle_arm_pt.); check and set value of temp_start field in
- 	multiple places. Also, when ceching for end of template args,
- 	check to see if at end of static member of template class.
+	demangle_arm_pt.); check and set value of temp_start field in
+	multiple places. Also, when ceching for end of template args,
+	check to see if at end of static member of template class.
 	(demangle_class): new local variable : save_class_name_end Don't
- 	include template args in string defining class.
+	include template args in string defining class.
 	(demangle_class_name): use demangel_arm_hp_template.
 	(demangle_function_name): handle case where demangling style is
- 	HP_DEMANGLING and currently point at an 'X' in the mangled name.
-  	Handle EDG_DEMANGLING style.  Handle constructor and destructor
- 	ops for HP style.
+	HP_DEMANGLING and currently point at an 'X' in the mangled name.
+	Handle EDG_DEMANGLING style.  Handle constructor and destructor
+	ops for HP style.
 	(demangle_prefix): handle EDG_DEMANGLING and ARM_DEMANGLING
- 	styles.  global destructor and constructor for HP style are same
- 	as for ARM style. Same for local variables.
+	styles.  global destructor and constructor for HP style are same
+	as for ARM style. Same for local variables.
 	(demangle_qualified): handle EDG_DEMANGLING style.
 	(demangle_signature): add case for volatile member function.  For
- 	cases '1' - '9' : initialize the temp_start field to -1 and handle
- 	the EDG_DEMANGLING style.  for case 'F' : handle EDG_DEMANGLING
- 	and AUTO_DEMANGLING styles.  If expecting a function and managed
- 	to demangle the funct args, then handle the LUCID_DEMANGLING,
- 	ARM_DEMANGLING, and EDG_DEMANGLING styles.  Add case for local
- 	class name after "Lnnn_ in HP style case. HP style too needs to
- 	forget types.  _nnn is OK for HP style, so don't report failure.
+	cases '1' - '9' : initialize the temp_start field to -1 and handle
+	the EDG_DEMANGLING style.  for case 'F' : handle EDG_DEMANGLING
+	and AUTO_DEMANGLING styles.  If expecting a function and managed
+	to demangle the funct args, then handle the LUCID_DEMANGLING,
+	ARM_DEMANGLING, and EDG_DEMANGLING styles.  Add case for local
+	class name after "Lnnn_ in HP style case. HP style too needs to
+	forget types.  _nnn is OK for HP style, so don't report failure.
 	(do_hpacc_template_const_value): new function. Handle template's
- 	value param for HP/aCC.
+	value param for HP/aCC.
 	(do_hpacc_template_literal): new function.  Handle a template's
- 	literal parameter for HP aCC.
+	literal parameter for HP aCC.
 	(recursively_demangle): new function
 	(snarf_numeric_literal): new function.
 	(usage): add 'edg' to the list of demangling styles; add hp switch
- 	to message.
+	to message.
 
 Sat Nov 28 17:25:22 1998  Christopher Faylor <cgf@cygnus.com>
 
@@ -1637,13 +1654,13 @@ Mon Nov  2 15:05:33 1998  Geoffrey Noer  <noer@cygnus.com>
 Mon Nov  2 10:22:01 1998  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>
 
 	* pexecute.c: Check HAVE_CONFIG_H, not IN_GCC, when determining
- 	whether to include config.h.  Possibly include unistd.h in the
- 	!IN_GCC case.  Define VFORK_STRING as a printable function call
- 	for error messages (either "vfork" or "fork".)  If HAVE_VFORK_H is
- 	defined, include vfork.h.  If VMS is defined, define vfork()
- 	appropriately.  Remove vfork check on USG, we're using autoconf.
-  	(pexecute): Set `errmsg_fmt' to VFORK_STRING instead of checking
- 	locally what string to use.
+	whether to include config.h.  Possibly include unistd.h in the
+	!IN_GCC case.  Define VFORK_STRING as a printable function call
+	for error messages (either "vfork" or "fork".)  If HAVE_VFORK_H is
+	defined, include vfork.h.  If VMS is defined, define vfork()
+	appropriately.  Remove vfork check on USG, we're using autoconf.
+	(pexecute): Set `errmsg_fmt' to VFORK_STRING instead of checking
+	locally what string to use.
 
 1998-10-26  Mark Mitchell  <mark@markmitchell.com>
 
@@ -1749,8 +1766,8 @@ Thu Oct  8 23:42:08 1998  Jeffrey A Law  (law@cygnus.com)
 Mon Sep  7 23:29:01 1998  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>
 
 	* mkstemp.c: Include config.h even when not IN_GCC.  Wrap header
- 	inclusions inside HAVE_*_H macros.  Include ansidecl.h when not
- 	IN_GCC.
+	inclusions inside HAVE_*_H macros.  Include ansidecl.h when not
+	IN_GCC.
 
 	* vasprintf.c: Include stdarg.h/varargs.h first.
 
@@ -2064,7 +2081,7 @@ Mon May  4 13:00:28 1998  Ian Lance Taylor  <ian@cygnus.com>
 	* config/mh-sysv4 (RANLIB, INSTALL): Don't define.
 	* config.table: Change config_shell to CONFIG_SHELL, and use
 	libiberty_topdir to find move-if-change.
- 	(m68k-apollo-bsd*, m68k-apollo-sysv*): Remove.
+	(m68k-apollo-bsd*, m68k-apollo-sysv*): Remove.
 	(i[3456]86-ncr-sysv4*, *-*-dgux*, hppa*-hp-bsd*): Remove.
 	(*-*-irix*, *-*-m88kbcs*, *-*-sysv*): Remove.
 	* Makefile.in (srcdir): Set to @srcdir@.
@@ -2281,7 +2298,7 @@ Wed Sep 24 00:31:59 1997  Felix Lee  <flee@yin.cygnus.com>
 
 	* asprintf.c: stdarg.h when ALMOST_STDC
 	* config/mh-windows (EXTRA_OFILES): add asprintf.o and
- 	strncasecmp.o.
+	strncasecmp.o.
 
 Thu Aug 28 14:27:15 1997  Andrew Cagney  <cagney@b1.cygnus.com>
 
@@ -4323,7 +4340,7 @@ Thu Jan 30 22:48:41 1992  Stu Grossman  (grossman at cygnus.com)
 
 Mon Jan 20 18:53:23 1992  Stu Grossman  (grossman at cygnus.com)
 
- 	* getopt.c, getopt1.c, ../include/getopt.h:  Get latest versions.
+	* getopt.c, getopt1.c, ../include/getopt.h:  Get latest versions.
 
 Sat Jan 18 16:53:01 1992  Fred Fish  (fnf at cygnus.com)
 
diff --git a/libiberty/regex.c b/libiberty/regex.c
index f06775237913..555d3bbf66aa 100644
--- a/libiberty/regex.c
+++ b/libiberty/regex.c
@@ -410,9 +410,6 @@ typedef char boolean;
 static reg_errcode_t byte_regex_compile _RE_ARGS ((const char *pattern, size_t size,
                                                    reg_syntax_t syntax,
                                                    struct re_pattern_buffer *bufp));
-static reg_errcode_t wcs_regex_compile _RE_ARGS ((const char *pattern, size_t size,
-                                                   reg_syntax_t syntax,
-                                                   struct re_pattern_buffer *bufp));
 
 static int byte_re_match_2_internal PARAMS ((struct re_pattern_buffer *bufp,
 					     const char *string1, int size1,
@@ -420,6 +417,19 @@ static int byte_re_match_2_internal PARAMS ((struct re_pattern_buffer *bufp,
 					     int pos,
 					     struct re_registers *regs,
 					     int stop));
+static int byte_re_search_2 PARAMS ((struct re_pattern_buffer *bufp,
+				     const char *string1, int size1,
+				     const char *string2, int size2,
+				     int startpos, int range,
+				     struct re_registers *regs, int stop));
+static int byte_re_compile_fastmap PARAMS ((struct re_pattern_buffer *bufp));
+
+#ifdef MBS_SUPPORT
+static reg_errcode_t wcs_regex_compile _RE_ARGS ((const char *pattern, size_t size,
+                                                   reg_syntax_t syntax,
+                                                   struct re_pattern_buffer *bufp));
+
+
 static int wcs_re_match_2_internal PARAMS ((struct re_pattern_buffer *bufp,
 					    const char *cstring1, int csize1,
 					    const char *cstring2, int csize2,
@@ -429,19 +439,13 @@ static int wcs_re_match_2_internal PARAMS ((struct re_pattern_buffer *bufp,
 					    wchar_t *string1, int size1,
 					    wchar_t *string2, int size2,
 					    int *mbs_offset1, int *mbs_offset2));
-static int byte_re_search_2 PARAMS ((struct re_pattern_buffer *bufp,
-				     const char *string1, int size1,
-				     const char *string2, int size2,
-				     int startpos, int range,
-				     struct re_registers *regs, int stop));
 static int wcs_re_search_2 PARAMS ((struct re_pattern_buffer *bufp,
 				    const char *string1, int size1,
 				    const char *string2, int size2,
 				    int startpos, int range,
 				    struct re_registers *regs, int stop));
-static int byte_re_compile_fastmap PARAMS ((struct re_pattern_buffer *bufp));
 static int wcs_re_compile_fastmap PARAMS ((struct re_pattern_buffer *bufp));
-
+#endif
 
 /* These are the command codes that appear in compiled regular
    expressions.  Some opcodes are followed by argument bytes.  A
@@ -611,29 +615,31 @@ typedef enum
 # define PREFIX(name) byte_##name
 # define ARG_PREFIX(name) name
 # define PUT_CHAR(c) putchar (c)
-#elif defined WCHAR
-# define CHAR_T wchar_t
-# define UCHAR_T wchar_t
-# define COMPILED_BUFFER_VAR wc_buffer
-# define OFFSET_ADDRESS_SIZE 1 /* the size which STORE_NUMBER macro use */
-# define CHAR_CLASS_SIZE ((__alignof__(wctype_t)+sizeof(wctype_t))/sizeof(CHAR_T)+1)
-# define PREFIX(name) wcs_##name
-# define ARG_PREFIX(name) c##name
-/* Should we use wide stream??  */
-# define PUT_CHAR(c) printf ("%C", c);
-# define TRUE 1
-# define FALSE 0
 #else
-# ifdef MBS_SUPPORT
-#  define WCHAR
+# ifdef WCHAR
+#  define CHAR_T wchar_t
+#  define UCHAR_T wchar_t
+#  define COMPILED_BUFFER_VAR wc_buffer
+#  define OFFSET_ADDRESS_SIZE 1 /* the size which STORE_NUMBER macro use */
+#  define CHAR_CLASS_SIZE ((__alignof__(wctype_t)+sizeof(wctype_t))/sizeof(CHAR_T)+1)
+#  define PREFIX(name) wcs_##name
+#  define ARG_PREFIX(name) c##name
+/* Should we use wide stream??  */
+#  define PUT_CHAR(c) printf ("%C", c);
+#  define TRUE 1
+#  define FALSE 0
+# else
+#  ifdef MBS_SUPPORT
+#   define WCHAR
+#   define INSIDE_RECURSION
+#   include "regex.c"
+#   undef INSIDE_RECURSION
+#  endif
+#  define BYTE
 #  define INSIDE_RECURSION
 #  include "regex.c"
 #  undef INSIDE_RECURSION
 # endif
-# define BYTE
-# define INSIDE_RECURSION
-# include "regex.c"
-# undef INSIDE_RECURSION
 #endif
 
 #ifdef INSIDE_RECURSION
@@ -5076,16 +5082,35 @@ weak_alias (__re_search_2, re_search_2)
 #endif
 
 #ifdef WCHAR
-# define FREE_WCS_BUFFERS()    \
-  do {                         \
-    FREE_VAR (string1);                \
-    FREE_VAR (string2);                \
-    FREE_VAR (mbs_offset1);    \
-    FREE_VAR (mbs_offset2);    \
+# define MAX_ALLOCA_SIZE	2000
+
+# define FREE_WCS_BUFFERS() \
+  do {									      \
+    if (size1 > MAX_ALLOCA_SIZE)					      \
+      {									      \
+	free (wcs_string1);						      \
+	free (mbs_offset1);						      \
+      }									      \
+    else								      \
+      {									      \
+	FREE_VAR (wcs_string1);						      \
+	FREE_VAR (mbs_offset1);						      \
+      }									      \
+    if (size2 > MAX_ALLOCA_SIZE) 					      \
+      {									      \
+	free (wcs_string2);						      \
+	free (mbs_offset2);						      \
+      }									      \
+    else								      \
+      {									      \
+	FREE_VAR (wcs_string2);						      \
+	FREE_VAR (mbs_offset2);						      \
+      }									      \
   } while (0)
 
 #endif
 
+
 static int
 PREFIX(re_search_2) (bufp, string1, size1, string2, size2, startpos, range,
 		     regs, stop)
@@ -5160,36 +5185,72 @@ PREFIX(re_search_2) (bufp, string1, size1, string2, size2, startpos, range,
      fill them with converted string.  */
   if (size1 != 0)
     {
-      wcs_string1 = REGEX_TALLOC (size1 + 1, CHAR_T);
-      mbs_offset1 = REGEX_TALLOC (size1 + 1, int);
-      is_binary = REGEX_TALLOC (size1 + 1, char);
+      if (size1 > MAX_ALLOCA_SIZE)
+	{
+	  wcs_string1 = TALLOC (size1 + 1, CHAR_T);
+	  mbs_offset1 = TALLOC (size1 + 1, int);
+	  is_binary = TALLOC (size1 + 1, char);
+	}
+      else
+	{
+	  wcs_string1 = REGEX_TALLOC (size1 + 1, CHAR_T);
+	  mbs_offset1 = REGEX_TALLOC (size1 + 1, int);
+	  is_binary = REGEX_TALLOC (size1 + 1, char);
+	}
       if (!wcs_string1 || !mbs_offset1 || !is_binary)
 	{
-	  FREE_VAR (wcs_string1);
-	  FREE_VAR (mbs_offset1);
-	  FREE_VAR (is_binary);
+	  if (size1 > MAX_ALLOCA_SIZE)
+	    {
+	      free (wcs_string1);
+	      free (mbs_offset1);
+	      free (is_binary);
+	    }
+	  else
+	    {
+	      FREE_VAR (wcs_string1);
+	      FREE_VAR (mbs_offset1);
+	      FREE_VAR (is_binary);
+	    }
 	  return -2;
 	}
       wcs_size1 = convert_mbs_to_wcs(wcs_string1, string1, size1,
 				     mbs_offset1, is_binary);
       wcs_string1[wcs_size1] = L'\0'; /* for a sentinel  */
-      FREE_VAR (is_binary);
+      if (size1 > MAX_ALLOCA_SIZE)
+	free (is_binary);
+      else
+	FREE_VAR (is_binary);
     }
   if (size2 != 0)
     {
-      wcs_string2 = REGEX_TALLOC (size2 + 1, CHAR_T);
-      mbs_offset2 = REGEX_TALLOC (size2 + 1, int);
-      is_binary = REGEX_TALLOC (size2 + 1, char);
+      if (size2 > MAX_ALLOCA_SIZE)
+	{
+	  wcs_string2 = TALLOC (size2 + 1, CHAR_T);
+	  mbs_offset2 = TALLOC (size2 + 1, int);
+	  is_binary = TALLOC (size2 + 1, char);
+	}
+      else
+	{
+	  wcs_string2 = REGEX_TALLOC (size2 + 1, CHAR_T);
+	  mbs_offset2 = REGEX_TALLOC (size2 + 1, int);
+	  is_binary = REGEX_TALLOC (size2 + 1, char);
+	}
       if (!wcs_string2 || !mbs_offset2 || !is_binary)
 	{
 	  FREE_WCS_BUFFERS ();
-	  FREE_VAR (is_binary);
+	  if (size2 > MAX_ALLOCA_SIZE)
+	    free (is_binary);
+	  else
+	    FREE_VAR (is_binary);
 	  return -2;
 	}
       wcs_size2 = convert_mbs_to_wcs(wcs_string2, string2, size2,
 				     mbs_offset2, is_binary);
       wcs_string2[wcs_size2] = L'\0'; /* for a sentinel  */
-      FREE_VAR (is_binary);
+      if (size2 > MAX_ALLOCA_SIZE)
+	free (is_binary);
+      else
+	FREE_VAR (is_binary);
     }
 #endif /* WCHAR */
 
-- 
GitLab