From: Jim Meyering Date: Sat, 2 Nov 1996 03:47:04 +0000 (+0000) Subject: [!ENABLE_ASSERTIONS]: Guard NDEBUG definition. X-Git-Tag: TEXTUTILS-1_19q~181 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=febf70b73ffa2a61117e9bade6400d19e6f49344;p=thirdparty%2Fcoreutils.git [!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. --- diff --git a/src/sort.c b/src/sort.c index 9db737f684..ade06f2f05 100644 --- a/src/sort.c +++ b/src/sort.c @@ -27,7 +27,9 @@ #include #include #include -#define NDEBUG 1 +#ifndef ENABLE_ASSERTIONS +# define NDEBUG 1 +#endif #include #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);