bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug-tar] getline() differences FreeBSD / GNU libc


From: Bruno Haible
Subject: Re: [Bug-tar] getline() differences FreeBSD / GNU libc
Date: Sun, 31 Jan 2010 17:30:53 +0100
User-agent: KMail/1.9.9

Eric Blake wrote:
> FreeBSD is buggy.  Rewording the POSIX requirement slightly: if lineptr is
> NULL, then it is irrelevant that linecapp is non-zero.

I agree.

> Gnulib needs to work around this portability flaw, and guarantee a working
> getline implementation even on fBSD, at which point, your patch to tar to
> explicitly initialize bufsize to 0 is unnecessary.

Implemented as follows. Since I don't have access to a machine with FreeBSD 8.0,
I would appreciate if someone could test this on FreeBSD 8.0:
  $ ./gnulib-tool --create-testdir --dir=/tmp/testdir --with-tests getline
  $ cd /tmp/testdir
  $ ./configure CPPFLAGS=-Wall
  # What is the configure output regarding getline and getdelim here?
  $ make; make check

Bruno


2010-01-31  Bruno Haible  <address@hidden>

        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 <address@hidden> via Eric Blake.

--- doc/posix-functions/getline.texi.orig       Sun Jan 31 17:20:48 2010
+++ doc/posix-functions/getline.texi    Sun Jan 31 16:49:18 2010
@@ -17,6 +17,10 @@
 @item
 Some platforms provide a function by this name but with the wrong
 signature, for example in -linet.
address@hidden
+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:
--- m4/getline.m4.orig  Sun Jan 31 17:20:48 2010
+++ m4/getline.m4       Sun Jan 31 17:07:24 2010
@@ -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 @@
 
   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.
--- tests/test-getline.c.orig   Sun Jan 31 17:20:48 2010
+++ tests/test-getline.c        Sun Jan 31 17:20:22 2010
@@ -33,13 +33,13 @@
 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 @@
     }
 
   /* 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);




reply via email to

[Prev in Thread] Current Thread [Next in Thread]