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

[Xen-devel] [PATCH] xen/string: Fix strcmp() and strncmp()



The C spec requires that the comarison be done in terms of unsigned char.

The code style in this file is terrible, but does claim to be Xen BSD style,
so fix up these functions while rewriting them.

Signed-off-by: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
---
CC: Jan Beulich <JBeulich@xxxxxxxx>
CC: Wei Liu <wei.liu2@xxxxxxxxxx>
CC: Roger Pau Monné <roger.pau@xxxxxxxxxx>
---
 xen/common/string.c | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)

diff --git a/xen/common/string.c b/xen/common/string.c
index 1e122ab..24491bd 100644
--- a/xen/common/string.c
+++ b/xen/common/string.c
@@ -119,14 +119,16 @@ EXPORT_SYMBOL(strlcat);
  */
 int (strcmp)(const char *cs, const char *ct)
 {
-       register signed char __res;
+    for ( ; ; )
+    {
+        unsigned char c1 = *cs++, c2 = *ct++;
 
-       while (1) {
-               if ((__res = *cs - *ct++) != 0 || !*cs++)
-                       break;
-       }
+        if ( c1 != c2 )
+            return c1 - c2;
 
-       return __res;
+        if ( c1 == '\0' )
+            return 0;
+    }
 }
 #endif
 
@@ -139,15 +141,18 @@ int (strcmp)(const char *cs, const char *ct)
  */
 int (strncmp)(const char *cs, const char *ct, size_t count)
 {
-       register signed char __res = 0;
+    for ( ; count; count-- )
+    {
+        unsigned char c1 = *cs++, c2 = *ct++;
 
-       while (count) {
-               if ((__res = *cs - *ct++) != 0 || !*cs++)
-                       break;
-               count--;
-       }
+        if ( c1 != c2 )
+            return c1 - c2;
+
+        if ( c1 == '\0' )
+            break;
+    }
 
-       return __res;
+    return 0;
 }
 #endif
 
-- 
2.1.4


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/xen-devel

 


Rackspace

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