summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2010-08-10 13:49:41 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2010-08-10 13:49:57 -0700
commit2050a5913b0ee2e08de22db29340865f7f0ee608 (patch)
treef74c6793684f5933aaf738c5d068d0618c02d59e
parent7861d74ba8d1e237c22e5ba0d413806fd8436c03 (diff)
downloadcoreutils-2050a5913b0ee2e08de22db29340865f7f0ee608.tar.xz
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.
-rw-r--r--src/sort.c11
1 files changed, 9 insertions, 2 deletions
diff --git a/src/sort.c b/src/sort.c
index 084f4e328..3dc7ae0b7 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)