From 9899e6cd853baf2704fdb52e55270ffbabd37dff Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Tue, 10 Aug 2010 20:25:20 -0700 Subject: [PATCH] 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. --- 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 3dc7ae0b79..a8d0c142bf 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 2c0d94790c..ac988816be 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'; } -- 2.47.3