Subject: Re: [bug-diffutils] bug#47362: important potential issues found by covscan in diffutils-3.7 on fedora

From: Paul Eggert <[email-masked]>
Date: Wed, 24 Mar 2021 14:16:26 -0700

Thanks for the bug report. Those are all false alarms or are already 
fixed in Gnulib, except for a memory leak in ifdef.c for which I 
installed the attached patch. Thanks for reporting the problem.
From 1399b225ebecdbac80cc14be87c2454311f3b40f Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Wed, 24 Mar 2021 14:12:22 -0700
Subject: [PATCH] diff: plug memory leak in ifdef.c
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Problem reported by Than (Bug#47362).
Fix the bug by using xmalloca instead of vararrays.
* bootstrap.conf (gnulib_modules): Add xmalloca; remove vararrays.
* configure.ac: Remove AC_C_VARARRAYS.
* src/ifdef.c: Include xmalloca.h instead of xalloc.h.
(do_printf_spec): Use xmalloca instead of an xmalloc
that lacks a corresponding ‘free’ if HAVE_C_VARARRAYS
due to a typo in 2017-05-18T05:51:[email-masked].
---
 bootstrap.conf |  2 +-
 configure.ac   |  1 -
 src/ifdef.c    | 12 +++---------
 3 files changed, 4 insertions(+), 11 deletions(-)

diff --git a/bootstrap.conf b/bootstrap.conf
index d45abdb..f2359f6 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -81,13 +81,13 @@ system-quote
 unistd
 unlocked-io
 update-copyright
-vararrays
 verify
 version-etc
 version-etc-fsf
 wcwidth
 xalloc
 xfreopen
+xmalloca
 xreadlink
 xstdopen
 xstrtoimax
diff --git a/configure.ac b/configure.ac
index 3b2195e..6a76b12 100644
--- a/configure.ac
+++ b/configure.ac
@@ -111,7 +111,6 @@ if test "$gl_gcc_warnings" = yes; then
 fi
 
 AC_C_INLINE
-AC_C_VARARRAYS
 
 AC_DEFINE([DEFAULT_EDITOR_PROGRAM], ["ed"],
   [Name of editor program, unless overridden.])
diff --git a/src/ifdef.c b/src/ifdef.c
index 8e64b4c..d40a88e 100644
--- a/src/ifdef.c
+++ b/src/ifdef.c
@@ -22,7 +22,7 @@
 
 #include "diff.h"
 
-#include <xalloc.h>
+#include <xmalloca.h>
 
 struct group
 {
@@ -362,20 +362,14 @@ do_printf_spec (FILE *out, char const *spec,
             printint print_value = value;
             size_t spec_prefix_len = f - spec - 2;
             size_t pI_len = sizeof pI - 1;
-#if 0
-            char format[spec_prefix_len + pI_len + 2];
-#else
-            char *format = xmalloc (spec_prefix_len + pI_len + 2);
-#endif
+            char *format = xmalloca (spec_prefix_len + pI_len + 2);
             char *p = format + spec_prefix_len + pI_len;
             memcpy (format, spec, spec_prefix_len);
             memcpy (format + spec_prefix_len, pI, pI_len);
             *p++ = c;
             *p = '\0';
             fprintf (out, format, print_value);
-#if ! HAVE_C_VARARRAYS
-            free (format);
-#endif
+            freea (format);
           }
       }
       break;
-- 
2.27.0