This PR modifies `replace_all` to avoid the infinite loop described in #936. In each iteration, `replace_all` searches for `needle` starting from the beginning of `haystack`. If `replacement` contains `needle`, the result is an infinite loop. To prevent this from happening, `replace_all` is should continue searching for `needle` from the end of the previous `replacement`. You can view, comment on, or merge this pull request online at:
https://github.com/geany/geany-plugins/pull/1128
-- Commit Summary --
* <a href="https://github.com/geany/geany-plugins/pull/1128/commits/187c3e2ac40abe91edcb92d522db242192784086">Markdown: Modify replace_all to avoid infinite loop.</a>
-- File Changes --
M markdown/src/viewer.c (6)
-- Patch Links --
https://github.com/geany/geany-plugins/pull/1128.patch https://github.com/geany/geany-plugins/pull/1128.diff
@xiota pushed 1 commit.
43146331b7394d1875dd17d5b73140a189fe9628 Markdown: Precalculate replacement length
@elextr commented on this pull request.
@@ -194,14 +194,15 @@ replace_all(MarkdownViewer *self,
{ gchar *ptr; gsize needle_len = strlen(needle); + gsize replacement_len_minus_one = strlen(replacement) - 1;
Why -1 ?
@xiota commented on this pull request.
@@ -194,14 +194,15 @@ replace_all(MarkdownViewer *self,
{ gchar *ptr; gsize needle_len = strlen(needle); + gsize replacement_len_minus_one = strlen(replacement) - 1;
So I'd have something to change later?
@elextr commented on this pull request.
@@ -194,14 +194,15 @@ replace_all(MarkdownViewer *self,
{ gchar *ptr; gsize needle_len = strlen(needle); + gsize replacement_len_minus_one = strlen(replacement) - 1;
Ahh, the obligatory C programming off by one bug :smile:
@xiota pushed 1 commit.
0187f7c5e954b354f0fa8b836966c0f78fce153b Markdown: Fix mindless minus one
There is a [g_string_replace()](https://docs.gtk.org/glib/method.String.replace.html) that does replace all, but its very new, probably should keep this for now.
Looks ok to me, but I don't build or use markdown plugin, so I won't test it.
There is a g_string_replace() that does replace all, but its very new, probably should keep this for now.
Requires glib 2.68... Not available for Ubuntu LTS 18.04 (glib 2.54) or 20.04 (glib 2.64).
Yeah, thats why I called it "very new".
Closed #1128.
github-comments@lists.geany.org