Work around getline() bug on FreeBSD 8.0.
authorBruno Haible <bruno@clisp.org>
Sun, 31 Jan 2010 16:30:24 +0000 (17:30 +0100)
committerBruno Haible <bruno@clisp.org>
Sun, 31 Jan 2010 16:30:24 +0000 (17:30 +0100)
ChangeLog
doc/posix-functions/getline.texi
m4/getline.m4
tests/test-getline.c

index 3843078..d4d8776 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2010-01-31  Bruno Haible  <bruno@clisp.org>
+
+       Work around getline() bug on FreeBSD 8.0.
+       * m4/getline.m4 (gl_FUNC_GETLINE): Also test result for a NULL buffer
+       and a non-zero size.
+       * tests/test-getline.c (main): Likewise.
+       * doc/posix-functions/getline.texi: Mention the FreeBSD bug.
+       Reported by Dennis <noordsij@cs.helsinki.fi> via Eric Blake.
+
 2010-01-28  Eric Blake  <ebb9@byu.net>
 
        regex: fix build failure
index 7a410b7..e00f6b0 100644 (file)
@@ -17,6 +17,10 @@ BeOS.
 @item
 Some platforms provide a function by this name but with the wrong
 signature, for example in -linet.
+@item
+This function crashes when passed a pointer to a NULL buffer together with a
+pointer to a non-zero buffer size on some platforms:
+FreeBSD 8.0.
 @end itemize
 
 Portability problems not fixed by Gnulib:
index 5b8a712..7fca58e 100644 (file)
@@ -1,4 +1,4 @@
-# getline.m4 serial 20
+# getline.m4 serial 21
 
 dnl Copyright (C) 1998-2003, 2005-2007, 2009-2010 Free Software Foundation,
 dnl Inc.
@@ -24,26 +24,39 @@ AC_DEFUN([gl_FUNC_GETLINE],
 
   gl_getline_needs_run_time_check=no
   AC_CHECK_FUNC([getline],
-                dnl Found it in some library.  Verify that it works.
-                gl_getline_needs_run_time_check=yes,
-                am_cv_func_working_getline=no)
+                [dnl Found it in some library.  Verify that it works.
+                 gl_getline_needs_run_time_check=yes],
+                [am_cv_func_working_getline=no])
   if test $gl_getline_needs_run_time_check = yes; then
     AC_CACHE_CHECK([for working getline function], [am_cv_func_working_getline],
-    [echo fooN |tr -d '\012'|tr N '\012' > conftest.data
+    [echo fooNbarN | tr -d '\012' | tr N '\012' > conftest.data
     AC_RUN_IFELSE([AC_LANG_SOURCE([[
 #    include <stdio.h>
 #    include <stdlib.h>
 #    include <string.h>
     int main ()
-    { /* Based on a test program from Karl Heuer.  */
-      char *line = NULL;
-      size_t siz = 0;
-      int len;
+    {
       FILE *in = fopen ("./conftest.data", "r");
       if (!in)
         return 1;
-      len = getline (&line, &siz, in);
-      exit ((len == 4 && line && strcmp (line, "foo\n") == 0) ? 0 : 1);
+      {
+        /* Test result for a NULL buffer and a zero size.
+           Based on a test program from Karl Heuer.  */
+        char *line = NULL;
+        size_t siz = 0;
+        int len = getline (&line, &siz, in);
+        if (!(len == 4 && line && strcmp (line, "foo\n") == 0))
+          return 1;
+      }
+      {
+        /* Test result for a NULL buffer and a non-zero size.
+           This crashes on FreeBSD 8.0.  */
+        char *line = NULL;
+        size_t siz = (size_t)(~0) / 4;
+        if (getline (&line, &siz, in) == -1)
+          return 1;
+      }
+      return 0;
     }
     ]])], [am_cv_func_working_getline=yes] dnl The library version works.
     , [am_cv_func_working_getline=no] dnl The library version does NOT work.
index 7112b52..6a661ce 100644 (file)
@@ -33,13 +33,13 @@ int
 main (void)
 {
   FILE *f;
-  char *line = NULL;
-  size_t len = 0;
+  char *line;
+  size_t len;
   ssize_t result;
 
   /* Create test file.  */
   f = fopen ("test-getline.txt", "wb");
-  if (!f || fwrite ("a\nbc\nd\0f", 1, 8, f) != 8 || fclose (f) != 0)
+  if (!f || fwrite ("a\nA\nbc\nd\0f", 1, 10, f) != 10 || fclose (f) != 0)
     {
       fputs ("Failed to create sample file.\n", stderr);
       remove ("test-getline.txt");
@@ -54,13 +54,24 @@ main (void)
     }
 
   /* Test initial allocation, which must include trailing NUL.  */
+  line = NULL;
+  len = 0;
   result = getline (&line, &len, f);
   ASSERT (result == 2);
   ASSERT (strcmp (line, "a\n") == 0);
   ASSERT (2 < len);
+  free (line);
 
-  /* Test growth of buffer, must not leak.  */
+  /* Test initial allocation again, with line = NULL and len != 0.  */
+  line = NULL;
+  len = (size_t)(~0) / 4;
+  result = getline (&line, &len, f);
+  ASSERT (result == 2);
+  ASSERT (strcmp (line, "A\n") == 0);
+  ASSERT (2 < len);
   free (line);
+
+  /* Test growth of buffer, must not leak.  */
   line = malloc (1);
   len = 0;
   result = getline (&line, &len, f);