From a235aed6a25e52eb99e1ef71c070a258dd88a3e9 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sun, 25 Feb 2024 16:12:57 +0100
Subject: [PATCH] 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().
---
 ChangeLog          |  7 +++++++
 lib/bitset/stats.c | 11 +++++++++--
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 511a9f247e..270b8cb0aa 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+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().
+
 2024-02-25  Bruno Haible  <bruno@clisp.org>
 
 	bitset: Avoid newlines at the end of translatable strings.
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++;
 }
-- 
2.39.5