diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2010-08-10 20:25:20 -0700 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2010-08-10 20:25:56 -0700 |
commit | 9899e6cd853baf2704fdb52e55270ffbabd37dff (patch) | |
tree | 4e7d42c32ea90cb1409a7fb8c840dabbdca7dc04 /src | |
parent | 2050a5913b0ee2e08de22db29340865f7f0ee608 (diff) | |
download | coreutils-9899e6cd853baf2704fdb52e55270ffbabd37dff.tar.xz |
sort, who: prefer free+malloc to realloc when contents are irrelevant
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/sort.c | 6 | ||||
-rw-r--r-- | src/who.c | 9 |
2 files changed, 10 insertions, 5 deletions
diff --git a/src/sort.c b/src/sort.c index 3dc7ae0b7..a8d0c142b 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); @@ -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'; } |