Mercurial > hg > octave-lyh > gnulib-hg
changeset 12837:fff2b96bad5e
Work around getline() bug on FreeBSD 8.0.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Sun, 31 Jan 2010 17:30:24 +0100 |
parents | 39251fa967fc |
children | ed6e77e08386 |
files | ChangeLog doc/posix-functions/getline.texi m4/getline.m4 tests/test-getline.c |
diffstat | 4 files changed, 52 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- 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
--- a/doc/posix-functions/getline.texi +++ b/doc/posix-functions/getline.texi @@ -17,6 +17,10 @@ @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:
--- 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 @@ 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.
--- a/tests/test-getline.c +++ b/tests/test-getline.c @@ -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 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. */ - free (line); line = malloc (1); len = 0; result = getline (&line, &len, f);