]> Savannah Git Hosting - gnulib.git/commitdiff
regex: remove alloca usage on regex set_regs
authorPaul Eggert <eggert@cs.ucla.edu>
Fri, 8 Jan 2021 20:00:09 +0000 (12:00 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Fri, 8 Jan 2021 20:02:04 +0000 (12:02 -0800)
Derived from this patch by Adhemerval Zanella:
https://sourceware.org/pipermail/libc-alpha/2021-January/121372.html
* lib/regex_internal.h: Include dynarray.h, for Gnulib.
* lib/regexec.c (DYNARRAY_STRUCT, DYNARRAY_ELEMENT)
(DYNARRAY_PREFIX): New macros.
Include malloc/dynarray-skeleton.c.
(set_regs): Use dynarray rather than alloca.
* modules/regex (Depends-on): Add dynarray.

ChangeLog
lib/regex_internal.h
lib/regexec.c
modules/regex

index db37b0a248816d19efce009ff312a742f516bae1..aff3584e42ca250ddf58ae4e64fb4b2d232101ae 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
 2021-01-08  Paul Eggert  <eggert@cs.ucla.edu>
 
+       regex: remove alloca usage on regex set_regs
+       Derived from this patch by Adhemerval Zanella:
+       https://sourceware.org/pipermail/libc-alpha/2021-January/121372.html
+       * lib/regex_internal.h: Include dynarray.h, for Gnulib.
+       * lib/regexec.c (DYNARRAY_STRUCT, DYNARRAY_ELEMENT)
+       (DYNARRAY_PREFIX): New macros.
+       Include malloc/dynarray-skeleton.c.
+       (set_regs): Use dynarray rather than alloca.
+       * modules/regex (Depends-on): Add dynarray.
+
        dynarray: new module
        * config/srclist.txt: Mention the new files.
        * lib/cdefs.h (__attribute_maybe_unused__): New macro,
index e31ac926742ce64db278cac6c3142b1e6586d05f..5d4d5fe2b35e0cf49bf88ae9dce05d3a1495ede6 100644 (file)
@@ -32,6 +32,7 @@
 #include <stdbool.h>
 #include <stdint.h>
 
+#include <dynarray.h>
 #include <intprops.h>
 #include <verify.h>
 
index b083342f773cf498b281564f77cc54ddff6a0ada..889b10be9c83992a07a6cebffd62f3d60b334327 100644 (file)
@@ -1355,6 +1355,12 @@ pop_fail_stack (struct re_fail_stack_t *fs, Idx *pidx, Idx nregs,
   return fs->stack[num].node;
 }
 
+
+#define DYNARRAY_STRUCT  regmatch_list
+#define DYNARRAY_ELEMENT regmatch_t
+#define DYNARRAY_PREFIX  regmatch_list_
+#include <malloc/dynarray-skeleton.c>
+
 /* Set the positions where the subexpressions are starts/ends to registers
    PMATCH.
    Note: We assume that pmatch[0] is already set, and
@@ -1370,8 +1376,8 @@ set_regs (const regex_t *preg, const re_match_context_t *mctx, size_t nmatch,
   re_node_set eps_via_nodes;
   struct re_fail_stack_t *fs;
   struct re_fail_stack_t fs_body = { 0, 2, NULL };
-  regmatch_t *prev_idx_match;
-  bool prev_idx_match_malloced = false;
+  struct regmatch_list prev_match;
+  regmatch_list_init (&prev_match);
 
   DEBUG_ASSERT (nmatch > 1);
   DEBUG_ASSERT (mctx->state_log != NULL);
@@ -1388,18 +1394,13 @@ set_regs (const regex_t *preg, const re_match_context_t *mctx, size_t nmatch,
   cur_node = dfa->init_node;
   re_node_set_init_empty (&eps_via_nodes);
 
-  if (__libc_use_alloca (nmatch * sizeof (regmatch_t)))
-    prev_idx_match = (regmatch_t *) alloca (nmatch * sizeof (regmatch_t));
-  else
+  if (!regmatch_list_resize (&prev_match, nmatch))
     {
-      prev_idx_match = re_malloc (regmatch_t, nmatch);
-      if (prev_idx_match == NULL)
-       {
-         free_fail_stack_return (fs);
-         return REG_ESPACE;
-       }
-      prev_idx_match_malloced = true;
+      regmatch_list_free (&prev_match);
+      free_fail_stack_return (fs);
+      return REG_ESPACE;
     }
+  regmatch_t *prev_idx_match = regmatch_list_begin (&prev_match);
   memcpy (prev_idx_match, pmatch, sizeof (regmatch_t) * nmatch);
 
   for (idx = pmatch[0].rm_so; idx <= pmatch[0].rm_eo ;)
@@ -1417,8 +1418,7 @@ set_regs (const regex_t *preg, const re_match_context_t *mctx, size_t nmatch,
              if (reg_idx == nmatch)
                {
                  re_node_set_free (&eps_via_nodes);
-                 if (prev_idx_match_malloced)
-                   re_free (prev_idx_match);
+                 regmatch_list_free (&prev_match);
                  return free_fail_stack_return (fs);
                }
              cur_node = pop_fail_stack (fs, &idx, nmatch, pmatch,
@@ -1427,8 +1427,7 @@ set_regs (const regex_t *preg, const re_match_context_t *mctx, size_t nmatch,
          else
            {
              re_node_set_free (&eps_via_nodes);
-             if (prev_idx_match_malloced)
-               re_free (prev_idx_match);
+             regmatch_list_free (&prev_match);
              return REG_NOERROR;
            }
        }
@@ -1442,8 +1441,7 @@ set_regs (const regex_t *preg, const re_match_context_t *mctx, size_t nmatch,
          if (__glibc_unlikely (cur_node == -2))
            {
              re_node_set_free (&eps_via_nodes);
-             if (prev_idx_match_malloced)
-               re_free (prev_idx_match);
+             regmatch_list_free (&prev_match);
              free_fail_stack_return (fs);
              return REG_ESPACE;
            }
@@ -1453,15 +1451,13 @@ set_regs (const regex_t *preg, const re_match_context_t *mctx, size_t nmatch,
          else
            {
              re_node_set_free (&eps_via_nodes);
-             if (prev_idx_match_malloced)
-               re_free (prev_idx_match);
+             regmatch_list_free (&prev_match);
              return REG_NOMATCH;
            }
        }
     }
   re_node_set_free (&eps_via_nodes);
-  if (prev_idx_match_malloced)
-    re_free (prev_idx_match);
+  regmatch_list_free (&prev_match);
   return free_fail_stack_return (fs);
 }
 
index 570b0bd55307bbb32aef5e1c305de2167822c12f..39297dfe3cd06017061b416e4ea84f3d5c85adda 100644 (file)
@@ -19,6 +19,7 @@ ssize_t
 alloca-opt      [test $ac_use_included_regex = yes]
 btowc           [test $ac_use_included_regex = yes]
 builtin-expect  [test $ac_use_included_regex = yes]
+dynarray        [test $ac_use_included_regex = yes]
 intprops        [test $ac_use_included_regex = yes]
 langinfo        [test $ac_use_included_regex = yes]
 libc-config     [test $ac_use_included_regex = yes]