* Patch from Alexey Torgashin to handle invalid groups. Fix issue #39697

This commit is contained in:
Michaël Van Canneyt 2022-04-30 22:33:43 +02:00
parent 77851090aa
commit 20887aeadd

View File

@ -817,7 +817,7 @@ uses
const const
// TRegExpr.VersionMajor/Minor return values of these constants: // TRegExpr.VersionMajor/Minor return values of these constants:
REVersionMajor = 1; REVersionMajor = 1;
REVersionMinor = 154; REVersionMinor = 155;
OpKind_End = REChar(1); OpKind_End = REChar(1);
OpKind_MetaClass = REChar(2); OpKind_MetaClass = REChar(2);
@ -5745,10 +5745,11 @@ var
APtr := p; APtr := p;
end; end;
procedure FindSubstGroupIndex(var p: PRegExprChar; var Idx: integer); procedure FindSubstGroupIndex(var p: PRegExprChar; var Idx: integer; var NumberFound: boolean);
begin begin
Idx := ParseVarName(p); Idx := ParseVarName(p);
if (Idx >= 0) and (Idx <= High(GrpIndexes)) then NumberFound := (Idx >= 0) and (Idx <= High(GrpIndexes));
if NumberFound then
Idx := GrpIndexes[Idx]; Idx := GrpIndexes[Idx];
end; end;
@ -5759,6 +5760,7 @@ var
p, p0, p1, ResultPtr: PRegExprChar; p, p0, p1, ResultPtr: PRegExprChar;
ResultLen, n: integer; ResultLen, n: integer;
Ch, QuotedChar: REChar; Ch, QuotedChar: REChar;
GroupFound: boolean;
begin begin
// Check programm and input string // Check programm and input string
if not IsProgrammOk then if not IsProgrammOk then
@ -5780,10 +5782,12 @@ begin
Ch := p^; Ch := p^;
Inc(p); Inc(p);
n := -1; n := -1;
GroupFound := False;
if Ch = SubstituteGroupChar then if Ch = SubstituteGroupChar then
FindSubstGroupIndex(p, n); FindSubstGroupIndex(p, n, GroupFound);
if n >= 0 then if GroupFound then
begin begin
if n >= 0 then
Inc(ResultLen, GrpEnd[n] - GrpStart[n]); Inc(ResultLen, GrpEnd[n] - GrpStart[n]);
end end
else else
@ -5835,13 +5839,19 @@ begin
Inc(p); Inc(p);
p1 := p; p1 := p;
n := -1; n := -1;
GroupFound := False;
if Ch = SubstituteGroupChar then if Ch = SubstituteGroupChar then
FindSubstGroupIndex(p, n); FindSubstGroupIndex(p, n, GroupFound);
if (n >= 0) then if GroupFound then
begin
if n >= 0 then
begin begin
p0 := GrpStart[n]; p0 := GrpStart[n];
p1 := GrpEnd[n]; p1 := GrpEnd[n];
end end
else
p1 := p0;
end
else else
begin begin
if (Ch = EscChar) and (p < TemplateEnd) then if (Ch = EscChar) and (p < TemplateEnd) then