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

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


  • To: Xen-devel <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Jane Malalane <jane.malalane@xxxxxxxxxx>
  • Date: Tue, 27 Jul 2021 19:47:15 +0100
  • Authentication-results: esa4.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none
  • Cc: Jane Malalane <jane.malalane@xxxxxxxxxx>, Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, George Dunlap <george.dunlap@xxxxxxxxxx>, "Ian Jackson" <iwj@xxxxxxxxxxxxxx>, Jan Beulich <jbeulich@xxxxxxxx>, Julien Grall <julien@xxxxxxx>, Stefano Stabellini <sstabellini@xxxxxxxxxx>, Wei Liu <wl@xxxxxxx>
  • Delivery-date: Tue, 27 Jul 2021 18:48:42 +0000
  • Ironport-hdrordr: A9a23:iEMPOq2BGLfljuLkaLaGTAqjBLokLtp133Aq2lEZdPRUGvb4qy nOpoV86faaskdzZJhNo7+90cq7MBThHPxOkOss1N6ZNWGM0gbFEGgL1/qH/9SKIU3DH4Bmu5 uIC5IObeHNMQ==
  • Ironport-sdr: NHnH4aVTJR8qv93f14zQ1itk46UxNChB8DQjwevEBUyf+9C51KenV/yGjquRnHFNpaFq0OqE1R XLzA8CAOjQSynDP9D4WMd/5CHI83/ZYcpL7AsjUW1e7EHlmFKqlIDuEaCa5ve+slcv7RyiPrhV wg0Wgw108fs+xx5vv5f5YOfj8XREXaGzHoflr1r1fJTCkzRyAMKt4ZXYWlMS+RIJ7cJgd1us1H tnQMSNuLSwNdUwpPmkqtQ2Byi5+uPlafF1H9Pzn0BMKVy/bYymk89ClXjxhYLBVLDGI1HMWxe5 OGOJAya32U20hqEt22F0ZTPf
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

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>
---
CC: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
CC: George Dunlap <george.dunlap@xxxxxxxxxx>
CC: Ian Jackson <iwj@xxxxxxxxxxxxxx>
CC: Jan Beulich <jbeulich@xxxxxxxx>
CC: Julien Grall <julien@xxxxxxx>
CC: Stefano Stabellini <sstabellini@xxxxxxxxxx>
CC: Wei Liu <wl@xxxxxxx>
---
 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;
 }
 
 /*
-- 
2.11.0




 


Rackspace

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