]> git.ipfire.org Git - thirdparty/gnulib.git/commitdiff
gettext-h: Avoid gcc -Wformat-security warnings with --disable-nls.
authorBruno Haible <bruno@clisp.org>
Wed, 28 May 2025 13:14:07 +0000 (15:14 +0200)
committerBruno Haible <bruno@clisp.org>
Wed, 28 May 2025 13:14:07 +0000 (15:14 +0200)
Reported by Holger Hoffstätte <holger@applied-asynchrony.com> in
<https://lists.gnu.org/archive/html/bug-gnulib/2025-05/msg00225.html>.

* lib/gettext.h (gettext, dgettext, dcgettext): With gcc in C mode,
define these as inline functions.
* lib/sigpipe-die.c (sigpipe_die): Use translated string as a format
string, relying on the format string checking done by 'msgfmt -c'.
* lib/xmemcoll.c (collate_error): Revert commit from 2025-01-17.
* lib/xprintf.c (xvprintf, xvfprintf): Likewise.
* lib/openat-die.c (openat_save_fail, openat_restore_fail): Revert
commit from 2024-12-10.

ChangeLog
lib/gettext.h
lib/openat-die.c
lib/sigpipe-die.c
lib/xmemcoll.c
lib/xprintf.c

index d14ec6f7179d2c9f457f48831416e10fde0bf869..fb136339367c97a4e8291ef583ce62f0f4c334a1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2025-05-28  Bruno Haible  <bruno@clisp.org>
+
+       gettext-h: Avoid gcc -Wformat-security warnings with --disable-nls.
+       Reported by Holger Hoffstätte <holger@applied-asynchrony.com> in
+       <https://lists.gnu.org/archive/html/bug-gnulib/2025-05/msg00225.html>.
+       * lib/gettext.h (gettext, dgettext, dcgettext): With gcc in C mode,
+       define these as inline functions.
+       * lib/sigpipe-die.c (sigpipe_die): Use translated string as a format
+       string, relying on the format string checking done by 'msgfmt -c'.
+       * lib/xmemcoll.c (collate_error): Revert commit from 2025-01-17.
+       * lib/xprintf.c (xvprintf, xvfprintf): Likewise.
+       * lib/openat-die.c (openat_save_fail, openat_restore_fail): Revert
+       commit from 2024-12-10.
+
 2025-05-27  Bruno Haible  <bruno@clisp.org>
 
        stddef-h: Make 'unreachable' usable in C++ mode.
index ea0c27e00029d029db857ab291535fa7ab22244f..bb3d9755decdf3681cbdae8548cffe4ac33a14d7 100644 (file)
 #  endif
 # endif
 
-/* Disabled NLS.
-   The casts to 'const char *' serve the purpose of producing warnings
-   for invalid uses of the value returned from these functions.
-   On pre-ANSI systems without 'const', the config.h file is supposed to
-   contain "#define const".  */
-# undef gettext
-# define gettext(Msgid) ((const char *) (Msgid))
-# undef dgettext
-# define dgettext(Domainname, Msgid) ((void) (Domainname), gettext (Msgid))
-# undef dcgettext
-# define dcgettext(Domainname, Msgid, Category) \
-    ((void) (Category), dgettext (Domainname, Msgid))
+/* Disabled NLS.  */
+# if defined __GNUC__ && !defined __clang__ && !defined __cplusplus
+/* Use inline functions, to avoid warnings
+     warning: format not a string literal and no format arguments
+   that don't occur with enabled NLS.  */
+__attribute__ ((__always_inline__, __gnu_inline__)) extern inline
+const char *
+gettext (const char *msgid)
+{
+  return msgid;
+}
+__attribute__ ((__always_inline__, __gnu_inline__)) extern inline
+const char *
+dgettext (const char *domain, const char *msgid)
+{
+  (void) domain;
+  return msgid;
+}
+__attribute__ ((__always_inline__, __gnu_inline__)) extern inline
+const char *
+dcgettext (const char *domain, const char *msgid, int category)
+{
+  (void) domain;
+  (void) category;
+  return msgid;
+}
+# else
+/* The casts to 'const char *' serve the purpose of producing warnings
+   for invalid uses of the value returned from these functions.  */
+#  undef gettext
+#  define gettext(Msgid) ((const char *) (Msgid))
+#  undef dgettext
+#  define dgettext(Domainname, Msgid) ((void) (Domainname), gettext (Msgid))
+#  undef dcgettext
+#  define dcgettext(Domainname, Msgid, Category) \
+     ((void) (Category), dgettext (Domainname, Msgid))
+# endif
 # undef ngettext
 # define ngettext(Msgid1, Msgid2, N) \
     ((N) == 1 \
index 3fbb5d86e47d3a208bbfa38030bcd8f81b252740..79a5b23bc8397ef56876959aa5a7be6396198133 100644 (file)
@@ -34,7 +34,7 @@ _Noreturn void
 openat_save_fail (int errnum)
 {
 #ifndef GNULIB_LIBPOSIX
-  error (exit_failure, errnum, "%s",
+  error (exit_failure, errnum,
          _("unable to record current working directory"));
 #endif
   /* _Noreturn cannot be applied to error, since it returns
@@ -53,7 +53,7 @@ _Noreturn void
 openat_restore_fail (int errnum)
 {
 #ifndef GNULIB_LIBPOSIX
-  error (exit_failure, errnum, "%s",
+  error (exit_failure, errnum,
          _("failed to return to initial working directory"));
 #endif
 
index 006f3b1636d1578b5d339f59a47e258ea718201b..ddd05cd49c5f4b771cf6ac47cd9a5fd633dcbb7f 100644 (file)
@@ -33,7 +33,7 @@
 void
 sigpipe_die (void)
 {
-  error (exit_failure, 0, "%s",
+  error (exit_failure, 0,
          _("error writing to a closed pipe or socket"));
 
   /* Ensure that this function really does not return.  */
index c0ac7c486366f509c1918a34fe3c4b8db2497f30..d1b141c9e057d5b2371d05804ebe2f61465d1a41 100644 (file)
@@ -36,8 +36,8 @@ collate_error (int collation_errno,
                char const *s1, size_t s1len,
                char const *s2, size_t s2len)
 {
-  error (0, collation_errno, "%s", _("string comparison failed"));
-  error (0, 0, "%s", _("Set LC_ALL='C' to work around the problem."));
+  error (0, collation_errno, _("string comparison failed"));
+  error (0, 0, _("Set LC_ALL='C' to work around the problem."));
   error (exit_failure, 0,
          _("The strings compared were %s and %s."),
          quotearg_n_style_mem (0, locale_quoting_style, s1, s1len),
index 4d9a3e5e4def54b32c07ba542df5d9e4340e3253..790af5320b9b856ffa31b0e58c7af0e2eae7b26d 100644 (file)
@@ -45,7 +45,7 @@ xvprintf (char const *restrict format, va_list args)
 {
   off64_t retval = vzprintf (format, args);
   if (retval < 0 && ! ferror (stdout))
-    error (exit_failure, errno, "%s", _("cannot perform formatted output"));
+    error (exit_failure, errno, _("cannot perform formatted output"));
 
   return retval;
 }
@@ -67,7 +67,7 @@ xvfprintf (FILE *restrict stream, char const *restrict format, va_list args)
 {
   off64_t retval = vfzprintf (stream, format, args);
   if (retval < 0 && ! ferror (stream))
-    error (exit_failure, errno, "%s", _("cannot perform formatted output"));
+    error (exit_failure, errno, _("cannot perform formatted output"));
 
   return retval;
 }