diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2021-08-02 11:36:30 -0700 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2021-08-02 11:37:08 -0700 |
commit | b03cdc1b6fbc7abae6befd254bf440861aad8ec7 (patch) | |
tree | 785ff53216483dd5fe1a272dbca932c5a5a3591c /lib | |
parent | 7c6538cf0584a5ad3e2e46f64b1936eea4cddecc (diff) | |
download | gnulib-b03cdc1b6fbc7abae6befd254bf440861aad8ec7.tar.gz |
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.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/dfa.c | 19 |
1 files changed, 11 insertions, 8 deletions
@@ -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') |