[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] dfa: omit unneeded malloc+free
From: |
Paul Eggert |
Subject: |
[PATCH] dfa: omit unneeded malloc+free |
Date: |
Mon, 2 Aug 2021 11:37:22 -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 | 8 ++++++++
lib/dfa.c | 19 +++++++++++--------
2 files changed, 19 insertions(+), 8 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 3027aa45f..d4025b64e 100644
--- 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
diff --git a/lib/dfa.c b/lib/dfa.c
index 44c3b65c2..8286ea10d 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')
--
2.30.2
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [PATCH] dfa: omit unneeded malloc+free,
Paul Eggert <=