diff options
author | Bruno Haible <bruno@clisp.org> | 2023-04-19 01:01:56 +0200 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2023-04-19 01:07:24 +0200 |
commit | 6c28538c9d6bbf692ab12972de6cc035e54b0c67 (patch) | |
tree | 192b7cf854082a8ebc66f8bb81d88bc6acc1fb5f /lib | |
parent | 5c9f32c99a0ad5bb6977c4d416fd6835eeb4fad5 (diff) | |
download | gnulib-6c28538c9d6bbf692ab12972de6cc035e54b0c67.tar.gz |
wmemcmp: Work around ISO C compliance bug on several platforms.
* lib/wchar.in.h (wmemcmp): Consider REPLACE_WMEMCMP.
* lib/wmemcmp-impl.h (wmemcmp): Don't assume that the two wide
characters are in the range 0..INT_MAX.
* m4/wmemcmp.m4 (gl_FUNC_WMEMCMP): Test whether wmemcmp works for all
wide characters. Set REPLACE_WMEMCMP.
* m4/wchar_h.m4 (gl_WCHAR_H_DEFAULTS): Initialize REPLACE_WMEMCMP.
* modules/wchar (Makefile.am): Substitute REPLACE_WMEMCMP.
* modules/wmemcmp (configure.ac): Consider REPLACE_WMEMCMP.
* doc/posix-functions/wmemcmp.texi: Mention the bug.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/wchar.in.h | 16 | ||||
-rw-r--r-- | lib/wmemcmp-impl.h | 5 |
2 files changed, 17 insertions, 4 deletions
diff --git a/lib/wchar.in.h b/lib/wchar.in.h index 80b6652e95..6a5b18d39d 100644 --- a/lib/wchar.in.h +++ b/lib/wchar.in.h @@ -641,13 +641,25 @@ _GL_WARN_ON_USE (wmemchr, "wmemchr is unportable - " /* Compare N wide characters of S1 and S2. */ #if @GNULIB_WMEMCMP@ -# if !@HAVE_WMEMCMP@ +# if @REPLACE_WMEMCMP@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# undef wmemcmp +# define wmemcmp rpl_wmemcmp +# endif +_GL_FUNCDECL_RPL (wmemcmp, int, + (const wchar_t *s1, const wchar_t *s2, size_t n) + _GL_ATTRIBUTE_PURE); +_GL_CXXALIAS_RPL (wmemcmp, int, + (const wchar_t *s1, const wchar_t *s2, size_t n)); +# else +# if !@HAVE_WMEMCMP@ _GL_FUNCDECL_SYS (wmemcmp, int, (const wchar_t *s1, const wchar_t *s2, size_t n) _GL_ATTRIBUTE_PURE); -# endif +# endif _GL_CXXALIAS_SYS (wmemcmp, int, (const wchar_t *s1, const wchar_t *s2, size_t n)); +# endif # if __GLIBC__ >= 2 _GL_CXXALIASWARN (wmemcmp); # endif diff --git a/lib/wmemcmp-impl.h b/lib/wmemcmp-impl.h index 2b8125fe26..6148220de7 100644 --- a/lib/wmemcmp-impl.h +++ b/lib/wmemcmp-impl.h @@ -27,8 +27,9 @@ wmemcmp (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 wmemcmp 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. */ } return 0; |