]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
sort: free/xmalloc rather than xrealloc
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 10 Aug 2010 20:49:41 +0000 (13:49 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 10 Aug 2010 20:49:57 +0000 (13:49 -0700)
* 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.

src/sort.c

index 084f4e3280b0c88666fa3f6bd4c032eea356a236..3dc7ae0b79e44c8bea6e0aa55af889aeacc68717 100644 (file)
@@ -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)