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

Submitted by Jens Harms on Jan. 2, 2019, 4:02 p.m.

Details

Message ID CAJ3Gro9JZcEtwTgYjRZWSJ6d5Nk3mSC_OJ4oDzrhNL0zaoagaQ@mail.gmail.com
State New
Headers show
Series "FIX:xf86nameCompare fix corner case compare with NULL" ( rev: 1 ) in X.org

Not browsing as part of any series.

Commit Message

Jens Harms Jan. 2, 2019, 4:02 p.m.
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@gmail.com>


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

Patch hide | download patch | download mbox

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)
 {