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




commit 0a8c7e57ab3737be4fff795deab8ab5e7e975377
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]