Fix fuzzer.

This commit is contained in:
Nuno Cruces
2024-12-12 13:38:46 +00:00
parent 5ed4a6cb9d
commit 844fab4167
3 changed files with 24 additions and 21 deletions

View File

@@ -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"
)

View File

@@ -3,11 +3,11 @@ package driver
func notWhitespace(sql string) bool {
const (
code = iota
minus
slash
minus
ccomment
endcomment
sqlcomment
endcomment
)
state := code
@@ -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 '/':
@@ -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
}

View File

@@ -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(";")
@@ -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