Skip to content

Commit

Permalink
fix crash in GoNext/GoPrev seq
Browse files Browse the repository at this point in the history
  • Loading branch information
joy4eg committed Nov 6, 2024
1 parent b202a03 commit a57109a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
8 changes: 4 additions & 4 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,20 +174,20 @@ func (s *Stream) IsNextSequence(keys ...TokenKey) bool {
var (
result = true
hSize = 0
id = s.CurrentToken().ID()
)
if s.historySize > 0 && s.historySize < len(keys) {
hSize = s.historySize
s.historySize = len(keys)
}

start := s.current
for _, key := range keys {
if !s.GoNext().CurrentToken().Is(key) {
result = false
break
}
}
s.GoTo(id)
s.current = start

if hSize != 0 {
s.SetHistorySize(hSize)
Expand All @@ -200,13 +200,13 @@ func (s *Stream) IsAnyNextSequence(keys ...[]TokenKey) bool {
var (
result = true
hSize = 0
id = s.CurrentToken().ID()
)
if s.historySize > 0 && s.historySize < len(keys) {
hSize = s.historySize
s.historySize = len(keys)
}

start := s.current
for _, key := range keys {
found := false
for _, k := range key {
Expand All @@ -220,7 +220,7 @@ func (s *Stream) IsAnyNextSequence(keys ...[]TokenKey) bool {
break
}
}
s.GoTo(id)
s.current = start

if hSize != 0 {
s.SetHistorySize(hSize)
Expand Down
14 changes: 13 additions & 1 deletion stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package tokenizer
import (
"bytes"
"fmt"
"github.com/stretchr/testify/require"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestStream(t *testing.T) {
Expand Down Expand Up @@ -272,6 +273,17 @@ func TestIssue9(t *testing.T) {
}
}

func TestStreamOverflow(t *testing.T) {
parser := New()
buf := bytes.NewBuffer([]byte("a b c"))

stream := parser.ParseStream(buf, 4096)
defer stream.Close()

require.False(t, stream.IsNextSequence(TokenKeyword, TokenKeyword, TokenKeyword, TokenKeyword))
require.False(t, stream.IsNextSequence(TokenKeyword, TokenKeyword, TokenKeyword, TokenKeyword))
}

func TestIssue7(t *testing.T) {
p := New()
buf := bytes.NewBuffer(nil)
Expand Down

0 comments on commit a57109a

Please sign in to comment.