From: Paul Eggert Date: Mon, 2 Aug 2021 18:36:30 +0000 (-0700) Subject: dfa: omit unneeded malloc+free X-Git-Tag: v1.0~2729 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=b03cdc1b6fbc7abae6befd254bf440861aad8ec7;p=gnulib.git 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. --- diff --git a/ChangeLog b/ChangeLog index 3027aa45fb..d4025b64e8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,13 @@ 2021-08-02 Paul Eggert + 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 diff --git a/lib/dfa.c b/lib/dfa.c index 44c3b65c28..8286ea10d1 100644 --- 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')