coreutils
[Top][All Lists]
Advanced

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

[coreutils] [PATCH] sort, who: prefer free+malloc to realloc when conten


From: Paul Eggert
Subject: [coreutils] [PATCH] sort, who: prefer free+malloc to realloc when contents are irrelevant
Date: Wed, 11 Aug 2010 05:45:17 +0200
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.11) Gecko/20100713 Thunderbird/3.0.6

This change was prompted by the previous one: I audited the code
looking for similar examples.  Too bad valgrind doesn't catch this.
* src/sort.c (check, mergefps): xrealloc -> free + xmalloc
* src/who.c (print_user): Likewise.
---
 src/sort.c |    6 ++++--
 src/who.c  |    9 ++++++---
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/src/sort.c b/src/sort.c
index 3dc7ae0..a8d0c14 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -2767,7 +2767,8 @@ check (char const *file_name, char checkonly)
             }
           while (alloc < line->length);
 
-          temp.text = xrealloc (temp.text, alloc);
+          free (temp.text);
+          temp.text = xmalloc (alloc);
         }
       memcpy (temp.text, line->text, line->length);
       temp.length = line->length;
@@ -2907,7 +2908,8 @@ mergefps (struct sortfile *files, size_t ntemps, size_t 
nfiles,
                       }
                   while ((savealloc *= 2) < smallest->length);
 
-                  saved.text = xrealloc (saved.text, savealloc);
+                  free (saved.text);
+                  saved.text = xmalloc (savealloc);
                 }
               saved.length = smallest->length;
               memcpy (saved.text, smallest->text, saved.length);
diff --git a/src/who.c b/src/who.c
index 2c0d947..ac98881 100644
--- a/src/who.c
+++ b/src/who.c
@@ -410,7 +410,8 @@ print_user (const STRUCT_UTMP *utmp_ent, time_t boottime)
           if (hostlen < strlen (host) + strlen (display) + 4)
             {
               hostlen = strlen (host) + strlen (display) + 4;
-              hoststr = xrealloc (hoststr, hostlen);
+              free (hoststr);
+              hoststr = xmalloc (hostlen);
             }
           sprintf (hoststr, "(%s:%s)", host, display);
         }
@@ -419,7 +420,8 @@ print_user (const STRUCT_UTMP *utmp_ent, time_t boottime)
           if (hostlen < strlen (host) + 3)
             {
               hostlen = strlen (host) + 3;
-              hoststr = xrealloc (hoststr, hostlen);
+              free (hoststr);
+              hoststr = xmalloc (hostlen);
             }
           sprintf (hoststr, "(%s)", host);
         }
@@ -432,7 +434,8 @@ print_user (const STRUCT_UTMP *utmp_ent, time_t boottime)
       if (hostlen < 1)
         {
           hostlen = 1;
-          hoststr = xrealloc (hoststr, hostlen);
+          free (hoststr);
+          hoststr = xmalloc (hostlen);
         }
       *hoststr = '\0';
     }
-- 
1.7.2




reply via email to

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