xen/lib: Fix strcmp() and strncmp()
authorJane Malalane <jane.malalane@citrix.com>
Tue, 27 Jul 2021 18:47:15 +0000 (19:47 +0100)
committerIan Jackson <iwj@xenproject.org>
Fri, 30 Jul 2021 09:52:46 +0000 (10:52 +0100)
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 <andrew.cooper3@citrix.com>
Signed-off-by: Jane Malalane <jane.malalane@citrix.com>
Reviewed-by: Ian Jackson <iwj@xenproject.org>
xen/lib/strcmp.c
xen/lib/strncmp.c

index 465f1c4191e512aad836f2f34a5a3ae624f19cf5..f85c1e8741f74fce7dd11d04341b7eedea77a8f1 100644 (file)
  */
 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;
 }
 
 /*
index 9af7fa1c99b9d0157187f9fecc12f13fb398e3ae..1480f58c2eb4fb4fdc258c224548d9256b13010d 100644 (file)
  */
 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;
 }
 
 /*