]> Savannah Git Hosting - gnulib.git/commitdiff
regex: fix free_fail_stack undefined behavior
authorPaul Eggert <eggert@cs.ucla.edu>
Fri, 11 Mar 2022 21:27:33 +0000 (13:27 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Fri, 11 Mar 2022 21:34:42 +0000 (13:34 -0800)
* lib/regexec.c (push_fail_stack): Don’t increment number of
re_fail_stack_t entries until after successful allocation.  This
prevents a crash if re_realloc or re_malloc fails here, and a
later free_fail_stack examines regs or a later pop_fail_stack
examines node.  Problem discovered by Coverity scan sent
2022-03-11 11:03:52Z.

ChangeLog
lib/regexec.c

index 7713294982282cf965f0ff25320e515bf2b88bda..50f60c6372051541f138ab95a6c497d972e6d53f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2022-03-11  Paul Eggert  <eggert@cs.ucla.edu>
+
+       regex: fix free_fail_stack undefined behavior
+       * lib/regexec.c (push_fail_stack): Don’t increment number of
+       re_fail_stack_t entries until after successful allocation.  This
+       prevents a crash if re_realloc or re_malloc fails here, and a
+       later free_fail_stack examines regs or a later pop_fail_stack
+       examines node.  Problem discovered by Coverity scan sent
+       2022-03-11 11:03:52Z.
+
 2022-03-10  Paul Eggert  <eggert@cs.ucla.edu>
 
        fts: revert change to use AT_NO_AUTOMOUNT
index aea1e7da52c1d938fa49a694610153c8efb76f25..0691e91e1ee7d2790bc6b822eb752a4bc41aa454 100644 (file)
@@ -1308,8 +1308,8 @@ push_fail_stack (struct re_fail_stack_t *fs, Idx str_idx, Idx dest_node,
                 re_node_set *eps_via_nodes)
 {
   reg_errcode_t err;
-  Idx num = fs->num++;
-  if (fs->num == fs->alloc)
+  Idx num = fs->num;
+  if (num + 1 == fs->alloc)
     {
       struct re_fail_stack_ent_t *new_array;
       new_array = re_realloc (fs->stack, struct re_fail_stack_ent_t,
@@ -1324,6 +1324,7 @@ push_fail_stack (struct re_fail_stack_t *fs, Idx str_idx, Idx dest_node,
   fs->stack[num].regs = re_malloc (regmatch_t, 2 * nregs);
   if (fs->stack[num].regs == NULL)
     return REG_ESPACE;
+  fs->num = num + 1;
   memcpy (fs->stack[num].regs, regs, sizeof (regmatch_t) * nregs);
   memcpy (fs->stack[num].regs + nregs, prevregs, sizeof (regmatch_t) * nregs);
   err = re_node_set_init_copy (&fs->stack[num].eps_via_nodes, eps_via_nodes);