bug-gnulib
[Top][All Lists]
Advanced

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

Re: [PATCH] misc: port better to gcc -fsanitize=address


From: Pádraig Brady
Subject: Re: [PATCH] misc: port better to gcc -fsanitize=address
Date: Sat, 6 Feb 2016 19:20:59 -0800
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0

On 06/02/16 18:12, Paul Eggert wrote:
> Without these patches, ./configure CFLAGS='-fsanitize=address'
> would compute incorrect values.  This patch fixes some (but not all)
> test failures with recent glibc, with this configuration.
> * m4/acl.m4 (gl_ACL_GET_FILE):
> * m4/calloc.m4 (_AC_FUNC_CALLOC_IF):
> * m4/canonicalize.m4 (gl_FUNC_REALPATH_WORKS):
> * m4/d-ino.m4 (gl_CHECK_TYPE_STRUCT_DIRENT_D_INO):
> * m4/duplocale.m4 (gl_FUNC_DUPLOCALE):
> * m4/getcwd.m4 (gl_FUNC_GETCWD_NULL):
> * m4/getdelim.m4 (gl_FUNC_GETDELIM):
> * m4/getgroups.m4 (gl_FUNC_GETGROUPS):
> * m4/getline.m4 (gl_FUNC_GETLINE):
> * m4/malloc.m4 (_AC_FUNC_MALLOC_IF):
> * m4/realloc.m4 (_AC_FUNC_REALLOC_IF):
> * m4/regex.m4 (gl_REGEX):
> * m4/strndup.m4 (gl_FUNC_STRNDUP):
> * tests/test-calloc-gnu.c (main):
> * tests/test-duplocale.c (main):
> * tests/test-getgroups.c (main):
> * tests/test-getline.c (main):
> * tests/test-inttostr.c (main):
> * tests/test-localename.c (test_locale_name)
> (test_locale_name_thread, test_locale_name_environ)
> (test_locale_name_default):
> * tests/test-regex.c (main):
> * tests/test-setlocale1.c (main):
> * tests/test-stat.h (test_stat_func):
> Free heap-allocated storage before exiting.
> * m4/asm-underscore.m4 (gl_ASM_SYMBOL_PREFIX):
> Don't match *_foo symbols inserted by AddressSanitizer.
> * tests/test-regex.c, tests/test-stat.c: Include stdlib.h, for 'free'.
> ---
>  ChangeLog               | 35 ++++++++++++++++++++++
>  m4/acl.m4               |  7 ++---
>  m4/asm-underscore.m4    |  4 +--
>  m4/calloc.m4            | 10 +++++--
>  m4/canonicalize.m4      |  9 ++++--
>  m4/d-ino.m4             |  3 +-
>  m4/duplocale.m4         |  6 ++--
>  m4/getcwd.m4            |  4 ++-
>  m4/getdelim.m4          |  4 ++-
>  m4/getgroups.m4         |  6 ++--
>  m4/getline.m4           |  5 +++-
>  m4/malloc.m4            | 11 ++++---
>  m4/realloc.m4           | 11 ++++---
>  m4/regex.m4             |  4 ++-
>  m4/strndup.m4           |  7 +++--
>  tests/test-calloc-gnu.c | 12 ++++++--
>  tests/test-duplocale.c  |  8 ++++-
>  tests/test-getgroups.c  |  1 +
>  tests/test-getline.c    |  4 +--
>  tests/test-inttostr.c   |  2 ++
>  tests/test-localename.c | 29 ++++++++++++++----
>  tests/test-regex.c      | 78 
> ++++++++++++++++++++++++++++++++++++++++---------
>  tests/test-setlocale1.c |  2 ++
>  tests/test-stat.c       |  1 +
>  tests/test-stat.h       |  1 +
>  25 files changed, 208 insertions(+), 56 deletions(-)
> 
> diff --git a/ChangeLog b/ChangeLog
> index d353609..5e47384 100644
> --- a/ChangeLog
> +++ b/ChangeLog
> @@ -1,3 +1,38 @@
> +2016-02-06  Paul Eggert  <address@hidden>
> +
> +     misc: port better to gcc -fsanitize=address
> +     Without these patches, ./configure CFLAGS='-fsanitize=address'
> +     would compute incorrect values.  This patch fixes some (but not all)
> +     test failures with recent glibc, with this configuration.
> +     * m4/acl.m4 (gl_ACL_GET_FILE):
> +     * m4/calloc.m4 (_AC_FUNC_CALLOC_IF):
> +     * m4/canonicalize.m4 (gl_FUNC_REALPATH_WORKS):
> +     * m4/d-ino.m4 (gl_CHECK_TYPE_STRUCT_DIRENT_D_INO):
> +     * m4/duplocale.m4 (gl_FUNC_DUPLOCALE):
> +     * m4/getcwd.m4 (gl_FUNC_GETCWD_NULL):
> +     * m4/getdelim.m4 (gl_FUNC_GETDELIM):
> +     * m4/getgroups.m4 (gl_FUNC_GETGROUPS):
> +     * m4/getline.m4 (gl_FUNC_GETLINE):
> +     * m4/malloc.m4 (_AC_FUNC_MALLOC_IF):
> +     * m4/realloc.m4 (_AC_FUNC_REALLOC_IF):
> +     * m4/regex.m4 (gl_REGEX):
> +     * m4/strndup.m4 (gl_FUNC_STRNDUP):
> +     * tests/test-calloc-gnu.c (main):
> +     * tests/test-duplocale.c (main):
> +     * tests/test-getgroups.c (main):
> +     * tests/test-getline.c (main):
> +     * tests/test-inttostr.c (main):
> +     * tests/test-localename.c (test_locale_name)
> +     (test_locale_name_thread, test_locale_name_environ)
> +     (test_locale_name_default):
> +     * tests/test-regex.c (main):
> +     * tests/test-setlocale1.c (main):
> +     * tests/test-stat.h (test_stat_func):
> +     Free heap-allocated storage before exiting.
> +     * m4/asm-underscore.m4 (gl_ASM_SYMBOL_PREFIX):
> +     Don't match *_foo symbols inserted by AddressSanitizer.
> +     * tests/test-regex.c, tests/test-stat.c: Include stdlib.h, for 'free'.
> +
>  2016-02-02  Jim Meyering  <address@hidden>
>  
>       verify-tests: also remove stray test-verify.Tpo
> diff --git a/m4/acl.m4 b/m4/acl.m4
> index 0f96337..ce0fe6b 100644
> --- a/m4/acl.m4
> +++ b/m4/acl.m4
> @@ -1,5 +1,5 @@
>  # acl.m4 - check for access control list (ACL) primitives
> -# serial 21
> +# serial 22
>  
>  # Copyright (C) 2002, 2004-2016 Free Software Foundation, Inc.
>  # This file is free software; the Free Software Foundation
> @@ -163,9 +163,8 @@ AC_DEFUN([gl_ACL_GET_FILE],
>             #include <sys/acl.h>
>             #include <errno.h>
>            ]],
> -          [[if (!acl_get_file (".", ACL_TYPE_ACCESS) && errno == ENOENT)
> -              return 1;
> -            return 0;
> +          [[acl_t acl = acl_get_file (".", ACL_TYPE_ACCESS);
> +            return acl ? acl_free (acl) != 0 : errno == ENOENT;
>            ]])],
>         [if test $cross_compiling = yes; then
>            gl_cv_func_working_acl_get_file="guessing yes"
> diff --git a/m4/asm-underscore.m4 b/m4/asm-underscore.m4
> index ac1ae7c..547dd62 100644
> --- a/m4/asm-underscore.m4
> +++ b/m4/asm-underscore.m4
> @@ -1,4 +1,4 @@
> -# asm-underscore.m4 serial 2
> +# asm-underscore.m4 serial 3
>  dnl Copyright (C) 2010-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,
> @@ -29,7 +29,7 @@ int foo(void) { return 0; }
>  EOF
>       # Look for the assembly language name in the .s file.
>       AC_TRY_COMMAND(${CC-cc} $CFLAGS $CPPFLAGS $gl_c_asm_opt conftest.c) 
> >/dev/null 2>&1
> -     if grep _foo conftest.$gl_asmext >/dev/null ; then
> +     if LC_ALL=C grep -E '(^|[^a-zA-Z0-9_])_foo([^a-zA-Z0-9_]|$)' 
> conftest.$gl_asmext >/dev/null; then
>         gl_cv_prog_as_underscore=yes
>       else
>         gl_cv_prog_as_underscore=no
> diff --git a/m4/calloc.m4 b/m4/calloc.m4
> index 099f9af..3109fb2 100644
> --- a/m4/calloc.m4
> +++ b/m4/calloc.m4
> @@ -1,4 +1,4 @@
> -# calloc.m4 serial 15
> +# calloc.m4 serial 16
>  
>  # Copyright (C) 2004-2016 Free Software Foundation, Inc.
>  # This file is free software; the Free Software Foundation
> @@ -25,10 +25,14 @@ AC_DEFUN([_AC_FUNC_CALLOC_IF],
>         [AC_LANG_PROGRAM(
>            [AC_INCLUDES_DEFAULT],
>            [[int result = 0;
> -            if (!calloc (0, 0))
> +            char *p = calloc (0, 0);
> +            if (!p)
>                result |= 1;
> -            if (calloc ((size_t) -1 / 8 + 1, 8))
> +            free (p);
> +            p = calloc ((size_t) -1 / 8 + 1, 8);
> +            if (p)
>                result |= 2;
> +            free (p);
>              return result;
>            ]])],
>         [ac_cv_func_calloc_0_nonnull=yes],
> diff --git a/m4/canonicalize.m4 b/m4/canonicalize.m4
> index d8f5283..bfc9ebe 100644
> --- a/m4/canonicalize.m4
> +++ b/m4/canonicalize.m4
> @@ -1,4 +1,4 @@
> -# canonicalize.m4 serial 26
> +# canonicalize.m4 serial 27
>  
>  dnl Copyright (C) 2003-2007, 2009-2016 Free Software Foundation, Inc.
>  
> @@ -83,22 +83,27 @@ AC_DEFUN([gl_FUNC_REALPATH_WORKS],
>            char *name = realpath ("conftest.a", NULL);
>            if (!(name && *name == '/'))
>              result |= 1;
> +          free (name);
>          }
>          {
>            char *name = realpath ("conftest.b/../conftest.a", NULL);
>            if (name != NULL)
>              result |= 2;
> +          free (name);
>          }
>          {
>            char *name = realpath ("conftest.a/", NULL);
>            if (name != NULL)
>              result |= 4;
> +          free (name);
>          }
>          {
>            char *name1 = realpath (".", NULL);
>            char *name2 = realpath ("conftest.d//./..", NULL);
> -          if (strcmp (name1, name2) != 0)
> +          if (! (name1 && name2 && strcmp (name1, name2) != 0))
>              result |= 8;
> +          free (name1);
> +          free (name2);
>          }
>          return result;
>        ]])
> diff --git a/m4/d-ino.m4 b/m4/d-ino.m4
> index 5f491c6..f3ce54c 100644
> --- a/m4/d-ino.m4
> +++ b/m4/d-ino.m4
> @@ -1,4 +1,4 @@
> -# serial 14
> +# serial 15
>  
>  dnl From Jim Meyering.
>  dnl
> @@ -34,6 +34,7 @@ AC_DEFUN([gl_CHECK_TYPE_STRUCT_DIRENT_D_INO],
>                 return 3;
>               if (e->d_ino != st.st_ino)
>                 return 4;
> +             closedir (dp);
>               return 0;
>             ]])],
>             [gl_cv_struct_dirent_d_ino=yes],
> diff --git a/m4/duplocale.m4 b/m4/duplocale.m4
> index 549dde6..fcf9d39 100644
> --- a/m4/duplocale.m4
> +++ b/m4/duplocale.m4
> @@ -1,4 +1,4 @@
> -# duplocale.m4 serial 7
> +# duplocale.m4 serial 8
>  dnl Copyright (C) 2009-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,
> @@ -25,8 +25,10 @@ AC_DEFUN([gl_FUNC_DUPLOCALE],
>  #endif
>  int main ()
>  {
> -  if (duplocale (LC_GLOBAL_LOCALE) == (locale_t)0)
> +  locale_t loc = duplocale (LC_GLOBAL_LOCALE);
> +  if (!loc)
>      return 1;
> +  freelocale (loc);
>    return 0;
>  }]])],
>           [gl_cv_func_duplocale_works=yes],
> diff --git a/m4/getcwd.m4 b/m4/getcwd.m4
> index 2b1c416..566c3c1 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 12
> +# serial 13
>  
>  AC_DEFUN([gl_FUNC_GETCWD_NULL],
>    [
> @@ -15,6 +15,7 @@ AC_DEFUN([gl_FUNC_GETCWD_NULL],
>     AC_CACHE_CHECK([whether getcwd (NULL, 0) allocates memory for result],
>       [gl_cv_func_getcwd_null],
>       [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
> +#     include <stdlib.h>
>  #        if HAVE_UNISTD_H
>  #         include <unistd.h>
>  #        else /* on Windows with MSVC */
> @@ -39,6 +40,7 @@ AC_DEFUN([gl_FUNC_GETCWD_NULL],
>                   return 3;
>                 if (f[1] != '\0')
>                   return 4;
> +               free (f);
>                 return 0;
>               }
>  #endif
> diff --git a/m4/getdelim.m4 b/m4/getdelim.m4
> index f11195a..a1a7dcc 100644
> --- a/m4/getdelim.m4
> +++ b/m4/getdelim.m4
> @@ -1,4 +1,4 @@
> -# getdelim.m4 serial 10
> +# getdelim.m4 serial 11
>  
>  dnl Copyright (C) 2005-2007, 2009-2016 Free Software Foundation, Inc.
>  dnl
> @@ -48,7 +48,9 @@ AC_DEFUN([gl_FUNC_GETDELIM],
>          size_t siz = (size_t)(~0) / 4;
>          if (getdelim (&line, &siz, '\n', in) == -1)
>            return 3;
> +        free (line);
>        }
> +      fclose (in);
>        return 0;
>      }
>      ]])], [gl_cv_func_working_getdelim=yes] dnl The library version works.
> diff --git a/m4/getgroups.m4 b/m4/getgroups.m4
> index 4b1573d..bf01f32 100644
> --- a/m4/getgroups.m4
> +++ b/m4/getgroups.m4
> @@ -1,4 +1,4 @@
> -# serial 18
> +# serial 19
>  
>  dnl From Jim Meyering.
>  dnl A wrapper around AC_FUNC_GETGROUPS.
> @@ -87,7 +87,9 @@ AC_DEFUN([gl_FUNC_GETGROUPS],
>          [AC_RUN_IFELSE([AC_LANG_PROGRAM([AC_INCLUDES_DEFAULT],
>            [[int size = getgroups (0, 0);
>              gid_t *list = malloc (size * sizeof *list);
> -            return getgroups (-1, list) != -1;]])],
> +            int result = getgroups (-1, list) != -1;
> +            free (list);
> +            return result;]])],
>            [gl_cv_func_getgroups_works=yes],
>            [gl_cv_func_getgroups_works=no],
>            [case "$host_os" in
> diff --git a/m4/getline.m4 b/m4/getline.m4
> index 443871f..f44751c 100644
> --- a/m4/getline.m4
> +++ b/m4/getline.m4
> @@ -1,4 +1,4 @@
> -# getline.m4 serial 26
> +# getline.m4 serial 27
>  
>  dnl Copyright (C) 1998-2003, 2005-2007, 2009-2016 Free Software Foundation,
>  dnl Inc.
> @@ -47,6 +47,7 @@ AC_DEFUN([gl_FUNC_GETLINE],
>          int len = getline (&line, &siz, in);
>          if (!(len == 4 && line && strcmp (line, "foo\n") == 0))
>            return 2;
> +        free (line);
>        }
>        {
>          /* Test result for a NULL buffer and a non-zero size.
> @@ -55,7 +56,9 @@ AC_DEFUN([gl_FUNC_GETLINE],
>          size_t siz = (size_t)(~0) / 4;
>          if (getline (&line, &siz, in) == -1)
>            return 3;
> +        free (line);
>        }
> +      fclose (in);
>        return 0;
>      }
>      ]])], [am_cv_func_working_getline=yes] dnl The library version works.
> diff --git a/m4/malloc.m4 b/m4/malloc.m4
> index 717e8ad..c393690 100644
> --- a/m4/malloc.m4
> +++ b/m4/malloc.m4
> @@ -1,4 +1,4 @@
> -# malloc.m4 serial 14
> +# malloc.m4 serial 15
>  dnl Copyright (C) 2007, 2009-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,
> @@ -6,8 +6,8 @@ dnl with or without modifications, as long as this notice is 
> preserved.
>  
>  m4_version_prereq([2.70], [] ,[
>  
> -# This is taken from the following Autoconf patch:
> -# 
> http://git.savannah.gnu.org/gitweb/?p=autoconf.git;a=commitdiff;h=7fbb553727ed7e0e689a17594b58559ecf3ea6e9
> +# This is adapted with modifications from upstream Autoconf here:
> +# 
> http://git.savannah.gnu.org/cgit/autoconf.git/commit/?id=04be2b7a29d65d9a08e64e8e56e594c91749598c
>  AC_DEFUN([_AC_FUNC_MALLOC_IF],
>  [
>    AC_REQUIRE([AC_HEADER_STDC])dnl
> @@ -23,7 +23,10 @@ AC_DEFUN([_AC_FUNC_MALLOC_IF],
>              char *malloc ();
>              #endif
>            ]],
> -          [[return ! malloc (0);]])
> +          [[char *p = malloc (0);
> +            int result = !p;
> +            free (p);
> +            return result;]])
>         ],
>         [ac_cv_func_malloc_0_nonnull=yes],
>         [ac_cv_func_malloc_0_nonnull=no],
> diff --git a/m4/realloc.m4 b/m4/realloc.m4
> index 7b32ddf..fc22825 100644
> --- a/m4/realloc.m4
> +++ b/m4/realloc.m4
> @@ -1,4 +1,4 @@
> -# realloc.m4 serial 13
> +# realloc.m4 serial 14
>  dnl Copyright (C) 2007, 2009-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,
> @@ -6,8 +6,8 @@ dnl with or without modifications, as long as this notice is 
> preserved.
>  
>  m4_version_prereq([2.70], [] ,[
>  
> -# This is taken from the following Autoconf patch:
> -# 
> http://git.savannah.gnu.org/gitweb/?p=autoconf.git;a=commitdiff;h=7fbb553727ed7e0e689a17594b58559ecf3ea6e9
> +# This is adapted with modifications from upstream Autoconf here:
> +# 
> http://git.savannah.gnu.org/cgit/autoconf.git/commit/?id=04be2b7a29d65d9a08e64e8e56e594c91749598c
>  AC_DEFUN([_AC_FUNC_REALLOC_IF],
>  [
>    AC_REQUIRE([AC_HEADER_STDC])dnl
> @@ -23,7 +23,10 @@ AC_DEFUN([_AC_FUNC_REALLOC_IF],
>              char *realloc ();
>              #endif
>            ]],
> -          [[return ! realloc (0, 0);]])
> +          [[char *p = realloc (0, 0);
> +            int result = !p;
> +            free (p);
> +            return result;]])
>         ],
>         [ac_cv_func_realloc_0_nonnull=yes],
>         [ac_cv_func_realloc_0_nonnull=no],
> diff --git a/m4/regex.m4 b/m4/regex.m4
> index 1996c47..abfd262 100644
> --- a/m4/regex.m4
> +++ b/m4/regex.m4
> @@ -1,4 +1,4 @@
> -# serial 65
> +# serial 66
>  
>  # Copyright (C) 1996-2001, 2003-2016 Free Software Foundation, Inc.
>  #
> @@ -93,6 +93,7 @@ AC_DEFUN([gl_REGEX],
>                                        0, sizeof data - 1, &regs)
>                             != -1)
>                      result |= 1;
> +                  regfree (&regex);
>                  }
>  
>                  {
> @@ -124,6 +125,7 @@ AC_DEFUN([gl_REGEX],
>                        if (i != 0 && i != 21)
>                          result |= 1;
>                      }
> +                  regfree (&regex);
>                  }
>  
>                  if (! setlocale (LC_ALL, "C"))
> diff --git a/m4/strndup.m4 b/m4/strndup.m4
> index de37222..ac48a22 100644
> --- a/m4/strndup.m4
> +++ b/m4/strndup.m4
> @@ -1,4 +1,4 @@
> -# strndup.m4 serial 21
> +# strndup.m4 serial 22
>  dnl Copyright (C) 2002-2003, 2005-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,
> @@ -31,11 +31,14 @@ AC_DEFUN([gl_FUNC_STRNDUP],
>    #endif
>    char *strndup (const char *, size_t);
>  #endif
> +  int result;
>    char *s;
>    s = strndup ("some longer string", 15);
>    free (s);
>    s = strndup ("shorter string", 13);
> -  return s[13] != '\0';]])],
> +  result = s[13] != '\0';
> +  free (s);
> +  return result;]])],
>           [gl_cv_func_strndup_works=yes],
>           [gl_cv_func_strndup_works=no],
>           [
> diff --git a/tests/test-calloc-gnu.c b/tests/test-calloc-gnu.c
> index bbfdf22..a5d7bd7 100644
> --- a/tests/test-calloc-gnu.c
> +++ b/tests/test-calloc-gnu.c
> @@ -22,13 +22,19 @@ int
>  main ()
>  {
>    /* Check that calloc (0, 0) is not a NULL pointer.  */
> -  if (calloc (0, 0) == NULL)
> +  void *p = calloc (0, 0);
> +  if (p == NULL)
>      return 1;
> +  free (p);
>  
>    /* Check that calloc fails when requested to allocate a block of memory
>       larger than SIZE_MAX bytes.  */
> -  if (calloc ((size_t) -1 / 8 + 1, 8) != NULL)
> -    return 1;
> +  p = calloc ((size_t) -1 / 8 + 1, 8);
> +  if (p != NULL)
> +    {
> +      free (p);
> +      return 1;
> +    }
>  
>    return 0;
>  }
> diff --git a/tests/test-duplocale.c b/tests/test-duplocale.c
> index c87ba43..498e31d 100644
> --- a/tests/test-duplocale.c
> +++ b/tests/test-duplocale.c
> @@ -58,6 +58,7 @@ main ()
>    struct locale_dependent_values expected_results;
>    locale_t mixed1;
>    locale_t mixed2;
> +  locale_t perthread;
>  
>    /* Set up a locale which is a mix between different system locales.  */
>    setlocale (LC_ALL, "en_US.UTF-8");
> @@ -70,7 +71,8 @@ main ()
>    ASSERT (mixed1 != NULL);
>  
>    /* Use a per-thread locale.  */
> -  uselocale (newlocale (LC_ALL_MASK, "es_ES.UTF-8", NULL));
> +  perthread = newlocale (LC_ALL_MASK, "es_ES.UTF-8", NULL);
> +  uselocale (perthread);
>  
>    /* Save the locale in a locale_t object again.  */
>    mixed2 = duplocale (LC_GLOBAL_LOCALE);
> @@ -108,6 +110,10 @@ main ()
>      ASSERT (strcmp (results.time, expected_results.time) == 0);
>    }
>  
> +  setlocale (LC_ALL, "C");
> +  freelocale (mixed1);
> +  freelocale (mixed2);
> +  freelocale (perthread);
>    return 0;
>  }
>  
> diff --git a/tests/test-getgroups.c b/tests/test-getgroups.c
> index 9ccf3b8..be1758c 100644
> --- a/tests/test-getgroups.c
> +++ b/tests/test-getgroups.c
> @@ -74,5 +74,6 @@ main (int argc, char **argv _GL_UNUSED)
>        for (i = 0; i < result; i++)
>          printf ("%d\n", (int) groups[i]);
>      }
> +  free (groups);
>    return 0;
>  }
> diff --git a/tests/test-getline.c b/tests/test-getline.c
> index adf2cc4..13dcb1d 100644
> --- a/tests/test-getline.c
> +++ b/tests/test-getline.c
> @@ -71,8 +71,8 @@ main (void)
>    free (line);
>  
>    /* Test growth of buffer, must not leak.  */
> -  line = malloc (1);
> -  len = 0;
> +  len = 1;
> +  line = malloc (len);
>    result = getline (&line, &len, f);
>    ASSERT (result == 3);
>    ASSERT (strcmp (line, "bc\n") == 0);
> diff --git a/tests/test-inttostr.c b/tests/test-inttostr.c
> index 3e59af0..18ff4a2 100644
> --- a/tests/test-inttostr.c
> +++ b/tests/test-inttostr.c
> @@ -84,9 +84,11 @@ main (void)
>        CK (off_t,        offtostr);
>        CK (uintmax_t,    umaxtostr);
>        CK (intmax_t,     imaxtostr);
> +      free (b);
>        return 0;
>      }
>  
>    /* snprintf doesn't accept %ju; skip this test.  */
> +  free (b);
>    return 77;
>  }
> diff --git a/tests/test-localename.c b/tests/test-localename.c
> index 040c025..a61fa2c 100644
> --- a/tests/test-localename.c
> +++ b/tests/test-localename.c
> @@ -183,6 +183,8 @@ test_locale_name (void)
>          ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
>          name = gl_locale_name (LC_MESSAGES, "LC_MESSAGES");
>          ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
> +        uselocale (LC_GLOBAL_LOCALE);
> +        freelocale (locale);
>        }
>    }
>  
> @@ -194,10 +196,10 @@ test_locale_name (void)
>      for (i = 0; i < SIZEOF (categories); i++)
>        {
>          int category_mask = categories[i].mask;
> -        locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
> -        if (locale != NULL)
> +        locale_t loc = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
> +        if (loc != NULL)
>            {
> -            locale = newlocale (category_mask, "de_DE.UTF-8", locale);
> +            locale_t locale = newlocale (category_mask, "de_DE.UTF-8", loc);
>              if (locale != NULL)
>                {
>                  unsigned int j;
> @@ -212,7 +214,10 @@ test_locale_name (void)
>                      else
>                        ASSERT (strcmp (name_j, "fr_FR.UTF-8") == 0);
>                    }
> +                uselocale (LC_GLOBAL_LOCALE);
> +                freelocale (locale);
>                }
> +            freelocale (loc);
>            }
>        }
>    }
> @@ -245,6 +250,8 @@ test_locale_name_thread (void)
>          ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
>          name = gl_locale_name_thread (LC_MESSAGES, "LC_MESSAGES");
>          ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
> +        uselocale (LC_GLOBAL_LOCALE);
> +        freelocale (locale);
>        }
>    }
>  
> @@ -256,10 +263,10 @@ test_locale_name_thread (void)
>      for (i = 0; i < SIZEOF (categories); i++)
>        {
>          int category_mask = categories[i].mask;
> -        locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
> -        if (locale != NULL)
> +        locale_t loc = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
> +        if (loc != NULL)
>            {
> -            locale = newlocale (category_mask, "de_DE.UTF-8", locale);
> +            locale_t locale = newlocale (category_mask, "de_DE.UTF-8", loc);
>              if (locale != NULL)
>                {
>                  unsigned int j;
> @@ -275,7 +282,10 @@ test_locale_name_thread (void)
>                      else
>                        ASSERT (strcmp (name_j, "fr_FR.UTF-8") == 0);
>                    }
> +                uselocale (LC_GLOBAL_LOCALE);
> +                freelocale (locale);
>                }
> +            freelocale (loc);
>            }
>        }
>    }
> @@ -444,6 +454,7 @@ test_locale_name_thread (void)
>                  ASSERT (strcmp (unsaved_names[j][i], name) == 0);
>                }
>              uselocale (LC_GLOBAL_LOCALE);
> +            freelocale (locale);
>            }
>        }
>      /* Verify the unsaved_names are still valid.  */
> @@ -590,6 +601,8 @@ test_locale_name_posix (void)
>          uselocale (locale);
>          name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
>          ASSERT (strcmp (name, "C") == 0);
> +        uselocale (LC_GLOBAL_LOCALE);
> +        freelocale (locale);
>        }
>    }
>  #endif
> @@ -702,6 +715,8 @@ test_locale_name_environ (void)
>          uselocale (locale);
>          name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
>          ASSERT (strcmp (name, "C") == 0);
> +        uselocale (LC_GLOBAL_LOCALE);
> +        freelocale (locale);
>        }
>    }
>  #endif
> @@ -729,6 +744,8 @@ test_locale_name_default (void)
>        {
>          uselocale (locale);
>          ASSERT (strcmp (gl_locale_name_default (), name) == 0);
> +        uselocale (LC_GLOBAL_LOCALE);
> +        freelocale (locale);
>        }
>    }
>  #endif
> diff --git a/tests/test-regex.c b/tests/test-regex.c
> index 6b9d1cb..c0d9824 100644
> --- a/tests/test-regex.c
> +++ b/tests/test-regex.c
> @@ -20,6 +20,7 @@
>  
>  #include <locale.h>
>  #include <limits.h>
> +#include <stdlib.h>
>  #include <string.h>
>  #if HAVE_DECL_ALARM
>  # include <unistd.h>
> @@ -61,10 +62,17 @@ main (void)
>          s = re_compile_pattern (pat, sizeof pat - 1, &regex);
>          if (s)
>            result |= 1;
> -        else if (re_search (&regex, data, sizeof data - 1,
> -                            0, sizeof data - 1, &regs)
> -                 != -1)
> -          result |= 1;
> +        else
> +          {
> +            memset (&regs, 0, sizeof regs);
> +            if (re_search (&regex, data, sizeof data - 1,
> +                           0, sizeof data - 1, &regs)
> +                != -1)
> +              result |= 1;
> +            regfree (&regex);
> +            free (regs.start);
> +            free (regs.end);
> +          }
>        }
>  
>        /* Check whether it's really a UTF-8 locale.
> @@ -96,10 +104,14 @@ main (void)
>              result |= 1;
>            else
>              {
> +              memset (&regs, 0, sizeof regs);
>                i = re_search (&regex, data, sizeof data - 1,
>                               0, sizeof data - 1, 0);
>                if (i != 0 && i != 21)
>                  result |= 1;
> +              regfree (&regex);
> +              free (regs.start);
> +              free (regs.end);
>              }
>          }
>  
> @@ -114,8 +126,15 @@ main (void)
>    if (s)
>      result |= 2;
>    /* This should fail, but succeeds for glibc-2.5.  */
> -  else if (re_search (&regex, "a\nb", 3, 0, 3, &regs) != -1)
> -    result |= 2;
> +  else
> +    {
> +      memset (&regs, 0, sizeof regs);
> +      if (re_search (&regex, "a\nb", 3, 0, 3, &regs) != -1)
> +        result |= 2;
> +      regfree (&regex);
> +      free (regs.start);
> +      free (regs.end);
> +    }
>  
>    /* This regular expression is from Spencer ere test number 75
>       in grep-2.3.  */
> @@ -127,7 +146,10 @@ main (void)
>    s = re_compile_pattern ("a[[:@:>@:]]b\n", 11, &regex);
>    /* This should fail with _Invalid character class name_ error.  */
>    if (!s)
> -    result |= 4;
> +    {
> +      result |= 4;
> +      regfree (&regex);
> +    }
>  
>    /* Ensure that [b-a] is diagnosed as invalid, when
>       using RE_NO_EMPTY_RANGES. */
> @@ -135,13 +157,18 @@ main (void)
>    memset (&regex, 0, sizeof regex);
>    s = re_compile_pattern ("a[b-a]", 6, &regex);
>    if (s == 0)
> -    result |= 8;
> +    {
> +      result |= 8;
> +      regfree (&regex);
> +    }
>  
>    /* This should succeed, but does not for glibc-2.1.3.  */
>    memset (&regex, 0, sizeof regex);
>    s = re_compile_pattern ("{1", 2, &regex);
>    if (s)
>      result |= 8;
> +  else
> +    regfree (&regex);
>  
>    /* The following example is derived from a problem report
>       against gawk from Jorge Stolfi <address@hidden>.  */
> @@ -150,16 +177,30 @@ main (void)
>    if (s)
>      result |= 8;
>    /* This should match, but does not for glibc-2.2.1.  */
> -  else if (re_match (&regex, "an", 2, 0, &regs) != 2)
> -    result |= 8;
> +  else
> +    {
> +      memset (&regs, 0, sizeof regs);
> +      if (re_match (&regex, "an", 2, 0, &regs) != 2)
> +        result |= 8;
> +      regfree (&regex);
> +      free (regs.start);
> +      free (regs.end);
> +    }
>  
>    memset (&regex, 0, sizeof regex);
>    s = re_compile_pattern ("x", 1, &regex);
>    if (s)
>      result |= 8;
>    /* glibc-2.2.93 does not work with a negative RANGE argument.  */
> -  else if (re_search (&regex, "wxy", 3, 2, -2, &regs) != 1)
> -    result |= 8;
> +  else
> +    {
> +      memset (&regs, 0, sizeof regs);
> +      if (re_search (&regex, "wxy", 3, 2, -2, &regs) != 1)
> +        result |= 8;
> +      regfree (&regex);
> +      free (regs.start);
> +      free (regs.end);
> +    }
>  
>    /* The version of regex.c in older versions of gnulib
>       ignored RE_ICASE.  Detect that problem too.  */
> @@ -168,8 +209,15 @@ main (void)
>    s = re_compile_pattern ("x", 1, &regex);
>    if (s)
>      result |= 16;
> -  else if (re_search (&regex, "WXY", 3, 0, 3, &regs) < 0)
> -    result |= 16;
> +  else
> +    {
> +      memset (&regs, 0, sizeof regs);
> +      if (re_search (&regex, "WXY", 3, 0, 3, &regs) < 0)
> +        result |= 16;
> +      regfree (&regex);
> +      free (regs.start);
> +      free (regs.end);
> +    }
>  
>    /* Catch a bug reported by Vin Shelton in
>       http://lists.gnu.org/archive/html/bug-coreutils/2007-06/msg00089.html
> @@ -181,6 +229,8 @@ main (void)
>    s = re_compile_pattern ("[[:alnum:]_-]\\\\+$", 16, &regex);
>    if (s)
>      result |= 32;
> +  else
> +    regfree (&regex);
>  
>    /* REG_STARTEND was added to glibc on 2004-01-15.
>       Reject older versions.  */
> diff --git a/tests/test-setlocale1.c b/tests/test-setlocale1.c
> index 3bb9c3e..143c0d8 100644
> --- a/tests/test-setlocale1.c
> +++ b/tests/test-setlocale1.c
> @@ -54,6 +54,8 @@ main (int argc, char *argv[])
>  
>    /* Test that the two results are the same.  */
>    ASSERT (strcmp (name1, name2) == 0);
> +  free (name1);
> +  free (name2);
>  
>    return 0;
>  }
> diff --git a/tests/test-stat.c b/tests/test-stat.c
> index b1f2d12..4510e59 100644
> --- a/tests/test-stat.c
> +++ b/tests/test-stat.c
> @@ -30,6 +30,7 @@ SIGNATURE_CHECK (stat, int, (char const *, struct stat *));
>  #include <fcntl.h>
>  #include <errno.h>
>  #include <stdbool.h>
> +#include <stdlib.h>
>  #include <stdio.h>
>  #include <unistd.h>
>  
> diff --git a/tests/test-stat.h b/tests/test-stat.h
> index 28e1e24..4cf6c6d 100644
> --- a/tests/test-stat.h
> +++ b/tests/test-stat.h
> @@ -95,6 +95,7 @@ test_stat_func (int (*func) (char const *, struct stat *), 
> bool print)
>    ASSERT (unlink (BASE "link2") == 0);
>    ASSERT (unlink (BASE "link3") == 0);
>    ASSERT (unlink (BASE "link4") == 0);
> +  free (cwd);
>  
>    return 0;
>  }
> 

Thanks for doing all that.

This does add overhead though it's less of an issue at configure time and tests,
and so probably not worth complicating with #ifdef lint as is done in coreutils
and a few places in gnulib.

For reference, another option to avoid leak warnings is:
export ASAN_OPTIONS=detect_leaks=0

thanks,
Pádraig



reply via email to

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