bug-grep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 2/2] maint: clean up and plug a leak-on-OOM


From: Jim Meyering
Subject: [PATCH 2/2] maint: clean up and plug a leak-on-OOM
Date: Sat, 20 Aug 2011 22:42:16 +0200

I dug up an old patch and then fixed an unlikely leak-on-OOM
which led to more cleanup:

>From b0edb0fc72a8d2c706f4144867b6795b49103f85 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Sun, 2 Jan 2011 10:18:50 +0100
Subject: [PATCH 1/2] maint: use x2nrealloc, not xrealloc

* src/main.c (main): Use x2nrealloc, not xrealloc
---
 src/main.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/src/main.c b/src/main.c
index fd9f662..2cdf82f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1927,7 +1927,7 @@ main (int argc, char **argv)
           {
             keycc += cc;
             if (keycc == keyalloc - 1)
-              keys = xrealloc (keys, keyalloc *= 2);
+              keys = x2nrealloc (keys, &keyalloc, sizeof *keys);
           }
         if (fp != stdin)
           fclose(fp);
--
1.7.6.677.gb5fca


>From bb85185a2b32f4c35f3cf629319ca03088602144 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Fri, 12 Aug 2011 17:57:29 +0200
Subject: [PATCH 2/2] maint: clean up and plug a leak-on-OOM

* src/dfa.c (icatalloc): Clean up; use xrealloc in place of malloc
and realloc; remove conditionals that are unnecessary, now that
failed allocation results in exit.
(enlist): Use xrealloc in place of realloc; remove conditional.
(comsubs): Avoid leak upon failed enlist call.
(dfamust): Use xmalloc in place of malloc.
Remove conditionals, now that icpyalloc and icatalloc never return NULL.
---
 src/dfa.c |   48 ++++++++++++++++++++----------------------------
 1 files changed, 20 insertions(+), 28 deletions(-)

diff --git a/src/dfa.c b/src/dfa.c
index b1a5266..76e34a7 100644
--- a/src/dfa.c
+++ b/src/dfa.c
@@ -3643,20 +3643,12 @@ static char *
 icatalloc (char *old, char const *new)
 {
   char *result;
-  size_t oldsize, newsize;
-
-  newsize = (new == NULL) ? 0 : strlen(new);
-  if (old == NULL)
-    oldsize = 0;
-  else if (newsize == 0)
+  size_t oldsize = old == NULL ? 0 : strlen (old);
+  size_t newsize = new == NULL ? 0 : strlen (new);
+  if (newsize == 0)
     return old;
-  else oldsize = strlen(old);
-  if (old == NULL)
-    result = malloc(newsize + 1);
-  else
-    result = realloc(old, oldsize + newsize + 1);
-  if (result != NULL && new != NULL)
-    strcpy(result + oldsize, new);
+  result = xrealloc (old, oldsize + newsize + 1);
+  strcpy (result + oldsize, new);
   return result;
 }

@@ -3764,8 +3756,16 @@ comsubs (char *left, char const *right)
         }
       if (len == 0)
         continue;
-      if ((cpp = enlist(cpp, lcp, len)) == NULL)
-        break;
+      {
+        char **p = enlist (cpp, lcp, len);
+        if (p == NULL)
+          {
+            freelist (cpp);
+            cpp = NULL;
+            break;
+          }
+        cpp = p;
+      }
     }
   return cpp;
 }
@@ -3858,13 +3858,10 @@ dfamust (struct dfa *d)
     mp[i] = must0;
   for (i = 0; i <= d->tindex; ++i)
     {
-      mp[i].in = malloc(sizeof *mp[i].in);
-      mp[i].left = malloc(2);
-      mp[i].right = malloc(2);
-      mp[i].is = malloc(2);
-      if (mp[i].in == NULL || mp[i].left == NULL ||
-          mp[i].right == NULL || mp[i].is == NULL)
-        goto done;
+      mp[i].in = xmalloc(sizeof *mp[i].in);
+      mp[i].left = xmalloc(2);
+      mp[i].right = xmalloc(2);
+      mp[i].is = xmalloc(2);
       mp[i].left[0] = mp[i].right[0] = mp[i].is[0] = '\0';
       mp[i].in[0] = NULL;
     }
@@ -3971,13 +3968,8 @@ dfamust (struct dfa *d)
                 char *tp;

                 tp = icpyalloc(lmp->right);
-                if (tp == NULL)
-                  goto done;
                 tp = icatalloc(tp, rmp->left);
-                if (tp == NULL)
-                  goto done;
-                lmp->in = enlist(lmp->in, tp,
-                                 strlen(tp));
+                lmp->in = enlist(lmp->in, tp, strlen(tp));
                 free(tp);
                 if (lmp->in == NULL)
                   goto done;
--
1.7.6.677.gb5fca



reply via email to

[Prev in Thread] Current Thread [Next in Thread]