Skip to content

Commit

Permalink
Merge pull request #1232 from xiota/pr-markdown-replace-all
Browse files Browse the repository at this point in the history
Markdown: Modify replace_all to avoid infinite loop

Fixes #936.
  • Loading branch information
b4n authored Apr 25, 2024
2 parents 644550b + a9914c7 commit d851fac
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions markdown/src/viewer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down

0 comments on commit d851fac

Please sign in to comment.