]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
sort, who: prefer free+malloc to realloc when contents are irrelevant
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 11 Aug 2010 03:25:20 +0000 (20:25 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Wed, 11 Aug 2010 03:25:56 +0000 (20:25 -0700)
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
src/who.c

index 3dc7ae0b79e44c8bea6e0aa55af889aeacc68717..a8d0c142bfffefad7eeb9db10c58b811232ae9b3 100644 (file)
@@ -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);
index 2c0d94790c79ab9d8077d5d26150ef9559d7b4e5..ac988816be370672fa3c5723cbe8ad6fd05e3e74 100644 (file)
--- 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';
     }