Skip to content

Commit

Permalink
xen/lib: Fix strcmp() and strncmp()
Browse files Browse the repository at this point in the history
The C standard requires that each character be compared as unsigned
char. Xen's current behaviour compares as signed char, which changes
the answer when chars with a value greater than 0x7f are used.

Suggested-by: Andrew Cooper <[email protected]>
Signed-off-by: Jane Malalane <[email protected]>
Reviewed-by: Ian Jackson <[email protected]>
  • Loading branch information
janeyasmin authored and ijackson-citrix committed Jul 30, 2021
1 parent 58ad654 commit 3747a2b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 5 additions & 3 deletions xen/lib/strcmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
*/
int (strcmp)(const char *cs, const char *ct)
{
register signed char __res;
unsigned char *csu = (unsigned char *)cs;
unsigned char *ctu = (unsigned char *)ct;
int res;

while (1) {
if ((__res = *cs - *ct++) != 0 || !*cs++)
if ((res = *csu - *ctu++) != 0 || !*csu++)
break;
}

return __res;
return res;
}

/*
Expand Down
8 changes: 5 additions & 3 deletions xen/lib/strncmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
*/
int (strncmp)(const char *cs, const char *ct, size_t count)
{
register signed char __res = 0;
unsigned char *csu = (unsigned char *)cs;
unsigned char *ctu = (unsigned char *)ct;
int res = 0;

while (count) {
if ((__res = *cs - *ct++) != 0 || !*cs++)
if ((res = *csu - *ctu++) != 0 || !*csu++)
break;
count--;
}

return __res;
return res;
}

/*
Expand Down

0 comments on commit 3747a2b

Please sign in to comment.