Skip to content

Commit

Permalink
Fix infinite recursion in ReplacedString
Browse files Browse the repository at this point in the history
... when <old> is empty
  • Loading branch information
fingolfin committed Oct 22, 2023
1 parent 58d9d53 commit 70bca27
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/init.g
Original file line number Diff line number Diff line change
Expand Up @@ -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("<old> must not be empty");
fi;
if LEN_LIST( arg ) > 0 then
all := arg[1] = "all";
else
Expand Down
8 changes: 8 additions & 0 deletions tst/testinstall/strings.tst
Original file line number Diff line number Diff line change
Expand Up @@ -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, <old> must not be empty

#
gap> STOP_TEST( "strings.tst", 1);

0 comments on commit 70bca27

Please sign in to comment.