Skip to content

Commit

Permalink
Markdown: Modify replace_all to avoid infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
xiota committed Mar 4, 2023
1 parent 2665511 commit a9914c7
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 a9914c7

Please sign in to comment.