text: Fix a crash when retrieving the selected text
authorTimm Bäder <mail@baedert.org>
Tue, 27 Aug 2019 08:05:51 +0000 (10:05 +0200)
committerTimm Bäder <mail@baedert.org>
Mon, 9 Sep 2019 15:36:25 +0000 (17:36 +0200)
gtk/gtktext.c

index 59281d5e990bfc418ff4e0e0c9097e61a9c4fe65..5ac7637a98ac92eb1580452904e0ded8e2ac6289 100644 (file)
@@ -2695,9 +2695,12 @@ _gtk_text_get_selected_text (GtkText *self)
 
   if (priv->selection_bound != priv->current_pos)
     {
+      const int start = MIN (priv->selection_bound, priv->current_pos);
+      const int end = MAX (priv->selection_bound, priv->current_pos);
       const char *text = gtk_entry_buffer_get_text (get_buffer (self));
-      int start_index = g_utf8_offset_to_pointer (text, priv->selection_bound) - text;
-      int end_index = g_utf8_offset_to_pointer (text, priv->current_pos) - text;
+      const int start_index = g_utf8_offset_to_pointer (text, start) - text;
+      const int end_index = g_utf8_offset_to_pointer (text, end) - text;
+
       return g_strndup (text + start_index, end_index - start_index);
     }