1feee8
commit 86a701a20479dfbc23540b3143fd5b28660a2447
1feee8
Author: Paul Eggert <eggert@cs.ucla.edu>
1feee8
Date:   Tue Sep 21 07:47:45 2021 -0700
1feee8
1feee8
    regex: copy back from Gnulib
1feee8
    
1feee8
    Copy regex-related files back from Gnulib, to fix a problem with
1feee8
    static checking of regex calls noted by Martin Sebor.  This merges the
1feee8
    following changes:
1feee8
    
1feee8
    * New macro __attribute_nonnull__ in misc/sys/cdefs.h, for use later
1feee8
    when copying other files back from Gnulib.
1feee8
    
1feee8
    * Use __GNULIB_CDEFS instead of __GLIBC__ when deciding
1feee8
    whether to include bits/wordsize.h etc.
1feee8
    
1feee8
    * Avoid duplicate entries in epsilon closure table.
1feee8
    
1feee8
    * New regex.h macro _REGEX_NELTS to let regexec say that its pmatch
1feee8
    arg should contain nmatch elts.  Use that for regexec, instead of
1feee8
    __attr_access (which is incorrect).
1feee8
    
1feee8
    * New regex.h macro _Attr_access_ which is like __attr_access except
1feee8
    portable to non-glibc platforms.
1feee8
    
1feee8
    * Add some DEBUG_ASSERTs to pacify gcc -fanalyzer and to catch
1feee8
    recently-fixed performance bugs if they recur.
1feee8
    
1feee8
    * Add Gnulib-specific stuff to port the dynarray- and lock-using parts
1feee8
    of regex code to non-glibc platforms.
1feee8
    
1feee8
    * Fix glibc bug 11053.
1feee8
    
1feee8
    * Avoid some undefined behavior when popping an empty fail stack.
1feee8
    
1feee8
    (cherry picked from commit 0b5ca7c3e551e5502f3be3b06453324fe8604e82)
1feee8
1feee8
diff --git a/include/intprops.h b/include/intprops.h
1feee8
index 967e32ea0cbedd56..9d10028a5966c1c6 100644
1feee8
--- a/include/intprops.h
1feee8
+++ b/include/intprops.h
1feee8
@@ -133,7 +133,8 @@
1feee8
    operators might not yield numerically correct answers due to
1feee8
    arithmetic overflow.  They do not rely on undefined or
1feee8
    implementation-defined behavior.  Their implementations are simple
1feee8
-   and straightforward, but they are a bit harder to use than the
1feee8
+   and straightforward, but they are harder to use and may be less
1feee8
+   efficient than the INT_<op>_WRAPV, INT_<op>_OK, and
1feee8
    INT_<op>_OVERFLOW macros described below.
1feee8
 
1feee8
    Example usage:
1feee8
@@ -158,6 +159,9 @@
1feee8
    must have minimum value MIN and maximum MAX.  Unsigned types should
1feee8
    use a zero MIN of the proper type.
1feee8
 
1feee8
+   Because all arguments are subject to integer promotions, these
1feee8
+   macros typically do not work on types narrower than 'int'.
1feee8
+
1feee8
    These macros are tuned for constant MIN and MAX.  For commutative
1feee8
    operations such as A + B, they are also tuned for constant B.  */
1feee8
 
1feee8
@@ -339,9 +343,15 @@
1feee8
    arguments should not have side effects.
1feee8
 
1feee8
    The WRAPV macros are not constant expressions.  They support only
1feee8
-   +, binary -, and *.  Because the WRAPV macros convert the result,
1feee8
-   they report overflow in different circumstances than the OVERFLOW
1feee8
-   macros do.
1feee8
+   +, binary -, and *.
1feee8
+
1feee8
+   Because the WRAPV macros convert the result, they report overflow
1feee8
+   in different circumstances than the OVERFLOW macros do.  For
1feee8
+   example, in the typical case with 16-bit 'short' and 32-bit 'int',
1feee8
+   if A, B and R are all of type 'short' then INT_ADD_OVERFLOW (A, B)
1feee8
+   returns false because the addition cannot overflow after A and B
1feee8
+   are converted to 'int', whereas INT_ADD_WRAPV (A, B, &R) returns
1feee8
+   true or false depending on whether the sum fits into 'short'.
1feee8
 
1feee8
    These macros are tuned for their last input argument being a constant.
1feee8
 
1feee8
diff --git a/include/regex.h b/include/regex.h
1feee8
index 24eca2c297bb6043..34fb67d85536bcb9 100644
1feee8
--- a/include/regex.h
1feee8
+++ b/include/regex.h
1feee8
@@ -37,7 +37,8 @@ extern int __regcomp (regex_t *__preg, const char *__pattern, int __cflags);
1feee8
 libc_hidden_proto (__regcomp)
