Subject: dfa.c will fail if used on more than one DFA
From: Aharon Robbins <[email-masked]>
To: [email-masked]
Date: Wed, 23 Apr 2014 21:36:11 +0300
List-Id: <debbugs-submit.debbugs.gnu.org>

Hello.

There is a built-in assumption that the routines in dfa.c will only
be used on one struct dfa.  This is true for grep but not true for gawk.
I found this when trying to update gawk's dfa with all the changes that
have been coming through.  The patch is below.

Thanks,

Arnold
-----------------------
diff --git a/src/dfa.c b/src/dfa.c
index 65fc03d..85ab9bd 100644
--- a/src/dfa.c
+++ b/src/dfa.c
@@ -3244,15 +3244,10 @@ dfaexec (struct dfa *d, char const *begin, char *end,
 
   if (d->mb_cur_max > 1)
     {
-      static bool mb_alloc = false;
       memset (&mbs, 0, sizeof (mbstate_t));
-      if (!mb_alloc)
-        {
-          d->mb_match_lens = xnmalloc (d->nleaves, sizeof *d->mb_match_lens);
-          d->mb_follows = xmalloc (sizeof *d->mb_follows);
-          alloc_position_set (d->mb_follows, d->nleaves);
-          mb_alloc = true;
-        }
+      d->mb_match_lens = xnmalloc (d->nleaves, sizeof *d->mb_match_lens);
+      d->mb_follows = xmalloc (sizeof *d->mb_follows);
+      alloc_position_set (d->mb_follows, d->nleaves);
     }
 
   for (;;)
@@ -3434,8 +3429,13 @@ free_mbdata (struct dfa *d)
     {
       free (d->mb_follows->elems);
       free (d->mb_follows);
+      d->mb_follows = NULL;
+    }
+  if (d->mb_match_lens)
+    {
+      free (d->mb_match_lens);
+      d->mb_match_lens = NULL;
     }
-  free (d->mb_match_lens);
 }
 
 /* Initialize the components of a dfa that the other routines don't