From 70bca27c0a54accc637d7e23a8089126b00f2113 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Mon, 23 Oct 2023 01:20:04 +0200 Subject: [PATCH] Fix infinite recursion in ReplacedString ... when is empty --- lib/init.g | 6 ++++++ tst/testinstall/strings.tst | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/init.g b/lib/init.g index cea842f139..40faba053b 100644 --- a/lib/init.g +++ b/lib/init.g @@ -104,7 +104,13 @@ infinity := "2b defined"; ## ReplacedString := function ( str, old, new, arg... ) local lss, all, p, s, pp; + if old = new then + return str; + fi; lss := LEN_LIST( old ); + if lss = 0 then + Error(" must not be empty"); + fi; if LEN_LIST( arg ) > 0 then all := arg[1] = "all"; else diff --git a/tst/testinstall/strings.tst b/tst/testinstall/strings.tst index d0dd5388b7..84439e4d9a 100644 --- a/tst/testinstall/strings.tst +++ b/tst/testinstall/strings.tst @@ -209,5 +209,13 @@ gap> for len in [10,100,1000,10000,100000] do > Assert(0, Concatenation(str,"\n") = DisplayString(str)); > od;; +# +gap> ReplacedString("Hello world", "Hello", "Goodbye"); +"Goodbye world" +gap> ReplacedString("Hello world", "", ""); +"Hello world" +gap> ReplacedString("Hello world", "", "*"); +Error, must not be empty + # gap> STOP_TEST( "strings.tst", 1);