bug-gnulib
[Top][All Lists]
Advanced

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

bitset: Don't access errno when it's not set


From: Bruno Haible
Subject: bitset: Don't access errno when it's not set
Date: Sun, 25 Feb 2024 16:19:48 +0100

The 'bitset' module accesses an undefined errno in two situations:
1) After calling fread() that reads fewer elements than requested,
   and after calling ferror(), the value of errno is undefined.[1][2]
2) After calling fclose() on native Windows, the value of errno is undefined.
   [3][4][5]

[1] https://pubs.opengroup.org/onlinepubs/9699919799/functions/fread.html
[2] https://pubs.opengroup.org/onlinepubs/9699919799/functions/ferror.html
[3] https://pubs.opengroup.org/onlinepubs/9699919799/functions/fclose.html
[4] https://www.gnu.org/software/gnulib/manual/html_node/fclose.html
[5] 
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fclose-fcloseall

This patch fixes it.


2024-02-25  Bruno Haible  <bruno@clisp.org>

        bitset: Don't access errno when it's not set.
        * lib/bitset/stats.c (bitset_stats_read): Don't use errno after neither
        fread() nor ferror() has set it. On native Windows, don't use errno
        after fclose().

diff --git a/lib/bitset/stats.c b/lib/bitset/stats.c
index 5c40fc9466..c06a450f7c 100644
--- a/lib/bitset/stats.c
+++ b/lib/bitset/stats.c
@@ -261,12 +261,19 @@ bitset_stats_read (const char *file_name)
                  1, file) != 1)
         {
           if (ferror (file))
-            perror (_("cannot read stats file"));
+            fprintf (stderr, "%s\n", _("cannot read stats file"));
           else
             fprintf (stderr, "%s\n", _("bad stats file size"));
         }
       if (fclose (file) != 0)
-        perror (_("cannot read stats file"));
+        {
+#if defined _WIN32 && !defined __CYGWIN__
+          fprintf (stderr, "%s\n", _("cannot read stats file"));
+#else
+          /* fclose() sets errno.  */
+          perror (_("cannot read stats file"));
+#endif
+        }
     }
   bitset_stats_info_data.runs++;
 }






reply via email to

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