Re: setlocale: Silence a -Wdiscarded-qualifiers warning on Windows.

Bruno Haible via Gnulib discussion list <[email protected]>
Newsgroups gmane.comp.lib.gnulib.bugs
Organization GNU
Message-ID <2440886.ElGaqSPkdT@nimes>
I wrote:
> 2026-06-07  Bruno Haible  <[email protected]>
> 
> 	setlocale: Detect invalid writes to the returned string in some cases.
> 	* lib/locale.in.h (setlocale): Change the return type to 'const char *'.

It's a portability pitfall if the developer gets a warning on some non-GNU
systems but no warning on GNU systems. So, better enforce something like
  warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
on all platforms.


2026-06-08  Bruno Haible  <[email protected]>

	setlocale: Avoid a portability pitfall through the last patch.
	* lib/locale.in.h (setlocale): Cast the return value to 'const char *'
	on the other platforms.
	* NEWS: Update.

diff --git a/NEWS b/NEWS
index 9369ba9acc..260f2736c4 100644
--- a/NEWS
+++ b/NEWS
@@ -79,7 +79,7 @@ User visible incompatible changes
 Date        Modules         Changes
 
 2026-06-07  setlocale       The return type of the setlocale function is now
-                            'const char *' on some platforms.  You may need to
+                            'const char *' on most platforms.  You may need to
                             adjust assignments of the form
                               char *l = setlocale (...);
                             to
diff --git a/lib/locale.in.h b/lib/locale.in.h
index 7db5621452..fdf8c065d4 100644
--- a/lib/locale.in.h
+++ b/lib/locale.in.h
@@ -287,6 +287,9 @@ _GL_CXXALIAS_RPL (setlocale, const char *,
 # else
 _GL_CXXALIAS_SYS_CAST (setlocale, const char *,
                        (int category, const char *locale));
+#  if !defined setlocale && !defined __cplusplus
+#   define setlocale(...) ((const char *) setlocale (__VA_ARGS__))
+#  endif
 # endif
 # if __GLIBC__ >= 2
 _GL_CXXALIASWARN (setlocale);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.