On 3/31/22 02:31, Raphael Isemann wrote:
the obvious memory leak here is causing problems
with a downstream compiler (the compiler is more strict about memory
leaks and keeps generating warnings when used in autoconf projects
that compile this file during configure)

Usually there's a way to suppress these false alarms, e.g., by configuring with the appropriate CFLAGS. It's not a bad idea to do that with mature code, as the leak diagnostics are typically more trouble than they're worth. The GNU Coding standards say,

"Memory analysis tools such as valgrind can be useful, but don’t complicate a program merely to avoid their false alarms. For example, if memory is used until just before a process exits, don’t free it simply to silence such a tool." <https://www.gnu.org/prep/standards/html_node/Memory-Usage.html>


All that being said, I installed the attached to try to help out, sometime in the distant future when everyone has upgraded to some future version of Autoconf.
From 374b0e9942bb87dd8d0c956d7c03c85dcc489bf7 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Thu, 31 Mar 2022 23:25:35 -0700
Subject: [PATCH] Work around memory-leak false alarms

Problem reported by Raphael Isemann in:
https://lists.gnu.org/r/autoconf-patches/2022-03/msg00007.html
* lib/autoconf/c.m4 (_AC_C_C99_TEST_MAIN, AC_C_FLEXIBLE_ARRAY_MEMBER):
* tests/data/gnulib_std_gnu11_2020_08_17.m4 (_AC_C_C99_TEST_HEADER):
Free storage allocated via malloc, to pacify overly-picky
implementations that issue false alarms about memory leaks.
---
 lib/autoconf/c.m4                         | 6 +++++-
 tests/data/gnulib_std_gnu11_2020_08_17.m4 | 2 ++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/lib/autoconf/c.m4 b/lib/autoconf/c.m4
index 48bd49a3..42fbcbd0 100644
--- a/lib/autoconf/c.m4
+++ b/lib/autoconf/c.m4
@@ -1330,6 +1330,8 @@ ac_c_conftest_c99_main='
   ia->datasize = 10;
   for (int i = 0; i < ia->datasize; ++i)
     ia->data[i] = i * 1.234;
+  // Work around memory leak warnings.
+  free (ia);
 
   // Check named initializers.
   struct named_init ni = {
@@ -2170,7 +2172,9 @@ AC_DEFUN([AC_C_FLEXIBLE_ARRAY_MEMBER],
 	    struct s *p = (struct s *) malloc (offsetof (struct s, d)
 					       + m * sizeof (double));
 	    p->d[0] = 0.0;
-	    return p->d != (double *) NULL;]])],
+	    m = p->d != (double *) NULL;
+	    free (p);
+	    return m;]])],
        [ac_cv_c_flexmember=yes],
        [ac_cv_c_flexmember=no])])
   if test $ac_cv_c_flexmember = yes; then
diff --git a/tests/data/gnulib_std_gnu11_2020_08_17.m4 b/tests/data/gnulib_std_gnu11_2020_08_17.m4
index da52cd6f..21f7cc7a 100644
--- a/tests/data/gnulib_std_gnu11_2020_08_17.m4
+++ b/tests/data/gnulib_std_gnu11_2020_08_17.m4
@@ -319,6 +319,8 @@ AC_DEFUN([_AC_C_C99_TEST_BODY],
   ia->datasize = 10;
   for (int i = 0; i < ia->datasize; ++i)
     ia->data[i] = i * 1.234;
+  // Work around memory leak warnings.
+  free (ia);
 
   // Check named initializers.
   struct named_init ni = {
-- 
2.32.0

Reply via email to