diff --git a/gtk/gtktextiter.c b/gtk/gtktextiter.c
index 6ee68bd..bca7b64 100644
--- a/gtk/gtktextiter.c
+++ b/gtk/gtktextiter.c
@@ -4575,7 +4575,7 @@ utf8_strrcasestr (const gchar *haystack,
   p = g_utf8_offset_to_pointer (caseless_haystack, i);
   needle_len = strlen (needle);
 
-  while (p >= caseless_haystack)
+  while (1)
     {
       if (exact_prefix_cmp (p, needle, needle_len))
         {
@@ -4583,7 +4583,11 @@ utf8_strrcasestr (const gchar *haystack,
           goto finally;
         }
 
-      p = g_utf8_prev_char (p);
+      if (p > caseless_haystack)
+        p = g_utf8_prev_char (p);
+      else
+        goto finally;
+
       i--;
     }
 
