On Tue, Oct 15, 2024 at 12:36:59PM +0200, Vitezslav Crhonek wrote:
> Hi,
> 
> Please review proposed fixes of issues found by static analysis
> of texinfo-7.1 in the attached patch and consider applying them.
> 
> During the investigation I stumbled on this chunk of code
> in install-info.c (starting by line 831):
> 
>       if (fclose (f) < 0)
>         return 0;
>       f2 = freopen (*opened_filename, FOPEN_RBIN, stdin);
> --->  if (!f)
>         return 0;
>       f = popen (command, "r");
>       fclose (f2);
>       if (!f)
>         {
>           /* Used for error message in calling code. */
>           *opened_filename = command;
>           return 0;
>         }
> 
> The marked line seems suspicious - I guess there's a typo and 'f2'
> should be tested instead of 'f' in the condition.
> But this is _not_ changed in proposed patch, just a heads-up here.

I found the code was changed after commit 8f29de69092a.
(2023-10-08 16:54:39 +0100).

It was the change sent in this email:

https://lists.gnu.org/archive/html/bug-texinfo/2023-10/msg00011.html

2023-10-08  Eli Zaretskii <[email protected]>

       * install-info/install-info.c (open_possibly_compressed_file):
       Close stdin file description in main process after calling popen
       on compression program.  This allows the file to be removed or
       renamed on Windows.

--- a/install-info/install-info.c
+++ b/install-info/install-info.c
@@ -826,13 +826,15 @@ determine_file_type:
       /* Redirect stdin to the file and fork the decompression process
          reading from stdin.  This allows shell metacharacters in filenames. */
       char *command = concat (*compression_program, " -d", "");
+      FILE *f2;
 
       if (fclose (f) < 0)
         return 0;
-      f = freopen (*opened_filename, FOPEN_RBIN, stdin);
+      f2 = freopen (*opened_filename, FOPEN_RBIN, stdin);
       if (!f)
         return 0;
       f = popen (command, "r");
+      fclose (f2);
       if (!f)
         {
           /* Used for error message in calling code. */


It looks like an oversight.  f2 is in fact the same as stdin if freopen
is successful.  Hence we can eliminate the f2 variable and simplify the
code slightly:

diff --git a/install-info/install-info.c b/install-info/install-info.c
index 9cd909434f..adbe461442 100644
--- a/install-info/install-info.c
+++ b/install-info/install-info.c
@@ -826,15 +826,13 @@ determine_file_type:
       /* Redirect stdin to the file and fork the decompression process
          reading from stdin.  This allows shell metacharacters in filenames. */
       char *command = concat (*compression_program, " -d", "");
-      FILE *f2;
 
       if (fclose (f) < 0)
         return 0;
-      f2 = freopen (*opened_filename, FOPEN_RBIN, stdin);
-      if (!f)
+      if (!freopen (*opened_filename, FOPEN_RBIN, stdin))
         return 0;
       f = popen (command, "r");
-      fclose (f2);
+      fclose (stdin);
       if (!f)
         {
           /* Used for error message in calling code. */


Reply via email to