1feee8
 
1feee8
 extern int __regexec (const regex_t *__preg, const char *__string,
1feee8
-		      size_t __nmatch, regmatch_t __pmatch[], int __eflags);
1feee8
+		      size_t __nmatch, regmatch_t __pmatch[__nmatch],
1feee8
+		      int __eflags);
1feee8
 libc_hidden_proto (__regexec)
1feee8
 
1feee8
 extern size_t __regerror (int __errcode, const regex_t *__preg,
1feee8
diff --git a/misc/sys/cdefs.h b/misc/sys/cdefs.h
1feee8
index e0ecd9147ee3ce48..b166f3d209fe361f 100644
1feee8
--- a/misc/sys/cdefs.h
1feee8
+++ b/misc/sys/cdefs.h
1feee8
@@ -366,16 +366,18 @@
1feee8
 #endif
1feee8
 
1feee8
 /* The nonnull function attribute marks pointer parameters that
1feee8
-   must not be NULL.  */
1feee8
-#ifndef __nonnull
1feee8
+   must not be NULL.  This has the name __nonnull in glibc,
1feee8
+   and __attribute_nonnull__ in files shared with Gnulib to avoid
1feee8
+   collision with a different __nonnull in DragonFlyBSD 5.9.  */
1feee8
+#ifndef __attribute_nonnull__
1feee8
 # if __GNUC_PREREQ (3,3) || __glibc_has_attribute (__nonnull__)
1feee8
-#  define __nonnull(params) __attribute__ ((__nonnull__ params))
1feee8
+#  define __attribute_nonnull__(params) __attribute__ ((__nonnull__ params))
1feee8
 # else
1feee8
-#  define __nonnull(params)
1feee8
+#  define __attribute_nonnull__(params)
1feee8
 # endif
1feee8
-#elif !defined __GLIBC__
1feee8
-# undef __nonnull
1feee8
-# define __nonnull(params) _GL_ATTRIBUTE_NONNULL (params)
1feee8
+#endif
1feee8
+#ifndef __nonnull
1feee8
+# define __nonnull(params) __attribute_nonnull__ (params)
1feee8
 #endif
1feee8
 
1feee8
 /* The returns_nonnull function attribute marks the return type of the function
1feee8
@@ -541,9 +543,9 @@
1feee8
       [!!sizeof (struct { int __error_if_negative: (expr) ? 2 : -1; })]
1feee8
 #endif
1feee8
 
1feee8
-/* The #ifndef lets Gnulib avoid including these on non-glibc
1feee8
-   platforms, where the includes typically do not exist.  */
1feee8
-#ifdef __GLIBC__
1feee8
+/* Gnulib avoids including these, as they don't work on non-glibc or
1feee8
+   older glibc platforms.  */
1feee8
+#ifndef __GNULIB_CDEFS
1feee8
 # include <bits/wordsize.h>
1feee8
 # include <bits/long-double.h>
1feee8
 #endif
1feee8
diff --git a/posix/regcomp.c b/posix/regcomp.c
1feee8
index d93698ae78447b46..887e5b50684e22f5 100644
1feee8
--- a/posix/regcomp.c
1feee8
+++ b/posix/regcomp.c
1feee8
@@ -1695,12 +1695,14 @@ calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa, Idx node, bool root)
1feee8
   reg_errcode_t err;
1feee8
   Idx i;
1feee8
   re_node_set eclosure;
1feee8
-  bool ok;
1feee8
   bool incomplete = false;
1feee8
   err = re_node_set_alloc (&eclosure, dfa->edests[node].nelem + 1);
1feee8
   if (__glibc_unlikely (err != REG_NOERROR))
1feee8
     return err;
1feee8
 
1feee8
+  /* An epsilon closure includes itself.  */
1feee8
+  eclosure.elems[eclosure.nelem++] = node;
1feee8
+
1feee8
   /* This indicates that we are calculating this node now.
1feee8
      We reference this value to avoid infinite loop.  */
1feee8
   dfa->eclosures[node].nelem = -1;
1feee8
@@ -1753,10 +1755,6 @@ calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa, Idx node, bool root)
1feee8
 	  }
1feee8
       }
1feee8
 
1feee8
-  /* An epsilon closure includes itself.  */
1feee8
-  ok = re_node_set_insert (&eclosure, node);
1feee8
-  if (__glibc_unlikely (! ok))
1feee8
-    return REG_ESPACE;
1feee8
   if (incomplete && !root)
1feee8
     dfa->eclosures[node].nelem = 0;
1feee8
   else
