From 1150c074e8e94a4f310915f336cb01d3f196788c Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 31 Jan 2010 17:30:24 +0100 Subject: [PATCH] Work around getline() bug on FreeBSD 8.0. --- ChangeLog | 9 +++++++++ doc/posix-functions/getline.texi | 4 ++++ m4/getline.m4 | 35 ++++++++++++++++++++++++----------- tests/test-getline.c | 19 +++++++++++++++---- 4 files changed, 52 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index 384307830..d4d877625 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2010-01-31 Bruno Haible + + 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 via Eric Blake. + 2010-01-28 Eric Blake regex: fix build failure diff --git a/doc/posix-functions/getline.texi b/doc/posix-functions/getline.texi index 7a410b7fc..e00f6b0c8 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 5b8a712fb..7fca58ecf 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 # include # include 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 7112b52a4..6a661ce53 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); -- 2.11.0