[glib/glib-2-68: 5/8] g_string_replace: Don't replace empty string more than once per location




commit c70eb3f0aaa510b4d7bb4e24c137842ab745c58d
Author: Simon McVittie <smcv collabora com>
Date:   Mon Aug 2 11:51:56 2021 +0100

    g_string_replace: Don't replace empty string more than once per location
    
    This matches the behaviour of Python `str.replace()`, and avoids carrying
    out 2**32 replacements before n wraps around, which is almost certainly
    not what we want.
    
    Resolves: https://gitlab.gnome.org/GNOME/glib/-/issues/2452
    Signed-off-by: Simon McVittie <smcv collabora com>

 glib/gstring.c | 9 +++++++++
 1 file changed, 9 insertions(+)
---
diff --git a/glib/gstring.c b/glib/gstring.c
index 030d75316..94d700b7a 100644
--- a/glib/gstring.c
+++ b/glib/gstring.c
@@ -995,6 +995,15 @@ g_string_replace (GString     *string,
       g_string_insert (string, pos, replace);
       cur = string->str + pos + r_len;
       n++;
+      /* Only match the empty string once at any given position, to
+       * avoid infinite loops */
+      if (f_len == 0)
+        {
+          if (cur[0] == '\0')
+            break;
+          else
+            cur++;
+        }
       if (n == limit)
         break;
     }


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