diff options
author | Bruno Haible <bruno@clisp.org> | 2010-01-31 17:30:24 +0100 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2010-01-31 17:30:24 +0100 |
commit | 1150c074e8e94a4f310915f336cb01d3f196788c (patch) | |
tree | 8aac7ff4344d03ec87872f3b989b41d972cfe8c0 | |
parent | 6c53c9025551ed72e7011f8de83aca2639bc5bfa (diff) | |
download | gnulib-1150c074e8e94a4f310915f336cb01d3f196788c.tar.gz |
Work around getline() bug on FreeBSD 8.0.
-rw-r--r-- | ChangeLog | 9 | ||||
-rw-r--r-- | doc/posix-functions/getline.texi | 4 | ||||
-rw-r--r-- | m4/getline.m4 | 35 | ||||
-rw-r--r-- | tests/test-getline.c | 19 |
4 files changed, 52 insertions, 15 deletions
@@ -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 diff --git a/doc/posix-functions/getline.texi b/doc/posix-functions/getline.texi index 7a410b7fc5..e00f6b0c8b 100644 --- a/doc/posix-functions/getline.texi +++ b/doc/posix-functions/getline.texi @@ -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: diff --git a/m4/getline.m4 b/m4/getline.m4 index 5b8a712fbc..7fca58ecf2 100644 --- a/m4/getline.m4 +++ b/m4/getline.m4 @@ -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. diff --git a/tests/test-getline.c b/tests/test-getline.c index 7112b52a4c..6a661ce530 100644 --- a/tests/test-getline.c +++ b/tests/test-getline.c @@ -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); |