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/driver"
_ "github.com/ncruces/go-sqlite3/embed" _ "github.com/ncruces/go-sqlite3/embed"
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
_ "github.com/ncruces/go-sqlite3/vfs/memdb" _ "github.com/ncruces/go-sqlite3/vfs/memdb"
) )

View File

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

View File

@@ -3,9 +3,12 @@ package driver
import ( import (
"context" "context"
"testing" "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(" ") f.Add(" ")
f.Add(";") f.Add(";")
@@ -27,10 +30,15 @@ func Fuzz_isWhitespace(f *testing.F) {
defer db.Close() defer db.Close()
f.Fuzz(func(t *testing.T, str string) { f.Fuzz(func(t *testing.T, str string) {
if len(str) > 128 {
t.SkipNow()
}
c, err := db.Conn(context.Background()) c, err := db.Conn(context.Background())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer c.Close()
c.Raw(func(driverConn any) error { c.Raw(func(driverConn any) error {
conn := driverConn.(*conn).Conn conn := driverConn.(*conn).Conn