libc/string: strcmp/strncmp cast unsigned char

Summary:
- Cast to unsigned char for strcmp and strncmp
- strcmp and strncmp are described following by opengroup.org

  The sign of a non-zero return value shall be determined by the sign
  of the difference between the values of the first pair of bytes
  (both interpreted as type unsigned char) that differ in the strings
  being compared.

  https://pubs.opengroup.org/onlinepubs/9699919799/functions/strcmp.html
  https://pubs.opengroup.org/onlinepubs/9699919799/functions/strncmp.html

Impact:
- strcmp and strncmp return value

Testing:
- ostest on sabre-6quad:smp w/ qemu

Signed-off-by: Oki Minabe <minabe.oki@gmail.com>
This commit is contained in:
Oki Minabe
2022-04-23 16:23:25 +09:00
committed by Petro Karashchenko
parent 54508a3798
commit 185b1cb1b7
2 changed files with 8 additions and 4 deletions
+6 -3
View File
@@ -34,11 +34,14 @@
#undef strcmp /* See mm/README.txt */
int strcmp(FAR const char *cs, FAR const char *ct)
{
register signed char result;
register int result;
for (; ; )
{
if ((result = *cs - *ct++) != 0 || !*cs++)
break;
if ((result = (unsigned char)*cs - (unsigned char)*ct++) != 0 ||
!*cs++)
{
break;
}
}
return result;
+2 -1
View File
@@ -37,7 +37,8 @@ int strncmp(const char *cs, const char *ct, size_t nb)
int result = 0;
for (; nb > 0; nb--)
{
if ((result = (int)*cs - (int)*ct++) != 0 || !*cs++)
if ((result = (unsigned char)*cs - (unsigned char)*ct++) != 0 ||
!*cs++)
{
break;
}