[glib] Fix a corner-case in g_utf8_find_next_char



commit e0e652e4032a181d4f0b0a12aeddf0678b7a3c04
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Oct 4 15:28:02 2015 -0400

    Fix a corner-case in g_utf8_find_next_char
    
    In the case that *p is '\0', we should return p + 1, not p.
    This change allows to simplify g_utf8_find_next_char a bit.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=547200

 glib/gutf8.c              |   18 ++++++++++--------
 glib/tests/utf8-pointer.c |    8 +++++++-
 2 files changed, 17 insertions(+), 9 deletions(-)
---
diff --git a/glib/gutf8.c b/glib/gutf8.c
index 99b89da..eae10da 100644
--- a/glib/gutf8.c
+++ b/glib/gutf8.c
@@ -168,16 +168,18 @@ gchar *
 g_utf8_find_next_char (const gchar *p,
                       const gchar *end)
 {
-  if (*p)
+  if (end)
     {
-      if (end)
-       for (++p; p < end && (*p & 0xc0) == 0x80; ++p)
-         ;
-      else
-       for (++p; (*p & 0xc0) == 0x80; ++p)
-         ;
+      for (++p; p < end && (*p & 0xc0) == 0x80; ++p)
+        ;
+      return (p >= end) ? NULL : (gchar *)p;
+    }
+  else
+    {
+      for (++p; (*p & 0xc0) == 0x80; ++p)
+        ;
+      return (gchar *)p;
     }
-  return (p == end) ? NULL : (gchar *)p;
 }
 
 /**
diff --git a/glib/tests/utf8-pointer.c b/glib/tests/utf8-pointer.c
index 90c128f..6fa4b4c 100644
--- a/glib/tests/utf8-pointer.c
+++ b/glib/tests/utf8-pointer.c
@@ -97,7 +97,7 @@ test_find (void)
    * U+0041 Latin Capital Letter A (\101)
    * U+1EB6 Latin Capital Letter A With Breve And Dot Below (\341\272\266)
    */
-  const gchar *str = "\340\254\213\360\220\244\200\101\341\272\266";
+  const gchar *str = "\340\254\213\360\220\244\200\101\341\272\266\0\101";
   const gchar *p = str + strlen (str);
   const gchar *q;
 
@@ -122,6 +122,12 @@ test_find (void)
   g_assert (q == str + 3);
   q = g_utf8_find_next_char (q, str + 6);
   g_assert (q == NULL);
+
+  q = g_utf8_find_next_char (str, str);
+  g_assert (q == NULL);
+
+  q = g_utf8_find_next_char (str + strlen (str), NULL);
+  g_assert (q == str + strlen (str) + 1);
 }
 
 int main (int argc, char *argv[])


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