summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>1996-11-02 03:47:04 +0000
committerJim Meyering <jim@meyering.net>1996-11-02 03:47:04 +0000
commitfebf70b73ffa2a61117e9bade6400d19e6f49344 (patch)
treecde4263f07105b78a4e72c18954e42a44c445f19
parentb5a71af82a6b7374b180a65e29421758e4afe129 (diff)
downloadcoreutils-febf70b73ffa2a61117e9bade6400d19e6f49344.tar.xz
[!ENABLE_ASSERTIONS]: Guard NDEBUG definition.
(checkfp): Fix off-by-one error that resulted in writing one byte beyond the end of a malloc'd buffer. It caused `sort -c' to segfault on Linux systems having a relatively recent libc. Before, running the command, perl -e "print 'x' x 30, \"\n\";"|sort -c would provoke the memory overrun (though not necessarily the failure). Add an assertion.
-rw-r--r--src/sort.c12
1 files changed, 9 insertions, 3 deletions
diff --git a/src/sort.c b/src/sort.c
index 9db737f68..ade06f2f0 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -27,7 +27,9 @@
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
-#define NDEBUG 1
+#ifndef ENABLE_ASSERTIONS
+# define NDEBUG 1
+#endif
#include <assert.h>
#include "system.h"
#include "long-options.h"
@@ -1247,12 +1249,16 @@ checkfp (FILE *fp)
/* Save the last line of the buffer and refill the buffer. */
prev_line = lines.lines + (lines.used - 1);
- if (prev_line->length > alloc)
+ if (prev_line->length + 1 > alloc)
+ {
+ do
{
- while (prev_line->length + 1 > alloc)
alloc *= 2;
+ }
+ while (alloc < prev_line->length + 1);
temp.text = xrealloc (temp.text, alloc);
}
+ assert (prev_line->length + 1 <= alloc);
memcpy (temp.text, prev_line->text, prev_line->length + 1);
temp.length = prev_line->length;
temp.keybeg = temp.text + (prev_line->keybeg - prev_line->text);