[PATCH xserver] FIX:xf86nameCompare fix corner case compare with NULL

Jens Harms au1064 at gmail.com
Wed Jan 2 16:02:25 UTC 2019


FIX:There is an bug in xf86nameCompare, if you compare
a zero string with a non-zero string the result is inverted.
s1=zero string, s2 = non-zero string
this should return a value less than zero but the code returns 1.

-    if (!s1 || *s1 == 0) {
-        if (!s2 || *s2 == 0)
-            return 0;
-        else
-            return 1;               // s1 == ZERO, s2 == NOT ZERO
-    } else if (!s2 || *s2 == 0) {
-        return -1;

The patch also rewrites and shrinkes the code to improve readability.
Signed-off-by: Jens Harms <au1064 at gmail.com>


---
 hw/xfree86/parser/scan.c | 56 ++++++++++++++++++----------------------
 1 file changed, 25 insertions(+), 31 deletions(-)

diff --git a/hw/xfree86/parser/scan.c b/hw/xfree86/parser/scan.c
index 1eb35ed73..7d63f0983 100644
--- a/hw/xfree86/parser/scan.c
+++ b/hw/xfree86/parser/scan.c
@@ -1006,45 +1006,39 @@ xf86getStringToken(const xf86ConfigSymTabRec * tab)
     return StringToToken(xf86_lex_val.str, tab);
 }

+
+/* skips space, underscore and tab, increments pointer.
+   returns: lowercase character code
+*/
+inline static char skipws(const char **s)
+{
+    if( *s == NULL ) return 0;
+    while (**s == '_' || **s == ' ' || **s == '\t')
+        (*s)++;
+    char ch = tolower(**s) ;
+    (*s)++;
+    return ch;
+}
+
 /*
- * Compare two names.  The characters '_', ' ', and '\t' are ignored
+ * Compare two names.  The characters '_', ' ', '\t',
+ * and upper/lower case are ignored
  * in the comparison.
  */
 int
 xf86nameCompare(const char *s1, const char *s2)
 {
-    char c1, c2;
-
-    if (!s1 || *s1 == 0) {
-        if (!s2 || *s2 == 0)
-            return 0;
-        else
-            return 1;
-    } else if (!s2 || *s2 == 0) {
-        return -1;
-    }
-
-    while (*s1 == '_' || *s1 == ' ' || *s1 == '\t')
-        s1++;
-    while (*s2 == '_' || *s2 == ' ' || *s2 == '\t')
-        s2++;
-    c1 = (isupper(*s1) ? tolower(*s1) : *s1);
-    c2 = (isupper(*s2) ? tolower(*s2) : *s2);
-    while (c1 == c2) {
-        if (c1 == '\0')
-            return 0;
-        s1++;
-        s2++;
-        while (*s1 == '_' || *s1 == ' ' || *s1 == '\t')
-            s1++;
-        while (*s2 == '_' || *s2 == ' ' || *s2 == '\t')
-            s2++;
-        c1 = (isupper(*s1) ? tolower(*s1) : *s1);
-        c2 = (isupper(*s2) ? tolower(*s2) : *s2);
-    }
-    return c1 - c2;
+    register char c1, c2, ret;
+
+    do {
+        c1 = skipws(&s1);
+        c2 = skipws(&s2);
+        ret = c1 - c2;
+    } while( !ret && c1 );
+    return ret;
 }

+
 char *
 xf86addComment(char *cur, const char *add)
 {
-- 
2.17.1
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.x.org/archives/xorg-devel/attachments/20190102/84a59dfd/attachment.html>


More information about the xorg-devel mailing list