[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[xen staging] xen/lib: Fix strcmp() and strncmp()



commit 3747a2bb67daa5a8baeff6cda57dc98a5ef79c3e
Author:     Jane Malalane <jane.malalane@xxxxxxxxxx>
AuthorDate: Tue Jul 27 19:47:15 2021 +0100
Commit:     Ian Jackson <iwj@xxxxxxxxxxxxxx>
CommitDate: Fri Jul 30 10:52:46 2021 +0100

    xen/lib: Fix strcmp() and strncmp()
    
    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@xxxxxxxxxx>
    Signed-off-by: Jane Malalane <jane.malalane@xxxxxxxxxx>
    Reviewed-by: Ian Jackson <iwj@xxxxxxxxxxxxxx>
---
 xen/lib/strcmp.c  | 8 +++++---
 xen/lib/strncmp.c | 8 +++++---
 2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/xen/lib/strcmp.c b/xen/lib/strcmp.c
index 465f1c4191..f85c1e8741 100644
--- a/xen/lib/strcmp.c
+++ b/xen/lib/strcmp.c
@@ -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;
 }
 
 /*
diff --git a/xen/lib/strncmp.c b/xen/lib/strncmp.c
index 9af7fa1c99..1480f58c2e 100644
--- a/xen/lib/strncmp.c
+++ b/xen/lib/strncmp.c
@@ -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;
 }
 
 /*
--
generated by git-patchbot for /home/xen/git/xen.git#staging



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.