mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-10-25 10:40:57 +00:00
Fix larger than WCHAR_MAX differences in wcs{,n}cmp (#795)
The C standard states:
> Unless explicitly stated otherwise, the functions described in this
> subclause order two wide characters the same way as two integers of
> the underlying integer type designated by wchar_t.
>
> [...]
>
> The wcscmp function returns an integer greater than, equal to, or
> less than zero, accordingly as the wide string pointed to by s1 is
> greater than, equal to, or less than the wide string pointed to by
> s2.
>
> [...]
>
> The wcsncmp function returns an integer greater than, equal to, or
> less than zero, accordingly as the possibly null-terminated array
> pointed to by s1 is greater than, equal to, or less than the
> possibly null-terminated array pointed to by s2.
- C Standard, 7.31.4.4. Wide string comparison functions
Cosmopolitan fails to obey this in cases where the difference between
two wide characters is larger than WCHAR_MAX.
This means that, for example, the following program:
#include <stdio.h>
#include <wchar.h>
#include <limits.h>
int main()
{
wchar_t str1[] = { WCHAR_MIN, L'\0' };
wchar_t str2[] = { WCHAR_MAX, L'\0' };
printf("%d\n", wcscmp(str1, str2));
printf("%d\n", wcsncmp(str1, str2, 2));
}
will print `1` twice, instead of the negative numbers mandated by the
standard (as WCHAR_MIN is less than WCHAR_MAX)
This patch fixes this, along with the associated Github issue,
https://github.com/jart/cosmopolitan/issues/783
This commit is contained in:
parent
999481ace0
commit
ba42248575
3 changed files with 10 additions and 10 deletions
|
|
@ -30,5 +30,5 @@ int wcscmp(const wchar_t *a, const wchar_t *b) {
|
|||
size_t i = 0;
|
||||
if (a == b) return 0;
|
||||
while (a[i] == b[i] && b[i]) ++i;
|
||||
return (unsigned)a[i] - (unsigned)b[i];
|
||||
return (a[i] > b[i]) - (a[i] < b[i]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,5 +30,5 @@ int wcsncmp(const wchar_t *a, const wchar_t *b, size_t n) {
|
|||
size_t i = 0;
|
||||
if (!n-- || a == b) return 0;
|
||||
while (i < n && a[i] == b[i] && b[i]) ++i;
|
||||
return (unsigned)a[i] - (unsigned)b[i];
|
||||
return (a[i] > b[i]) - (a[i] < b[i]);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue