On FreeBSD 11, I see this warning:

../src/fmt.c:501:50: warning: data argument not used by format string 
[-Wformat-extra-args]

The code there implements this logic:
  - If an error number is known, present an error message with a file name,
    but without telling the user in which context the error occurred.
  - If an error number is not known, tell the user about the context,
    but not about the file name.

This logic makes no sense.

Here's a patch to change that. Its effects are:

When a file name is known, e.g. "foobar.txt":

  If there was an error before fclose():
    Before:  read error
    After:   error reading 'foobar.txt'

  If there was no error before fclose():
    Before:  'foobar.txt': I/O error
    After:   error reading 'foobar.txt': I/O error

When reading from standard input (in this case no error number is known):

  Before:  read error
  After:   read error

>From e41b9e372f1f17378091654fc78ba76913ba61c6 Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Thu, 18 Sep 2025 23:05:06 +0200
Subject: [PATCH] fmt: Eliminate a clang -Wformat-extra-args warning

* src/fmt.c (fmt): When not reading from stdin, always mention the
file name in the error message.
---
 src/fmt.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/fmt.c b/src/fmt.c
index b44bbde5c..4591e266d 100644
--- a/src/fmt.c
+++ b/src/fmt.c
@@ -493,12 +493,18 @@ fmt (FILE *f, char const *file)
     }
 
   int err = ferror (f) ? 0 : -1;
+  /* err < 0 means success.  */
   if (f == stdin)
     clearerr (f);
   else if (fclose (f) != 0 && err < 0)
     err = errno;
   if (0 <= err)
-    error (0, err, err ? "%s" : _("read error"), quotef (file));
+    {
+      if (f == stdin)
+        error (0, err, _("read error"));
+      else
+        error (0, err, _("error reading %s"), quotef (file));
+    }
   return err < 0;
 }
 
-- 
2.51.0

Reply via email to