From 07616721f1fa6cb215ffbef23441cae80412484f Mon Sep 17 00:00:00 2001 From: Gabriel Ravier Date: Wed, 4 Jan 2023 16:07:19 +0100 Subject: fix return value of wcs{,n}cmp for extreme wchar_t values As a result of using simple subtraction to implement the return values for wcscmp and wcsncmp, integer overflow can occur (producing undefined behavior, and in practice, a wrong comparison result). This does not occur for meaningful character values (21-bit range) but the functions are specified to work on arbitrary wchar_t arrays. This patch replaces the subtraction with a little bit of code that orders the characters correctly, returning -1 if the character from the first string is smaller than the one from the second, 0 if they are equal and 1 if the character from the first string is larger than the one from the second. --- src/string/wcscmp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/string/wcscmp.c') diff --git a/src/string/wcscmp.c b/src/string/wcscmp.c index 26eeee70..286ec3ea 100644 --- a/src/string/wcscmp.c +++ b/src/string/wcscmp.c @@ -3,5 +3,5 @@ int wcscmp(const wchar_t *l, const wchar_t *r) { for (; *l==*r && *l && *r; l++, r++); - return *l - *r; + return *l < *r ? -1 : *l > *r; } -- cgit 1.4.1