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.
This commit is contained in:
Paul Eggert committed 2010-08-10 20:25:56 -07:00
1 parent 2050a5913b
commit 9899e6cd85
2 files changed
+10 -5

No files matched your search

+4 -2
View 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);
+6 -3
View File
@@ -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';
}