1feee8
diff --git a/posix/regex.c b/posix/regex.c
1feee8
index 7296be0f08da88d8..d32863972c7bcdcf 100644
1feee8
--- a/posix/regex.c
1feee8
+++ b/posix/regex.c
1feee8
@@ -24,6 +24,7 @@
1feee8
 
1feee8
 # if __GNUC_PREREQ (4, 6)
1feee8
 #  pragma GCC diagnostic ignored "-Wsuggest-attribute=pure"
1feee8
+#  pragma GCC diagnostic ignored "-Wvla"
1feee8
 # endif
1feee8
 # if __GNUC_PREREQ (4, 3)
1feee8
 #  pragma GCC diagnostic ignored "-Wold-style-definition"
1feee8
diff --git a/posix/regex.h b/posix/regex.h
1feee8
index 14fb1d8364a11d29..adb69768ee520554 100644
1feee8
--- a/posix/regex.h
1feee8
+++ b/posix/regex.h
1feee8
@@ -522,6 +522,30 @@ typedef struct
1feee8
 
1feee8
 /* Declarations for routines.  */
1feee8
 
1feee8
+#ifndef _REGEX_NELTS
1feee8
+# if (defined __STDC_VERSION__ && 199901L <= __STDC_VERSION__ \
1feee8
+	&& !defined __STDC_NO_VLA__)
1feee8
+#  define _REGEX_NELTS(n) n
1feee8
+# else
1feee8
+#  define _REGEX_NELTS(n)
1feee8
+# endif
1feee8
+#endif
1feee8
+
1feee8
+#if defined __GNUC__ && 4 < __GNUC__ + (6 <= __GNUC_MINOR__)
1feee8
+# pragma GCC diagnostic push
1feee8
+# pragma GCC diagnostic ignored "-Wvla"
1feee8
+#endif
1feee8
+
1feee8
+#ifndef _Attr_access_
1feee8
+# ifdef __attr_access
1feee8
+#  define _Attr_access_(arg) __attr_access (arg)
1feee8
+# elif defined __GNUC__ && 10 <= __GNUC__
1feee8
+#  define _Attr_access_(x) __attribute__ ((__access__ x))
1feee8
+# else
1feee8
+#  define _Attr_access_(x)
1feee8
+# endif
1feee8
+#endif
1feee8
+
1feee8
 #ifdef __USE_GNU
1feee8
 /* Sets the current default syntax to SYNTAX, and return the old syntax.
1feee8
    You can also simply assign to the 're_syntax_options' variable.  */
1feee8
@@ -537,7 +561,7 @@ extern reg_syntax_t re_set_syntax (reg_syntax_t __syntax);
1feee8
    'regfree'.  */
1feee8
 extern const char *re_compile_pattern (const char *__pattern, size_t __length,
1feee8
 				       struct re_pattern_buffer *__buffer)
1feee8
-    __attr_access ((__read_only__, 1, 2));
1feee8
+    _Attr_access_ ((__read_only__, 1, 2));
1feee8
 
1feee8
 
1feee8
 /* Compile a fastmap for the compiled pattern in BUFFER; used to
1feee8
@@ -555,7 +579,7 @@ extern regoff_t re_search (struct re_pattern_buffer *__buffer,
1feee8
 			   const char *__String, regoff_t __length,
1feee8
 			   regoff_t __start, regoff_t __range,
1feee8
 			   struct re_registers *__regs)
1feee8
-    __attr_access ((__read_only__, 2, 3));
1feee8
+    _Attr_access_ ((__read_only__, 2, 3));
1feee8
 
1feee8
 
1feee8
 /* Like 're_search', but search in the concatenation of STRING1 and
1feee8
@@ -566,8 +590,8 @@ extern regoff_t re_search_2 (struct re_pattern_buffer *__buffer,
1feee8
 			     regoff_t __start, regoff_t __range,
1feee8
 			     struct re_registers *__regs,
1feee8
 			     regoff_t __stop)
1feee8
-    __attr_access ((__read_only__, 2, 3))
1feee8
-    __attr_access ((__read_only__, 4, 5));
1feee8
+    _Attr_access_ ((__read_only__, 2, 3))
1feee8
+    _Attr_access_ ((__read_only__, 4, 5));
1feee8
 
1feee8
 
1feee8
 /* Like 're_search', but return how many characters in STRING the regexp
1feee8
@@ -575,7 +599,7 @@ extern regoff_t re_search_2 (struct re_pattern_buffer *__buffer,
1feee8
 extern regoff_t re_match (struct re_pattern_buffer *__buffer,
1feee8
 			  const char *__String, regoff_t __length,
1feee8
 			  regoff_t __start, struct re_registers *__regs)
1feee8
-    __attr_access ((__read_only__, 2, 3));
1feee8
+    _Attr_access_ ((__read_only__, 2, 3));
1feee8
 
1feee8
 
1feee8
 /* Relates to 're_match' as 're_search_2' relates to 're_search'.  */
