[glib/wip/smcv/2452-g-string-0-length-replace: 4/5] g_string_replace: Don't replace empty string more than once per location




commit ae0ab2bb8b56009f42b6620bbfffe55911e50c4b
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 ad2ec1771..e3ec98b0b 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]