bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* bitset: Don't access errno when it's not set
@ 2024-02-25 15:19 Bruno Haible
  0 siblings, 0 replies; only message in thread
From: Bruno Haible @ 2024-02-25 15:19 UTC (permalink / raw)
  To: bug-gnulib

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++;
 }





^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2024-02-25 15:20 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-25 15:19 bitset: Don't access errno when it's not set Bruno Haible

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).