1feee8
@@ -584,8 +608,8 @@ extern regoff_t re_match_2 (struct re_pattern_buffer *__buffer,
1feee8
 			    const char *__string2, regoff_t __length2,
1feee8
 			    regoff_t __start, struct re_registers *__regs,
1feee8
 			    regoff_t __stop)
1feee8
-    __attr_access ((__read_only__, 2, 3))
1feee8
-    __attr_access ((__read_only__, 4, 5));
1feee8
+    _Attr_access_ ((__read_only__, 2, 3))
1feee8
+    _Attr_access_ ((__read_only__, 4, 5));
1feee8
 
1feee8
 
1feee8
 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
1feee8
@@ -654,16 +678,19 @@ extern int regcomp (regex_t *_Restrict_ __preg,
1feee8
 
1feee8
 extern int regexec (const regex_t *_Restrict_ __preg,
1feee8
 		    const char *_Restrict_ __String, size_t __nmatch,
1feee8
-		    regmatch_t __pmatch[_Restrict_arr_],
1feee8
-		    int __eflags)
1feee8
-    __attr_access ((__write_only__, 4, 3));
1feee8
+		    regmatch_t __pmatch[_Restrict_arr_
1feee8
+					_REGEX_NELTS (__nmatch)],
1feee8
+		    int __eflags);
1feee8
 
1feee8
 extern size_t regerror (int __errcode, const regex_t *_Restrict_ __preg,
1feee8
 			char *_Restrict_ __errbuf, size_t __errbuf_size)
1feee8
-    __attr_access ((__write_only__, 3, 4));
1feee8
+    _Attr_access_ ((__write_only__, 3, 4));
1feee8
 
1feee8
 extern void regfree (regex_t *__preg);
1feee8
 
1feee8
+#if defined __GNUC__ && 4 < __GNUC__ + (6 <= __GNUC_MINOR__)
1feee8
+# pragma GCC diagnostic pop
1feee8
+#endif
1feee8
 
1feee8
 #ifdef __cplusplus
1feee8
 }
1feee8
diff --git a/posix/regex_internal.c b/posix/regex_internal.c
1feee8
index 9dd387ef85d64e62..aefcfa2f52e68c6a 100644
1feee8
--- a/posix/regex_internal.c
1feee8
+++ b/posix/regex_internal.c
1feee8
@@ -1211,6 +1211,10 @@ re_node_set_merge (re_node_set *dest, const re_node_set *src)
1feee8
 
1feee8
   if (__glibc_unlikely (dest->nelem == 0))
