]> Savannah Git Hosting - gnulib.git/commitdiff
fclose: pacify gcc -Wanalyzer-file-leak
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 24 Apr 2023 19:06:55 +0000 (12:06 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 24 Apr 2023 19:07:40 +0000 (12:07 -0700)
Without this patch, building coreutils with
--enable-gcc-checking=expensive would fail with a message like
“lib/exclude.c:682:6: error: leak of FILE 'in' [CWE-775]
[-Werror=analyzer-file-leak]”, because Gnulib replaced fclose but
not fopen, and GCC saw a call to fopen followed by a call to
rpl_fclose.  The patch causes GCC to instead see a call to
rpl_fopen followed by rpl_fclose.
* m4/fclose.m4 (gl_FUNC_FCLOSE): Replace fopen when replacing
fclose.
* modules/fclose (Depends-on): Add fopen.
(configure.ac): Replace fopen when replacing fclose.

ChangeLog
m4/fclose.m4
modules/fclose

index 972aec03db05ba25ee434ea8eb9b589a35cf0dcd..8d39a640237c610ac5e355cf84216e03fbe7d4d9 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,18 @@
 2023-04-24  Paul Eggert  <eggert@cs.ucla.edu>
 
+       fclose: pacify gcc -Wanalyzer-file-leak
+       Without this patch, building coreutils with
+       --enable-gcc-checking=expensive would fail with a message like
+       “lib/exclude.c:682:6: error: leak of FILE 'in' [CWE-775]
+       [-Werror=analyzer-file-leak]”, because Gnulib replaced fclose but
+       not fopen, and GCC saw a call to fopen followed by a call to
+       rpl_fclose.  The patch causes GCC to instead see a call to
+       rpl_fopen followed by rpl_fclose.
+       * m4/fclose.m4 (gl_FUNC_FCLOSE): Replace fopen when replacing
+       fclose.
+       * modules/fclose (Depends-on): Add fopen.
+       (configure.ac): Replace fopen when replacing fclose.
+
        unsetenv-tests: pacify -Wanalyzer-putenv-of-auto-var
        * tests/test-unsetenv.c (main): Make entry static.  Even with this
        change, it’s unclear whether this test is portable POSIX code, but
index dc021dd9f344b87d47b2c15dcdf913be6ae1c9bb..74b7df0a77a783e024adfa0ec367e7a819bad148 100644 (file)
@@ -31,6 +31,10 @@ AC_DEFUN([gl_FUNC_FCLOSE],
       *) REPLACE_FCLOSE=1 ;;
     esac
   fi
+
+  if test $REPLACE_FCLOSE = 1; then
+    REPLACE_FOPEN=1
+  fi
 ])
 
 dnl Determine whether fclose works on input streams.
index 703de9e686c8d3df0dfdce323ba149e62a58469c..9a949e48b8087a0ae4af46c733fc9eeeacafdc6d 100644 (file)
@@ -9,12 +9,18 @@ Depends-on:
 stdio
 close           [test $REPLACE_FCLOSE = 1]
 fflush          [test $REPLACE_FCLOSE = 1]
+fopen           [test $REPLACE_FCLOSE = 1]
 freading        [test $REPLACE_FCLOSE = 1]
 lseek           [test $REPLACE_FCLOSE = 1]
 msvc-inval      [test $REPLACE_FCLOSE = 1]
 
 configure.ac:
 gl_FUNC_FCLOSE
+if test $REPLACE_FCLOSE = 1; then
+  REPLACE_FOPEN=1
+  AC_LIBOBJ([fopen])
+  gl_PREREQ_FOPEN
+fi
 gl_CONDITIONAL([GL_COND_OBJ_FCLOSE], [test $REPLACE_FCLOSE = 1])
 gl_STDIO_MODULE_INDICATOR([fclose])