From a9914c7df9729cf8db4c5e9f5f20082c68fd158c Mon Sep 17 00:00:00 2001 From: xiota Date: Sat, 4 Mar 2023 12:21:32 -0800 Subject: [PATCH] Markdown: Modify replace_all to avoid infinite loop --- markdown/src/viewer.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/markdown/src/viewer.c b/markdown/src/viewer.c index b9ab58f70..0884c1a3e 100644 --- a/markdown/src/viewer.c +++ b/markdown/src/viewer.c @@ -194,12 +194,15 @@ replace_all(MarkdownViewer *self, { gchar *ptr; gsize needle_len = strlen(needle); + gsize replacement_len = strlen(replacement); + goffset offset = 0; /* For each occurrence of needle in haystack */ - while ((ptr = strstr(haystack->str, needle)) != NULL) { - goffset offset = ptr - haystack->str; + while ((ptr = strstr(haystack->str + offset, needle)) != NULL) { + offset = ptr - haystack->str; g_string_erase(haystack, offset, needle_len); g_string_insert(haystack, offset, replacement); + offset += replacement_len; } }