Skip to content

Commit

Permalink
fixes dlclark#31 grow stack spaces when performing a jump operation b…
Browse files Browse the repository at this point in the history
…ackward AND to the same address
  • Loading branch information
Douglas Clark authored and Douglas Clark committed Oct 7, 2020
1 parent 694b378 commit bbc9f75
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
34 changes: 34 additions & 0 deletions regexp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1058,3 +1058,37 @@ func TestBadGroupConstruct(t *testing.T) {
}
}
}

func TestEmptyCaptureLargeRepeat(t *testing.T) {
// a bug would cause our track to not grow and eventually panic
// with large numbers of repeats of a non-capturing group (>16)

// the issue was that the jump occured to the same statement over and over
// and the "grow stack/track" logic only triggered on jumps that moved
// backwards

r := MustCompile(`(?:){40}`, 0)
m, err := r.FindStringMatch("1")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if want, got := 0, m.Index; want != got {
t.Errorf("First Match Index wanted %v got %v", want, got)
}
if want, got := 0, m.Length; want != got {
t.Errorf("First Match Length wanted %v got %v", want, got)
}

m, _ = r.FindNextMatch(m)
if want, got := 1, m.Index; want != got {
t.Errorf("Second Match Index wanted %v got %v", want, got)
}
if want, got := 0, m.Length; want != got {
t.Errorf("Second Match Length wanted %v got %v", want, got)
}

m, _ = r.FindNextMatch(m)
if m != nil {
t.Fatal("Expected 2 matches, got more")
}
}
4 changes: 2 additions & 2 deletions runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -951,8 +951,8 @@ func (r *runner) advance(i int) {
}

func (r *runner) goTo(newpos int) {
// when branching backward, ensure storage
if newpos < r.codepos {
// when branching backward or in place, ensure storage
if newpos <= r.codepos {
r.ensureStorage()
}

Expand Down

0 comments on commit bbc9f75

Please sign in to comment.