1feee8
     {
1feee8
+      /* Although we already guaranteed above that dest->alloc != 0 and
1feee8
+         therefore dest->elems != NULL, add a debug assertion to pacify
1feee8
+         GCC 11.2.1's -fanalyzer.  */
1feee8
+      DEBUG_ASSERT (dest->elems);
1feee8
       dest->nelem = src->nelem;
1feee8
       memcpy (dest->elems, src->elems, src->nelem * sizeof (Idx));
1feee8
       return REG_NOERROR;
1feee8
@@ -1286,7 +1290,10 @@ re_node_set_insert (re_node_set *set, Idx elem)
1feee8
 
1feee8
   if (__glibc_unlikely (set->nelem) == 0)
1feee8
     {
1feee8
-      /* We already guaranteed above that set->alloc != 0.  */
1feee8
+      /* Although we already guaranteed above that set->alloc != 0 and
1feee8
+         therefore set->elems != NULL, add a debug assertion to pacify
1feee8
+         GCC 11.2 -fanalyzer.  */
1feee8
+      DEBUG_ASSERT (set->elems);
1feee8
       set->elems[0] = elem;
1feee8
       ++set->nelem;
1feee8
       return true;
1feee8
@@ -1314,6 +1321,7 @@ re_node_set_insert (re_node_set *set, Idx elem)
1feee8
     {
1feee8
       for (idx = set->nelem; set->elems[idx - 1] > elem; idx--)
1feee8
 	set->elems[idx] = set->elems[idx - 1];
1feee8
+      DEBUG_ASSERT (set->elems[idx - 1] < elem);
1feee8
     }
1feee8
 
1feee8
   /* Insert the new element.  */
1feee8
diff --git a/posix/regex_internal.h b/posix/regex_internal.h
1feee8
index edcdc07e999694ac..1245e782ffc69086 100644
1feee8
--- a/posix/regex_internal.h
1feee8
+++ b/posix/regex_internal.h
1feee8
@@ -32,6 +32,10 @@
1feee8
 #include <stdbool.h>
1feee8
 #include <stdint.h>
1feee8
 
1feee8
+#ifndef _LIBC
1feee8
+# include <dynarray.h>
1feee8
+#endif
1feee8
+
1feee8
 #include <intprops.h>
1feee8
 #include <verify.h>
1feee8
 
1feee8
@@ -49,14 +53,14 @@
1feee8
 # define lock_fini(lock) ((void) 0)
1feee8
 # define lock_lock(lock) __libc_lock_lock (lock)
1feee8
 # define lock_unlock(lock) __libc_lock_unlock (lock)
1feee8
-#elif defined GNULIB_LOCK && !defined USE_UNLOCKED_IO
1feee8
+#elif defined GNULIB_LOCK && !defined GNULIB_REGEX_SINGLE_THREAD
1feee8
 # include "glthread/lock.h"
1feee8
 # define lock_define(name) gl_lock_define (, name)
1feee8
 # define lock_init(lock) glthread_lock_init (&(lock))
1feee8
 # define lock_fini(lock) glthread_lock_destroy (&(lock))
1feee8
 # define lock_lock(lock) glthread_lock_lock (&(lock))
1feee8
 # define lock_unlock(lock) glthread_lock_unlock (&(lock))
1feee8
-#elif defined GNULIB_PTHREAD && !defined USE_UNLOCKED_IO
1feee8
+#elif defined GNULIB_PTHREAD && !defined GNULIB_REGEX_SINGLE_THREAD
1feee8
 # include <pthread.h>
1feee8
 # define lock_define(name) pthread_mutex_t name;
1feee8
 # define lock_init(lock) pthread_mutex_init (&(lock), 0)
1feee8
diff --git a/posix/regexec.c b/posix/regexec.c
1feee8
index f7b4f9cfc3f030df..83e9aaf8cad956a2 100644
1feee8
--- a/posix/regexec.c
1feee8
+++ b/posix/regexec.c
1feee8
@@ -59,7 +59,7 @@ static void update_regs (const re_dfa_t *dfa, regmatch_t *pmatch,
1feee8
 			 Idx cur_idx, Idx nmatch);
1feee8
 static reg_errcode_t push_fail_stack (struct re_fail_stack_t *fs,
1feee8
 				      Idx str_idx, Idx dest_node, Idx nregs,
1feee8
-				      regmatch_t *regs,
1feee8
+				      regmatch_t *regs, regmatch_t *prevregs,
1feee8
 				      re_node_set *eps_via_nodes);
1feee8
 static reg_errcode_t set_regs (const regex_t *preg,
1feee8
 			       const re_match_context_t *mctx,
1feee8
@@ -186,11 +186,12 @@ static reg_errcode_t extend_buffers (re_match_context_t *mctx, int min_len);
1feee8
    REG_NOTBOL is set, then ^ does not match at the beginning of the
1feee8
    string; if REG_NOTEOL is set, then $ does not match at the end.
1feee8
 
1feee8
-   We return 0 if we find a match and REG_NOMATCH if not.  */
1feee8
+   Return 0 if a match is found, REG_NOMATCH if not, REG_BADPAT if
1feee8
+   EFLAGS is invalid.  */
1feee8
 
1feee8
 int
1feee8
 regexec (const regex_t *__restrict preg, const char *__restrict string,
1feee8
-	 size_t nmatch, regmatch_t pmatch[], int eflags)
1feee8
+	 size_t nmatch, regmatch_t pmatch[_REGEX_NELTS (nmatch)], int eflags)
1feee8
 {
1feee8
   reg_errcode_t err;
1feee8
   Idx start, length;
1feee8
@@ -234,7 +235,7 @@ int
1feee8
 attribute_compat_text_section
1feee8
 __compat_regexec (const regex_t *__restrict preg,
1feee8
 		  const char *__restrict string, size_t nmatch,
1feee8
-		  regmatch_t pmatch[], int eflags)
1feee8
+		  regmatch_t pmatch[_REGEX_NELTS (nmatch)], int eflags)
1feee8
 {
1feee8
   return regexec (preg, string, nmatch, pmatch,
1feee8
 		  eflags & (REG_NOTBOL | REG_NOTEOL));
1feee8
@@ -269,8 +270,8 @@ compat_symbol (libc, __compat_regexec, regexec, GLIBC_2_0);
1feee8
    strings.)
1feee8
 
1feee8
    On success, re_match* functions return the length of the match, re_search*
1feee8
-   return the position of the start of the match.  Return value -1 means no
1feee8
-   match was found and -2 indicates an internal error.  */
1feee8
+   return the position of the start of the match.  They return -1 on
1feee8
+   match failure, -2 on error.  */
1feee8
 
1feee8
 regoff_t
1feee8
 re_match (struct re_pattern_buffer *bufp, const char *string, Idx length,
1feee8
@@ -1206,27 +1207,30 @@ check_halt_state_context (const re_match_context_t *mctx,
1feee8
 /* Compute the next node to which "NFA" transit from NODE("NFA" is a NFA
1feee8
    corresponding to the DFA).
1feee8
    Return the destination node, and update EPS_VIA_NODES;
1feee8
-   return -1 in case of errors.  */
1feee8
+   return -1 on match failure, -2 on error.  */
1feee8
 
1feee8
 static Idx
1feee8
 proceed_next_node (const re_match_context_t *mctx, Idx nregs, regmatch_t *regs,
1feee8
+		   regmatch_t *prevregs,
1feee8
 		   Idx *pidx, Idx node, re_node_set *eps_via_nodes,
1feee8
 		   struct re_fail_stack_t *fs)
1feee8
 {
1feee8
   const re_dfa_t *const dfa = mctx->dfa;
1feee8
-  Idx i;
1feee8
-  bool ok;
1feee8
   if (IS_EPSILON_NODE (dfa->nodes[node].type))
1feee8
     {
1feee8
       re_node_set *cur_nodes = &mctx->state_log[*pidx]->nodes;
1feee8
       re_node_set *edests = &dfa->edests[node];
1feee8
-      Idx dest_node;
1feee8
-      ok = re_node_set_insert (eps_via_nodes, node);
1feee8
-      if (__glibc_unlikely (! ok))
1feee8
-	return -2;
1feee8
-      /* Pick up a valid destination, or return -1 if none
1feee8
-	 is found.  */
1feee8
-      for (dest_node = -1, i = 0; i < edests->nelem; ++i)
1feee8
+
1feee8
+      if (! re_node_set_contains (eps_via_nodes, node))
1feee8
+        {
1feee8
+          bool ok = re_node_set_insert (eps_via_nodes, node);
1feee8
+          if (__glibc_unlikely (! ok))
1feee8
+            return -2;
1feee8
+        }
1feee8
+
1feee8
+      /* Pick a valid destination, or return -1 if none is found.  */
1feee8
+      Idx dest_node = -1;
1feee8
+      for (Idx i = 0; i < edests->nelem; i++)
1feee8
 	{
1feee8
 	  Idx candidate = edests->elems[i];
1feee8
 	  if (!re_node_set_contains (cur_nodes, candidate))
1feee8
@@ -1244,7 +1248,7 @@ proceed_next_node (const re_match_context_t *mctx, Idx nregs, regmatch_t *regs,
1feee8
 	      /* Otherwise, push the second epsilon-transition on the fail stack.  */
1feee8
 	      else if (fs != NULL
1feee8
 		       && push_fail_stack (fs, *pidx, candidate, nregs, regs,
1feee8
-					   eps_via_nodes))
1feee8
+					   prevregs, eps_via_nodes))
1feee8
 		return -2;
1feee8
 
1feee8
 	      /* We know we are going to exit.  */
1feee8
@@ -1288,7 +1292,7 @@ proceed_next_node (const re_match_context_t *mctx, Idx nregs, regmatch_t *regs,
1feee8
 	  if (naccepted == 0)
1feee8
 	    {
1feee8
 	      Idx dest_node;
1feee8
-	      ok = re_node_set_insert (eps_via_nodes, node);
1feee8
+	      bool ok = re_node_set_insert (eps_via_nodes, node);
1feee8
 	      if (__glibc_unlikely (! ok))
1feee8
 		return -2;
1feee8
 	      dest_node = dfa->edests[node].elems[0];
1feee8
@@ -1317,7 +1321,8 @@ proceed_next_node (const re_match_context_t *mctx, Idx nregs, regmatch_t *regs,
1feee8
 static reg_errcode_t
1feee8
 __attribute_warn_unused_result__
1feee8
 push_fail_stack (struct re_fail_stack_t *fs, Idx str_idx, Idx dest_node,
1feee8
-		 Idx nregs, regmatch_t *regs, re_node_set *eps_via_nodes)
1feee8
+		 Idx nregs, regmatch_t *regs, regmatch_t *prevregs,
1feee8
+		 re_node_set *eps_via_nodes)
1feee8
 {
1feee8
   reg_errcode_t err;
1feee8
   Idx num = fs->num++;
1feee8
@@ -1333,25 +1338,30 @@ push_fail_stack (struct re_fail_stack_t *fs, Idx str_idx, Idx dest_node,
1feee8
     }
1feee8
   fs->stack[num].idx = str_idx;
1feee8
   fs->stack[num].node = dest_node;
1feee8
-  fs->stack[num].regs = re_malloc (regmatch_t, nregs);
1feee8
+  fs->stack[num].regs = re_malloc (regmatch_t, 2 * nregs);
1feee8
   if (fs->stack[num].regs == NULL)
1feee8
     return REG_ESPACE;
1feee8
   memcpy (fs->stack[num].regs, regs, sizeof (regmatch_t) * nregs);
1feee8
+  memcpy (fs->stack[num].regs + nregs, prevregs, sizeof (regmatch_t) * nregs);
1feee8
   err = re_node_set_init_copy (&fs->stack[num].eps_via_nodes, eps_via_nodes);
1feee8
   return err;
1feee8
 }
1feee8
 
1feee8
 static Idx
1feee8
 pop_fail_stack (struct re_fail_stack_t *fs, Idx *pidx, Idx nregs,
1feee8
-		regmatch_t *regs, re_node_set *eps_via_nodes)
1feee8
+		regmatch_t *regs, regmatch_t *prevregs,
1feee8
+		re_node_set *eps_via_nodes)
1feee8
 {
1feee8
+  if (fs == NULL || fs->num == 0)
1feee8
+    return -1;
1feee8
   Idx num = --fs->num;
1feee8
-  DEBUG_ASSERT (num >= 0);
1feee8
   *pidx = fs->stack[num].idx;
1feee8
   memcpy (regs, fs->stack[num].regs, sizeof (regmatch_t) * nregs);
1feee8
+  memcpy (prevregs, fs->stack[num].regs + nregs, sizeof (regmatch_t) * nregs);
1feee8
   re_node_set_free (eps_via_nodes);
1feee8
   re_free (fs->stack[num].regs);
1feee8
   *eps_via_nodes = fs->stack[num].eps_via_nodes;
1feee8
+  DEBUG_ASSERT (0 <= fs->stack[num].node);
1feee8
   return fs->stack[num].node;
1feee8
 }
1feee8
 
1feee8
@@ -1407,33 +1417,32 @@ set_regs (const regex_t *preg, const re_match_context_t *mctx, size_t nmatch,
1feee8
     {
1feee8
       update_regs (dfa, pmatch, prev_idx_match, cur_node, idx, nmatch);
1feee8
 
1feee8
-      if (idx == pmatch[0].rm_eo && cur_node == mctx->last_node)
1feee8
+      if ((idx == pmatch[0].rm_eo && cur_node == mctx->last_node)
1feee8
+	  || (fs && re_node_set_contains (&eps_via_nodes, cur_node)))
1feee8
 	{
1feee8
 	  Idx reg_idx;
1feee8
+	  cur_node = -1;
1feee8
 	  if (fs)
1feee8
 	    {
1feee8
 	      for (reg_idx = 0; reg_idx < nmatch; ++reg_idx)
1feee8
 		if (pmatch[reg_idx].rm_so > -1 && pmatch[reg_idx].rm_eo == -1)
1feee8
-		  break;
1feee8
-	      if (reg_idx == nmatch)
1feee8
-		{
1feee8
-		  re_node_set_free (&eps_via_nodes);
1feee8
-		  regmatch_list_free (&prev_match);
1feee8
-		  return free_fail_stack_return (fs);
1feee8
-		}
1feee8
-	      cur_node = pop_fail_stack (fs, &idx, nmatch, pmatch,
1feee8
-					 &eps_via_nodes);
1feee8
+		  {
1feee8
+		    cur_node = pop_fail_stack (fs, &idx, nmatch, pmatch,
1feee8
+					       prev_idx_match, &eps_via_nodes);
1feee8
+		    break;
1feee8
+		  }
1feee8
 	    }
1feee8
-	  else
1feee8
+	  if (cur_node < 0)
1feee8
 	    {
1feee8
 	      re_node_set_free (&eps_via_nodes);
1feee8
 	      regmatch_list_free (&prev_match);
1feee8
-	      return REG_NOERROR;
1feee8
+	      return free_fail_stack_return (fs);
1feee8
 	    }
1feee8
 	}
1feee8
 
1feee8
       /* Proceed to next node.  */
1feee8
-      cur_node = proceed_next_node (mctx, nmatch, pmatch, &idx, cur_node,
1feee8
+      cur_node = proceed_next_node (mctx, nmatch, pmatch, prev_idx_match,
1feee8
+				    &idx, cur_node,
1feee8
 				    &eps_via_nodes, fs);
1feee8
 
1feee8
       if (__glibc_unlikely (cur_node < 0))
1feee8
@@ -1445,13 +1454,13 @@ set_regs (const regex_t *preg, const re_match_context_t *mctx, size_t nmatch,
1feee8
 	      free_fail_stack_return (fs);
1feee8
 	      return REG_ESPACE;
1feee8
 	    }
1feee8
-	  if (fs)
1feee8
-	    cur_node = pop_fail_stack (fs, &idx, nmatch, pmatch,
1feee8
-				       &eps_via_nodes);
1feee8
-	  else
1feee8
+	  cur_node = pop_fail_stack (fs, &idx, nmatch, pmatch,
1feee8
+				     prev_idx_match, &eps_via_nodes);
1feee8
+	  if (cur_node < 0)
1feee8
 	    {
1feee8
 	      re_node_set_free (&eps_via_nodes);
1feee8
 	      regmatch_list_free (&prev_match);
1feee8
+	      free_fail_stack_return (fs);
1feee8
 	      return REG_NOMATCH;
1feee8
 	    }
1feee8
 	}
1feee8
@@ -1495,10 +1504,10 @@ update_regs (const re_dfa_t *dfa, regmatch_t *pmatch,
1feee8
     }
1feee8
   else if (type == OP_CLOSE_SUBEXP)
1feee8
     {
1feee8
+      /* We are at the last node of this sub expression.  */
1feee8
       Idx reg_num = dfa->nodes[cur_node].opr.idx + 1;
1feee8
       if (reg_num < nmatch)
1feee8
 	{
1feee8
-	  /* We are at the last node of this sub expression.  */
1feee8
 	  if (pmatch[reg_num].rm_so < cur_idx)
1feee8
 	    {
1feee8
 	      pmatch[reg_num].rm_eo = cur_idx;
1feee8
@@ -2195,6 +2204,7 @@ sift_states_iter_mb (const re_match_context_t *mctx, re_sift_context_t *sctx,
1feee8
 
1feee8
 /* Return the next state to which the current state STATE will transit by
1feee8
    accepting the current input byte, and update STATE_LOG if necessary.
1feee8
+   Return NULL on failure.
1feee8
    If STATE can accept a multibyte char/collating element/back reference
1feee8
    update the destination of STATE_LOG.  */
1feee8
 
1feee8
@@ -2395,7 +2405,7 @@ check_subexp_matching_top (re_match_context_t *mctx, re_node_set *cur_nodes,
1feee8
 
1feee8
 #if 0
1feee8
 /* Return the next state to which the current state STATE will transit by
1feee8
-   accepting the current input byte.  */
1feee8
+   accepting the current input byte.  Return NULL on failure.  */
1feee8
 
1feee8
 static re_dfastate_t *
1feee8
 transit_state_sb (reg_errcode_t *err, re_match_context_t *mctx,
1feee8
@@ -2817,7 +2827,8 @@ find_subexp_node (const re_dfa_t *dfa, const re_node_set *nodes,
1feee8
 /* Check whether the node TOP_NODE at TOP_STR can arrive to the node
1feee8
    LAST_NODE at LAST_STR.  We record the path onto PATH since it will be
1feee8
    heavily reused.
1feee8
-   Return REG_NOERROR if it can arrive, or REG_NOMATCH otherwise.  */
1feee8
+   Return REG_NOERROR if it can arrive, REG_NOMATCH if it cannot,
1feee8
+   REG_ESPACE if memory is exhausted.  */
1feee8
 
1feee8
 static reg_errcode_t
1feee8
 __attribute_warn_unused_result__
1feee8
@@ -3433,7 +3444,8 @@ build_trtable (const re_dfa_t *dfa, re_dfastate_t *state)
1feee8
 /* Group all nodes belonging to STATE into several destinations.
1feee8
    Then for all destinations, set the nodes belonging to the destination
1feee8
    to DESTS_NODE[i] and set the characters accepted by the destination
1feee8
-   to DEST_CH[i].  This function return the number of destinations.  */
1feee8
+   to DEST_CH[i].  Return the number of destinations if successful,
1feee8
+   -1 on internal error.  */
1feee8
 
1feee8
 static Idx
1feee8
 group_nodes_into_DFAstates (const re_dfa_t *dfa, const re_dfastate_t *state,
1feee8
@@ -4211,7 +4223,8 @@ match_ctx_add_subtop (re_match_context_t *mctx, Idx node, Idx str_idx)
1feee8
 }
1feee8
 
1feee8
 /* Register the node NODE, whose type is OP_CLOSE_SUBEXP, and which matches
1feee8
-   at STR_IDX, whose corresponding OP_OPEN_SUBEXP is SUB_TOP.  */
1feee8
+   at STR_IDX, whose corresponding OP_OPEN_SUBEXP is SUB_TOP.
1feee8
+   Return the new entry if successful, NULL if memory is exhausted.  */
1feee8
 
1feee8
 static re_sub_match_last_t *
1feee8
 match_ctx_add_sublast (re_sub_match_top_t *subtop, Idx node, Idx str_idx)