From 9c6d8d0b4630263a90dde69356379af7cb5c7592 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 20 May 2017 15:26:26 +0200 Subject: [PATCH] Avoid wrong configure results with gcc -fsanitize=address. This completes the work done on 2016-02-06 on this topic. * m4/memmem.m4 (gl_FUNC_MEMMEM): Free allocated memory before returning. * m4/getcwd.m4 (gl_FUNC_GETCWD_NULL): Likewise. * m4/strcasestr.m4 (gl_FUNC_STRCASESTR): Likewise. * m4/strstr.m4 (gl_FUNC_STRSTR): Likewise. * m4/fopen.m4 (gl_FUNC_FOPEN): Close allocated FILE streams before returning. * m4/fflush.m4 (gl_FUNC_FFLUSH_STDIN): Likewise. * m4/fpurge.m4 (gl_FUNC_FPURGE): Likewise. * m4/ftello.m4 (gl_FUNC_FTELLO): Likewise. * m4/rmdir-errno.m4 (gl_FUNC_RMDIR_NOTEMPTY): Likewise. * m4/signbit.m4 (gl_FLOATTYPE_SIGN_LOCATION): Likewise. * m4/ungetc.m4 (gl_FUNC_UNGETC_WORKS): Likewise. * m4/getdelim.m4 (gl_FUNC_GETDELIM): Close allocated FILE streams and free allocated memory before returning. * m4/getline.m4 (gl_FUNC_GETLINE): Likewise. * m4/d-ino.m4 (gl_CHECK_TYPE_STRUCT_DIRENT_D_INO): Close allocated DIR objects before returning. * m4/iconv.m4 (AM_ICONV_LINK): Close allocated iconv_t handles before returning. --- ChangeLog | 24 +++++++++++++++++++++ m4/d-ino.m4 | 8 +++---- m4/fflush.m4 | 15 +++++++------- m4/fopen.m4 | 8 +++++-- m4/fpurge.m4 | 53 +++++++++++++++++++++++++++++++---------------- m4/ftello.m4 | 22 ++++++++++---------- m4/getcwd.m4 | 6 +++--- m4/getdelim.m4 | 6 +++--- m4/getline.m4 | 6 +++--- m4/iconv.m4 | 32 +++++++++++++++++++--------- m4/memmem.m4 | 5 ++++- m4/rmdir-errno.m4 | 3 ++- m4/signbit.m4 | 5 ++++- m4/strcasestr.m4 | 5 ++++- m4/strstr.m4 | 5 ++++- m4/ungetc.m4 | 38 +++++++++++++++++++++------------ 16 files changed, 162 insertions(+), 79 deletions(-) diff --git a/ChangeLog b/ChangeLog index 25a0f69f8c..8ebdd7b5d0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,27 @@ +2017-05-20 Bruno Haible + + Avoid wrong configure results with gcc -fsanitize=address. + This completes the work done on 2016-02-06 on this topic. + * m4/memmem.m4 (gl_FUNC_MEMMEM): Free allocated memory before returning. + * m4/getcwd.m4 (gl_FUNC_GETCWD_NULL): Likewise. + * m4/strcasestr.m4 (gl_FUNC_STRCASESTR): Likewise. + * m4/strstr.m4 (gl_FUNC_STRSTR): Likewise. + * m4/fopen.m4 (gl_FUNC_FOPEN): Close allocated FILE streams before + returning. + * m4/fflush.m4 (gl_FUNC_FFLUSH_STDIN): Likewise. + * m4/fpurge.m4 (gl_FUNC_FPURGE): Likewise. + * m4/ftello.m4 (gl_FUNC_FTELLO): Likewise. + * m4/rmdir-errno.m4 (gl_FUNC_RMDIR_NOTEMPTY): Likewise. + * m4/signbit.m4 (gl_FLOATTYPE_SIGN_LOCATION): Likewise. + * m4/ungetc.m4 (gl_FUNC_UNGETC_WORKS): Likewise. + * m4/getdelim.m4 (gl_FUNC_GETDELIM): Close allocated FILE streams and + free allocated memory before returning. + * m4/getline.m4 (gl_FUNC_GETLINE): Likewise. + * m4/d-ino.m4 (gl_CHECK_TYPE_STRUCT_DIRENT_D_INO): Close allocated DIR + objects before returning. + * m4/iconv.m4 (AM_ICONV_LINK): Close allocated iconv_t handles before + returning. + 2017-05-20 Bruno Haible gnulib-tool: Don't create hard links between gnulib and its testdirs. diff --git a/m4/d-ino.m4 b/m4/d-ino.m4 index 79731ca7cf..65cd4d8580 100644 --- a/m4/d-ino.m4 +++ b/m4/d-ino.m4 @@ -1,4 +1,4 @@ -# serial 15 +# serial 16 dnl From Jim Meyering. dnl @@ -29,11 +29,11 @@ AC_DEFUN([gl_CHECK_TYPE_STRUCT_DIRENT_D_INO], return 1; e = readdir (dp); if (! e) - return 2; + { closedir (dp); return 2; } if (lstat (e->d_name, &st) != 0) - return 3; + { closedir (dp); return 3; } if (e->d_ino != st.st_ino) - return 4; + { closedir (dp); return 4; } closedir (dp); return 0; ]])], diff --git a/m4/fflush.m4 b/m4/fflush.m4 index 1d1f1ba1dd..7732234ee9 100644 --- a/m4/fflush.m4 +++ b/m4/fflush.m4 @@ -1,4 +1,4 @@ -# fflush.m4 serial 15 +# fflush.m4 serial 16 # Copyright (C) 2007-2017 Free Software Foundation, Inc. # This file is free software; the Free Software Foundation @@ -44,16 +44,16 @@ AC_DEFUN([gl_FUNC_FFLUSH_STDIN], return 1; fd = fileno (f); if (fd < 0 || fread (buffer, 1, 5, f) != 5) - return 2; + { fclose (f); return 2; } /* For deterministic results, ensure f read a bigger buffer. */ if (lseek (fd, 0, SEEK_CUR) == 5) - return 3; + { fclose (f); return 3; } /* POSIX requires fflush-fseek to set file offset of fd. This fails on BSD systems and on mingw. */ if (fflush (f) != 0 || fseek (f, 0, SEEK_CUR) != 0) - return 4; + { fclose (f); return 4; } if (lseek (fd, 0, SEEK_CUR) != 5) - return 5; + { fclose (f); return 5; } /* Verify behaviour of fflush after ungetc. See */ /* Verify behaviour of fflush after a backup ungetc. This fails on @@ -62,14 +62,15 @@ AC_DEFUN([gl_FUNC_FFLUSH_STDIN], ungetc (c, f); fflush (f); if (fgetc (f) != c) - return 6; + { fclose (f); return 6; } /* Verify behaviour of fflush after a non-backup ungetc. This fails on glibc 2.8 and on BSD systems. */ c = fgetc (f); ungetc ('@', f); fflush (f); if (fgetc (f) != c) - return 7; + { fclose (f); return 7; } + fclose (f); return 0; ]])], [gl_cv_func_fflush_stdin=yes], [gl_cv_func_fflush_stdin=no], [gl_cv_func_fflush_stdin=cross]) diff --git a/m4/fopen.m4 b/m4/fopen.m4 index 0d09e4e43b..3772e5066a 100644 --- a/m4/fopen.m4 +++ b/m4/fopen.m4 @@ -1,4 +1,4 @@ -# fopen.m4 serial 9 +# fopen.m4 serial 10 dnl Copyright (C) 2007-2017 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -27,7 +27,11 @@ AC_DEFUN([gl_FUNC_FOPEN], #include int main () { - return fopen ("conftest.sl/", "w") != NULL; + FILE *fp = fopen ("conftest.sl/", "w"); + int result = (fp != NULL); + if (fp != NULL) + fclose (fp); + return result; }]])], [gl_cv_func_fopen_slash=yes], [gl_cv_func_fopen_slash=no], diff --git a/m4/fpurge.m4 b/m4/fpurge.m4 index 57be1b9e40..3533f4b8f3 100644 --- a/m4/fpurge.m4 +++ b/m4/fpurge.m4 @@ -1,4 +1,4 @@ -# fpurge.m4 serial 7 +# fpurge.m4 serial 8 dnl Copyright (C) 2007, 2009-2017 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -14,23 +14,40 @@ AC_DEFUN([gl_FUNC_FPURGE], HAVE_FPURGE=1 # Detect BSD bug. Only cygwin 1.7 is known to be immune. AC_CACHE_CHECK([whether fpurge works], [gl_cv_func_fpurge_works], - [AC_RUN_IFELSE([AC_LANG_PROGRAM([[#include -]], [FILE *f = fopen ("conftest.txt", "w+"); - if (!f) return 1; - if (fputc ('a', f) != 'a') return 2; - rewind (f); - if (fgetc (f) != 'a') return 3; - if (fgetc (f) != EOF) return 4; - if (fpurge (f) != 0) return 5; - if (putc ('b', f) != 'b') return 6; - if (fclose (f) != 0) return 7; - if ((f = fopen ("conftest.txt", "r")) == NULL) return 8; - if (fgetc (f) != 'a') return 9; - if (fgetc (f) != 'b') return 10; - if (fgetc (f) != EOF) return 11; - if (fclose (f) != 0) return 12; - if (remove ("conftest.txt") != 0) return 13; - return 0;])], + [AC_RUN_IFELSE( + [AC_LANG_PROGRAM( + [[#include +]], + [FILE *f = fopen ("conftest.txt", "w+"); + if (!f) + return 1; + if (fputc ('a', f) != 'a') + { fclose (f); return 2; } + rewind (f); + if (fgetc (f) != 'a') + { fclose (f); return 3; } + if (fgetc (f) != EOF) + { fclose (f); return 4; } + if (fpurge (f) != 0) + { fclose (f); return 5; } + if (putc ('b', f) != 'b') + { fclose (f); return 6; } + if (fclose (f) != 0) + return 7; + if ((f = fopen ("conftest.txt", "r")) == NULL) + return 8; + if (fgetc (f) != 'a') + { fclose (f); return 9; } + if (fgetc (f) != 'b') + { fclose (f); return 10; } + if (fgetc (f) != EOF) + { fclose (f); return 11; } + if (fclose (f) != 0) + return 12; + if (remove ("conftest.txt") != 0) + return 13; + return 0; + ])], [gl_cv_func_fpurge_works=yes], [gl_cv_func_fpurge_works=no], [gl_cv_func_fpurge_works='guessing no'])]) if test "x$gl_cv_func_fpurge_works" != xyes; then diff --git a/m4/ftello.m4 b/m4/ftello.m4 index 0867c2a645..8a4cdc5308 100644 --- a/m4/ftello.m4 +++ b/m4/ftello.m4 @@ -1,4 +1,4 @@ -# ftello.m4 serial 11 +# ftello.m4 serial 12 dnl Copyright (C) 2007-2017 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -73,7 +73,7 @@ main (void) if (fp == NULL) return 70; if (fwrite ("foogarsh", 1, 8, fp) < 8) - return 71; + { fclose (fp); return 71; } if (fclose (fp)) return 72; @@ -84,19 +84,19 @@ main (void) if (fp == NULL) return 73; if (fseek (fp, -1, SEEK_END)) - return 74; + { fclose (fp); return 74; } if (!(getc (fp) == 'h')) - return 1; + { fclose (fp); return 1; } if (!(getc (fp) == EOF)) - return 2; + { fclose (fp); return 2; } if (!(ftell (fp) == 8)) - return 3; + { fclose (fp); return 3; } if (!(ftell (fp) == 8)) - return 4; + { fclose (fp); return 4; } if (!(putc ('!', fp) == '!')) - return 5; + { fclose (fp); return 5; } if (!(ftell (fp) == 9)) - return 6; + { fclose (fp); return 6; } if (!(fclose (fp) == 0)) return 7; fp = fopen (TESTFILE, "r"); @@ -105,9 +105,9 @@ main (void) { char buf[10]; if (!(fread (buf, 1, 10, fp) == 9)) - return 10; + { fclose (fp); return 10; } if (!(memcmp (buf, "foogarsh!", 9) == 0)) - return 11; + { fclose (fp); return 11; } } if (!(fclose (fp) == 0)) return 12; diff --git a/m4/getcwd.m4 b/m4/getcwd.m4 index ec6b637c50..a3f62c4622 100644 --- a/m4/getcwd.m4 +++ b/m4/getcwd.m4 @@ -6,7 +6,7 @@ # with or without modifications, as long as this notice is preserved. # Written by Paul Eggert. -# serial 13 +# serial 14 AC_DEFUN([gl_FUNC_GETCWD_NULL], [ @@ -37,9 +37,9 @@ AC_DEFUN([gl_FUNC_GETCWD_NULL], if (! f) return 2; if (f[0] != '/') - return 3; + { free (f); return 3; } if (f[1] != '\0') - return 4; + { free (f); return 4; } free (f); return 0; } diff --git a/m4/getdelim.m4 b/m4/getdelim.m4 index 24adb94f31..67afdbab09 100644 --- a/m4/getdelim.m4 +++ b/m4/getdelim.m4 @@ -1,4 +1,4 @@ -# getdelim.m4 serial 11 +# getdelim.m4 serial 12 dnl Copyright (C) 2005-2007, 2009-2017 Free Software Foundation, Inc. dnl @@ -39,7 +39,7 @@ AC_DEFUN([gl_FUNC_GETDELIM], size_t siz = 0; int len = getdelim (&line, &siz, '\n', in); if (!(len == 4 && line && strcmp (line, "foo\n") == 0)) - return 2; + { free (line); fclose (in); return 2; } } { /* Test result for a NULL buffer and a non-zero size. @@ -47,7 +47,7 @@ AC_DEFUN([gl_FUNC_GETDELIM], char *line = NULL; size_t siz = (size_t)(~0) / 4; if (getdelim (&line, &siz, '\n', in) == -1) - return 3; + { fclose (in); return 3; } free (line); } fclose (in); diff --git a/m4/getline.m4 b/m4/getline.m4 index dcea772e56..bd91befab7 100644 --- a/m4/getline.m4 +++ b/m4/getline.m4 @@ -1,4 +1,4 @@ -# getline.m4 serial 27 +# getline.m4 serial 28 dnl Copyright (C) 1998-2003, 2005-2007, 2009-2017 Free Software Foundation, dnl Inc. @@ -46,7 +46,7 @@ AC_DEFUN([gl_FUNC_GETLINE], size_t siz = 0; int len = getline (&line, &siz, in); if (!(len == 4 && line && strcmp (line, "foo\n") == 0)) - return 2; + { free (line); fclose (in); return 2; } free (line); } { @@ -55,7 +55,7 @@ AC_DEFUN([gl_FUNC_GETLINE], char *line = NULL; size_t siz = (size_t)(~0) / 4; if (getline (&line, &siz, in) == -1) - return 3; + { fclose (in); return 3; } free (line); } fclose (in); diff --git a/m4/iconv.m4 b/m4/iconv.m4 index bdafc54e30..81ac1a6b45 100644 --- a/m4/iconv.m4 +++ b/m4/iconv.m4 @@ -1,4 +1,4 @@ -# iconv.m4 serial 20 +# iconv.m4 serial 21 dnl Copyright (C) 2000-2002, 2007-2014, 2016 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -167,15 +167,27 @@ AC_DEFUN([AM_ICONV_LINK], #endif /* Test against HP-UX 11.11 bug: No converter from EUC-JP to UTF-8 is provided. */ - if (/* Try standardized names. */ - iconv_open ("UTF-8", "EUC-JP") == (iconv_t)(-1) - /* Try IRIX, OSF/1 names. */ - && iconv_open ("UTF-8", "eucJP") == (iconv_t)(-1) - /* Try AIX names. */ - && iconv_open ("UTF-8", "IBM-eucJP") == (iconv_t)(-1) - /* Try HP-UX names. */ - && iconv_open ("utf8", "eucJP") == (iconv_t)(-1)) - result |= 16; + { + /* Try standardized names. */ + iconv_t cd1 = iconv_open ("UTF-8", "EUC-JP"); + /* Try IRIX, OSF/1 names. */ + iconv_t cd2 = iconv_open ("UTF-8", "eucJP"); + /* Try AIX names. */ + iconv_t cd3 = iconv_open ("UTF-8", "IBM-eucJP"); + /* Try HP-UX names. */ + iconv_t cd4 = iconv_open ("utf8", "eucJP"); + if (cd1 == (iconv_t)(-1) && cd2 == (iconv_t)(-1) + && cd3 == (iconv_t)(-1) && cd4 == (iconv_t)(-1)) + result |= 16; + if (cd1 != (iconv_t)(-1)) + iconv_close (cd1); + if (cd2 != (iconv_t)(-1)) + iconv_close (cd2); + if (cd3 != (iconv_t)(-1)) + iconv_close (cd3); + if (cd4 != (iconv_t)(-1)) + iconv_close (cd4); + } return result; ]])], [am_cv_func_iconv_works=yes], , diff --git a/m4/memmem.m4 b/m4/memmem.m4 index 9ef4dd494f..334cb665e7 100644 --- a/m4/memmem.m4 +++ b/m4/memmem.m4 @@ -1,4 +1,4 @@ -# memmem.m4 serial 24 +# memmem.m4 serial 25 dnl Copyright (C) 2002-2004, 2007-2017 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -113,6 +113,9 @@ static void quit (int sig) { _exit (sig + 128); } if (!memmem (haystack, 2 * m + 1, needle, m + 1)) result |= 1; } + /* Free allocated memory, in case some sanitizer is watching. */ + free (haystack); + free (needle); return result; ]])], [gl_cv_func_memmem_works_fast=yes], [gl_cv_func_memmem_works_fast=no], diff --git a/m4/rmdir-errno.m4 b/m4/rmdir-errno.m4 index c290a4822b..a389329da0 100644 --- a/m4/rmdir-errno.m4 +++ b/m4/rmdir-errno.m4 @@ -1,4 +1,4 @@ -# serial 10 +# serial 11 # Copyright (C) 2000-2001, 2005-2006, 2009-2017 Free Software Foundation, Inc. # This file is free software; the Free Software Foundation @@ -33,6 +33,7 @@ AC_DEFUN([gl_FUNC_RMDIR_NOTEMPTY], val = errno; s = fopen ("confdir2/errno", "w"); fprintf (s, "%d\n", val); + fclose (s); return 0; } ]])], diff --git a/m4/signbit.m4 b/m4/signbit.m4 index 9d2b0a8db1..f387ee91d6 100644 --- a/m4/signbit.m4 +++ b/m4/signbit.m4 @@ -1,4 +1,4 @@ -# signbit.m4 serial 13 +# signbit.m4 serial 14 dnl Copyright (C) 2007-2017 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -272,6 +272,7 @@ int main () { /* More than one bit difference. */ fprintf (fp, "unknown"); + fclose (fp); return 2; } if (x) @@ -284,6 +285,7 @@ int main () { /* No difference. */ fprintf (fp, "unknown"); + fclose (fp); return 3; } /* Now m = plus.word[k] ^ ~minus.word[k]. */ @@ -292,6 +294,7 @@ int main () /* Oh? The sign bit is set in the positive and cleared in the negative numbers? */ fprintf (fp, "unknown"); + fclose (fp); return 4; } for (i = 0; ; i++) diff --git a/m4/strcasestr.m4 b/m4/strcasestr.m4 index 7d15c651e1..3af9582ed6 100644 --- a/m4/strcasestr.m4 +++ b/m4/strcasestr.m4 @@ -1,4 +1,4 @@ -# strcasestr.m4 serial 21 +# strcasestr.m4 serial 22 dnl Copyright (C) 2005, 2007-2017 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -102,6 +102,9 @@ static void quit (int sig) { _exit (sig + 128); } if (!strcasestr (haystack, needle)) result |= 1; } + /* Free allocated memory, in case some sanitizer is watching. */ + free (haystack); + free (needle); return result; ]])], [gl_cv_func_strcasestr_linear=yes], [gl_cv_func_strcasestr_linear=no], diff --git a/m4/strstr.m4 b/m4/strstr.m4 index 9f7da641de..4b472ee623 100644 --- a/m4/strstr.m4 +++ b/m4/strstr.m4 @@ -1,4 +1,4 @@ -# strstr.m4 serial 17 +# strstr.m4 serial 18 dnl Copyright (C) 2008-2017 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -99,6 +99,9 @@ static void quit (int sig) { _exit (sig + 128); } if (!strstr (haystack, needle)) result |= 1; } + /* Free allocated memory, in case some sanitizer is watching. */ + free (haystack); + free (needle); return result; ]])], [gl_cv_func_strstr_linear=yes], [gl_cv_func_strstr_linear=no], diff --git a/m4/ungetc.m4 b/m4/ungetc.m4 index 9fd3db9ce5..72e833490f 100644 --- a/m4/ungetc.m4 +++ b/m4/ungetc.m4 @@ -1,4 +1,4 @@ -# ungetc.m4 serial 3 +# ungetc.m4 serial 4 dnl Copyright (C) 2009-2017 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -14,19 +14,31 @@ AC_DEFUN_ONCE([gl_FUNC_UNGETC_WORKS], [AC_RUN_IFELSE([AC_LANG_PROGRAM([[ #include ]], [FILE *f; - if (!(f = fopen ("conftest.tmp", "w+"))) return 1; - if (fputs ("abc", f) < 0) return 2; + if (!(f = fopen ("conftest.tmp", "w+"))) + return 1; + if (fputs ("abc", f) < 0) + { fclose (f); return 2; } rewind (f); - if (fgetc (f) != 'a') return 3; - if (fgetc (f) != 'b') return 4; - if (ungetc ('d', f) != 'd') return 5; - if (ftell (f) != 1) return 6; - if (fgetc (f) != 'd') return 7; - if (ftell (f) != 2) return 8; - if (fseek (f, 0, SEEK_CUR) != 0) return 9; - if (ftell (f) != 2) return 10; - if (fgetc (f) != 'c') return 11; - fclose (f); remove ("conftest.tmp");])], + if (fgetc (f) != 'a') + { fclose (f); return 3; } + if (fgetc (f) != 'b') + { fclose (f); return 4; } + if (ungetc ('d', f) != 'd') + { fclose (f); return 5; } + if (ftell (f) != 1) + { fclose (f); return 6; } + if (fgetc (f) != 'd') + { fclose (f); return 7; } + if (ftell (f) != 2) + { fclose (f); return 8; } + if (fseek (f, 0, SEEK_CUR) != 0) + { fclose (f); return 9; } + if (ftell (f) != 2) + { fclose (f); return 10; } + if (fgetc (f) != 'c') + { fclose (f); return 11; } + fclose (f); + remove ("conftest.tmp");])], [gl_cv_func_ungetc_works=yes], [gl_cv_func_ungetc_works=no], [case "$host_os" in # Guess yes on glibc and bionic systems. -- 2.39.5