[gnumeric] EXACT: Fix potential signedness problems in implementation.



commit 56030dfe2092937dc21d13bf280c5887396b27aa
Author: Morten Welinder <terra gnome org>
Date:   Sat Dec 12 14:24:57 2015 -0500

    EXACT: Fix potential signedness problems in implementation.

 plugins/fn-string/ChangeLog   |    5 +++
 plugins/fn-string/functions.c |   56 ++++++++++++++++++++++-------------------
 2 files changed, 35 insertions(+), 26 deletions(-)
---
diff --git a/plugins/fn-string/ChangeLog b/plugins/fn-string/ChangeLog
index 522f2f6..71f3a98 100644
--- a/plugins/fn-string/ChangeLog
+++ b/plugins/fn-string/ChangeLog
@@ -1,3 +1,8 @@
+2015-12-12  Morten Welinder  <terra gnome org>
+
+       * functions.c (gnm_compare_strings): Don't make assumptions of
+       signedness of "char" type.
+
 2015-09-20  Morten Welinder <terra gnome org>
 
        * Release 1.12.24
diff --git a/plugins/fn-string/functions.c b/plugins/fn-string/functions.c
index dc127df..dce78a4 100644
--- a/plugins/fn-string/functions.c
+++ b/plugins/fn-string/functions.c
@@ -200,44 +200,48 @@ gnm_compare_strings (const char *cstr1, const char *cstr2)
 {
        const char *a = cstr1, *b = cstr2;
        char *str1, *str2;
-       gboolean val;
+       gboolean eq;
 
-       /* If the string is identical in code points even without normalization */
-       /* then it is exactly the same. */
+       /* Skip leading ASCII prefixes that match.  */
        while (*a == *b && *a != 0 && *b != 0)
                a++, b++;
+       /*
+        * If we've hit the end of one string, we ought to have hit the
+        * end of the other.  Otherwise the strings are different.
+        */
+       if (*a == 0 || *b == 0)
+               return *a == *b;
 
-       if (*a == 0)
-               return (*b == 0);
-       if (*b == 0)
-               return FALSE;
-
-       /* If they differ in two ASCII characters, the string must be distinct. */
-       if (*a > 0 && *a < 128 && *b > 0 && *b < 128)
+       /*
+        * If they differ in two ASCII characters (including terminating
+        * NULs), the strings must be distinct.
+        */
+       if ((guchar)*a < 128 && (guchar)*b < 128)
                return FALSE;
 
-       /* We are using NFD normalization, ie. Characters are decomposed by */
-       /* canonical equivalence, and multiple combining characters are arranged */
-       /* in a specific order. Note that ligatures remain ligatures, formatting */
-       /* such as subscript-3 versus 3 are retained. */
-
-       /* Note that for example, the distinct Unicode strings "U+212B" */
-       /*(the angstrom sign "Å") and "U+00C5" (the Swedish letter "Å") */
-       /* are both expanded by NFD (or NFKD) into the sequence */
-       /*"U+0041 U+030A" (Latin letter "A" and combining ring above "°") */
-       /* Of course "U+0041 U+030A" is retained in form, so we need to work with */
-       /* at least the last ASCII character. Performance should nearly be */
-       /* identical to using all */
-
+       /*
+        * We are using NFD normalization, ie. Characters are decomposed by
+        * canonical equivalence, and multiple combining characters are
+        * arranged in a specific order. Note that ligatures remain ligatures,
+        * formatting such as subscript-3 versus 3 are retained.
+        *
+        * Note that for example, the distinct Unicode strings "U+212B"
+        * (the angstrom sign "Å") and "U+00C5" (the Swedish letter "Å")
+        * are both expanded by NFD (or NFKD) into the sequence
+        * "U+0041 U+030A" (Latin letter "A" and combining ring above "°")
+        * Of course "U+0041 U+030A" is retained in form, so we need to work
+        * with at least the last ASCII character. Performance should nearly
+        * be identical to using all
+        */
        str1 = g_utf8_normalize (cstr1, -1, G_NORMALIZE_DEFAULT);
        str2 = g_utf8_normalize (cstr2, -1, G_NORMALIZE_DEFAULT);
 
-       val = (g_strcmp0 (str1, str2) == 0);
+       eq = (g_strcmp0 (str1, str2) == 0);
 
        g_free (str1);
        g_free (str2);
 
-       return val;
+       return eq;
 }
 
 static GnmFuncHelp const help_exact[] = {


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