Skip to content

Commit bbc9f75

Browse files
Douglas ClarkDouglas Clark
authored andcommitted
fixes #31 grow stack spaces when performing a jump operation backward AND to the same address
1 parent 694b378 commit bbc9f75

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

regexp_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,3 +1058,37 @@ func TestBadGroupConstruct(t *testing.T) {
10581058
}
10591059
}
10601060
}
1061+
1062+
func TestEmptyCaptureLargeRepeat(t *testing.T) {
1063+
// a bug would cause our track to not grow and eventually panic
1064+
// with large numbers of repeats of a non-capturing group (>16)
1065+
1066+
// the issue was that the jump occured to the same statement over and over
1067+
// and the "grow stack/track" logic only triggered on jumps that moved
1068+
// backwards
1069+
1070+
r := MustCompile(`(?:){40}`, 0)
1071+
m, err := r.FindStringMatch("1")
1072+
if err != nil {
1073+
t.Fatalf("Unexpected error: %v", err)
1074+
}
1075+
if want, got := 0, m.Index; want != got {
1076+
t.Errorf("First Match Index wanted %v got %v", want, got)
1077+
}
1078+
if want, got := 0, m.Length; want != got {
1079+
t.Errorf("First Match Length wanted %v got %v", want, got)
1080+
}
1081+
1082+
m, _ = r.FindNextMatch(m)
1083+
if want, got := 1, m.Index; want != got {
1084+
t.Errorf("Second Match Index wanted %v got %v", want, got)
1085+
}
1086+
if want, got := 0, m.Length; want != got {
1087+
t.Errorf("Second Match Length wanted %v got %v", want, got)
1088+
}
1089+
1090+
m, _ = r.FindNextMatch(m)
1091+
if m != nil {
1092+
t.Fatal("Expected 2 matches, got more")
1093+
}
1094+
}

runner.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -951,8 +951,8 @@ func (r *runner) advance(i int) {
951951
}
952952

953953
func (r *runner) goTo(newpos int) {
954-
// when branching backward, ensure storage
955-
if newpos < r.codepos {
954+
// when branching backward or in place, ensure storage
955+
if newpos <= r.codepos {
956956
r.ensureStorage()
957957
}
958958

0 commit comments

Comments
 (0)