]> Savannah Git Hosting - gnulib.git/commitdiff
dfa: omit unneeded malloc+free
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 2 Aug 2021 18:36:30 +0000 (11:36 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 2 Aug 2021 18:37:08 +0000 (11:37 -0700)
Problem indirectly found by Coverity.
* lib/dfa.c (enlistnew): New function, with most of the body of
the old ‘enlist’.  It assumes its arg NEW has been malloced and
can be freed eventually.
(enlist, addlists, dfamust): Use it.
(dfamust): Omit an unnecessary malloc+free.

ChangeLog
lib/dfa.c

index 3027aa45fb0038c5e672999e8f8f883caa382be0..d4025b64e865ae5d24e76398b57d2a5e28cd5839 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2021-08-02  Paul Eggert  <eggert@cs.ucla.edu>
 
+       dfa: omit unneeded malloc+free
+       Problem indirectly found by Coverity.
+       * lib/dfa.c (enlistnew): New function, with most of the body of
+       the old ‘enlist’.  It assumes its arg NEW has been malloced and
+       can be freed eventually.
+       (enlist, addlists, dfamust): Use it.
+       (dfamust): Omit an unnecessary malloc+free.
+
        year2038: port to unusual time_t platforms
        * m4/year2038.m4 (gl_YEAR2038_TEST_INCLUDES): Check that time_t
        can go to 2**32 - 1, not to 2**63 - 1, as the former is enough to
index 44c3b65c280a6d26b8023d18dab7ff364528f760..8286ea10d1755cb1528cf9ea855693bf70ad6b2b 100644 (file)
--- a/lib/dfa.c
+++ b/lib/dfa.c
@@ -3921,10 +3921,8 @@ freelist (char **cpp)
 }
 
 static char **
-enlist (char **cpp, char *new, idx_t len)
+enlistnew (char **cpp, char *new)
 {
-  new = memcpy (ximalloc (len + 1), new, len);
-  new[len] = '\0';
   /* Is there already something in the list that's new (or longer)?  */
   idx_t i;
   for (i = 0; cpp[i] != NULL; i++)
@@ -3952,6 +3950,12 @@ enlist (char **cpp, char *new, idx_t len)
   return cpp;
 }
 
+static char **
+enlist (char **cpp, char const *str, idx_t len)
+{
+  return enlistnew (cpp, ximemdup0 (str, len));
+}
+
 /* Given pointers to two strings, return a pointer to an allocated
    list of their distinct common substrings.  */
 static char **
@@ -3982,7 +3986,7 @@ static char **
 addlists (char **old, char **new)
 {
   for (; *new; new++)
-    old = enlist (old, *new, strlen (*new));
+    old = enlistnew (old, xstrdup (*new));
   return old;
 }
 
@@ -4184,11 +4188,10 @@ dfamust (struct dfa const *d)
               {
                 idx_t lrlen = strlen (lmp->right);
                 idx_t rllen = strlen (rmp->left);
-                char *tp = ximalloc (lrlen + rllen);
+                char *tp = ximalloc (lrlen + rllen + 1);
+                memcpy (tp + lrlen, rmp->left, rllen + 1);
                 memcpy (tp, lmp->right, lrlen);
-                memcpy (tp + lrlen, rmp->left, rllen);
-                lmp->in = enlist (lmp->in, tp, lrlen + rllen);
-                free (tp);
+                lmp->in = enlistnew (lmp->in, tp);
               }
             /* Left-hand */
             if (lmp->is[0] != '\0')