summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2023-04-19 17:59:03 +0200
committerBruno Haible <bruno@clisp.org>2023-04-19 17:59:03 +0200
commitccb59e4c36bea4915303a24ea1c7dec109db97eb (patch)
tree30eb5e84cca4b21d1a18982cd5676fe80aaed08e /lib
parent9bf6bcc74b4caf4d74bab8d98a4e00f761f2e5ca (diff)
downloadgnulib-ccb59e4c36bea4915303a24ea1c7dec109db97eb.tar.gz
wcsncmp: Work around two ISO C compliance bugs on several platforms.
* lib/wchar.in.h (wcsncmp): Consider REPLACE_WCSNCMP. * lib/wcsncmp-impl.h (wcsncmp): Don't assume that the two wide characters are in the range 0..INT_MAX. * m4/wcsncmp.m4 (gl_FUNC_WCSNCMP): Test whether wcsncmp works for all wide characters. Set REPLACE_WCSNCMP. * m4/wchar_h.m4 (gl_WCHAR_H_DEFAULTS): Initialize REPLACE_WCSNCMP. * modules/wchar (Makefile.am): Substitute REPLACE_WCSNCMP. * modules/wcsncmp (Status, Notice): Un-obsolete this module. (configure.ac): Consider REPLACE_WCSNCMP. * doc/posix-functions/wcsncmp.texi: Mention the two bugs.
Diffstat (limited to 'lib')
-rw-r--r--lib/wchar.in.h16
-rw-r--r--lib/wcsncmp-impl.h5
2 files changed, 17 insertions, 4 deletions
diff --git a/lib/wchar.in.h b/lib/wchar.in.h
index c347256368..69fa2f8f59 100644
--- a/lib/wchar.in.h
+++ b/lib/wchar.in.h
@@ -967,13 +967,25 @@ _GL_WARN_ON_USE (wcscmp, "wcscmp is unportable - "
/* Compare no more than N wide characters of S1 and S2. */
#if @GNULIB_WCSNCMP@
-# if !@HAVE_WCSNCMP@
+# if @REPLACE_WCSNCMP@
+# if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+# undef wcsncmp
+# define wcsncmp rpl_wcsncmp
+# endif
+_GL_FUNCDECL_RPL (wcsncmp, int,
+ (const wchar_t *s1, const wchar_t *s2, size_t n)
+ _GL_ATTRIBUTE_PURE);
+_GL_CXXALIAS_RPL (wcsncmp, int,
+ (const wchar_t *s1, const wchar_t *s2, size_t n));
+# else
+# if !@HAVE_WCSNCMP@
_GL_FUNCDECL_SYS (wcsncmp, int,
(const wchar_t *s1, const wchar_t *s2, size_t n)
_GL_ATTRIBUTE_PURE);
-# endif
+# endif
_GL_CXXALIAS_SYS (wcsncmp, int,
(const wchar_t *s1, const wchar_t *s2, size_t n));
+# endif
# if __GLIBC__ >= 2
_GL_CXXALIASWARN (wcsncmp);
# endif
diff --git a/lib/wcsncmp-impl.h b/lib/wcsncmp-impl.h
index 4ab49ebb3a..72cfce7ac4 100644
--- a/lib/wcsncmp-impl.h
+++ b/lib/wcsncmp-impl.h
@@ -27,8 +27,9 @@ wcsncmp (const wchar_t *s1, const wchar_t *s2, size_t n)
n--;
continue;
}
- /* Note that wc1 and wc2 each have at most 31 bits. */
- return (int)wc1 - (int)wc2;
+ /* ISO C requires wcsncmp to work with all wchar_t values.
+ We cannot assume that wc1 and wc2 are in the range 0..INT_MAX. */
+ return _GL_CMP (wc1, wc2);
/* > 0 if wc1 > wc2, < 0 if wc1 < wc2,
= 0 if wc1 and wc2 are both '\0'. */
}