From: Paul Eggert Date: Tue, 10 Aug 2010 20:49:41 +0000 (-0700) Subject: sort: free/xmalloc rather than xrealloc X-Git-Tag: v8.6~69 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2050a5913b0ee2e08de22db29340865f7f0ee608;p=thirdparty%2Fcoreutils.git sort: free/xmalloc rather than xrealloc * src/sort.c (compare_random): Use free/xmalloc rather than xrealloc, since the old buffer contents need not be preserved. Also, don't fail if the guessed-sized malloc fails. Suggested by Bruno Haible. --- diff --git a/src/sort.c b/src/sort.c index 084f4e3280..3dc7ae0b79 100644 --- a/src/sort.c +++ b/src/sort.c @@ -2056,7 +2056,13 @@ compare_random (char *restrict texta, size_t lena, if (bufsize < guess_bufsize) { bufsize = MAX (guess_bufsize, bufsize * 3 / 2); - buf = allocated = xrealloc (allocated, bufsize); + free (allocated); + buf = allocated = malloc (bufsize); + if (! buf) + { + buf = stackbuf; + bufsize = sizeof stackbuf; + } } size_t sizea = @@ -2074,7 +2080,8 @@ compare_random (char *restrict texta, size_t lena, bufsize = sizea + sizeb; if (bufsize < SIZE_MAX / 3) bufsize = bufsize * 3 / 2; - buf = allocated = xrealloc (allocated, bufsize); + free (allocated); + buf = allocated = xmalloc (bufsize); if (texta < lima) strxfrm (buf, texta, sizea); if (textb < limb)