Skip to content

Commit

Permalink
Fix fuzzer.
Browse files Browse the repository at this point in the history
  • Loading branch information
ncruces committed Dec 12, 2024
1 parent 5ed4a6c commit 844fab4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
1 change: 1 addition & 0 deletions driver/savepoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
_ "github.com/ncruces/go-sqlite3/vfs/memdb"
)

Expand Down
34 changes: 14 additions & 20 deletions driver/whitespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package driver
func notWhitespace(sql string) bool {
const (
code = iota
minus
slash
minus
ccomment
endcomment
sqlcomment
endcomment
)

state := code
Expand All @@ -19,29 +19,33 @@ func notWhitespace(sql string) bool {
switch state {
case code:
switch b {
case '-':
state = minus
case '/':
state = slash
case '-':
state = minus
case ' ', ';', '\t', '\n', '\v', '\f', '\r':
continue
default:
return true
}
case minus:
if b != '-' {
return true
}
state = sqlcomment
case slash:
if b != '*' {
return true
}
state = ccomment
case minus:
if b != '-' {
return true
}
state = sqlcomment
case ccomment:
if b == '*' {
state = endcomment
}
case sqlcomment:
if b == '\n' {
state = code
}
case endcomment:
switch b {
case '/':
Expand All @@ -51,17 +55,7 @@ func notWhitespace(sql string) bool {
default:
state = ccomment
}
case sqlcomment:
if b == '\n' {
state = code
}
}
}

switch state {
case code, ccomment, endcomment, sqlcomment:
return false
default:
return true
}
return state == slash || state == minus
}
10 changes: 9 additions & 1 deletion driver/whitespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package driver
import (
"context"
"testing"

_ "github.com/ncruces/go-sqlite3/embed"
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
)

func Fuzz_isWhitespace(f *testing.F) {
func Fuzz_notWhitespace(f *testing.F) {
f.Add("")
f.Add(" ")
f.Add(";")
Expand All @@ -27,10 +30,15 @@ func Fuzz_isWhitespace(f *testing.F) {
defer db.Close()

f.Fuzz(func(t *testing.T, str string) {
if len(str) > 128 {
t.SkipNow()
}

c, err := db.Conn(context.Background())
if err != nil {
t.Fatal(err)
}
defer c.Close()

c.Raw(func(driverConn any) error {
conn := driverConn.(*conn).Conn
Expand Down

0 comments on commit 844fab4

Please sign in to comment.