]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
[!ENABLE_ASSERTIONS]: Guard NDEBUG definition.
authorJim Meyering <jim@meyering.net>
Sat, 2 Nov 1996 03:47:04 +0000 (03:47 +0000)
committerJim Meyering <jim@meyering.net>
Sat, 2 Nov 1996 03:47:04 +0000 (03:47 +0000)
(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.

src/sort.c

index 9db737f68416329f9fb4f83a59f9eec75546e530..ade06f2f0514196bde13b1ccd15e9c2847e86147 100644 (file)
@@ -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);