-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
129 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package driver | ||
|
||
func notWhitespace(sql string) bool { | ||
const ( | ||
code = iota | ||
minus | ||
slash | ||
ccomment | ||
endcomment | ||
sqlcomment | ||
) | ||
|
||
state := code | ||
for _, b := range ([]byte)(sql) { | ||
if b == 0 { | ||
break | ||
} | ||
|
||
switch state { | ||
case code: | ||
switch b { | ||
case '-': | ||
state = minus | ||
case '/': | ||
state = slash | ||
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 ccomment: | ||
if b == '*' { | ||
state = endcomment | ||
} | ||
case endcomment: | ||
switch b { | ||
case '/': | ||
state = code | ||
case '*': | ||
state = endcomment | ||
default: | ||
state = ccomment | ||
} | ||
case sqlcomment: | ||
if b == '\n' { | ||
state = code | ||
} | ||
} | ||
} | ||
|
||
switch state { | ||
case code, ccomment, endcomment, sqlcomment: | ||
return false | ||
default: | ||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package driver | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
) | ||
|
||
func Fuzz_isWhitespace(f *testing.F) { | ||
f.Add("") | ||
f.Add(" ") | ||
f.Add(";") | ||
f.Add("0") | ||
f.Add("-") | ||
f.Add("--") | ||
f.Add("/*") | ||
f.Add("/*/") | ||
f.Add("/**") | ||
f.Add("/**0/") | ||
f.Add("\v") | ||
f.Add(" \v") | ||
f.Add("\xf0") | ||
|
||
db, err := Open(":memory:") | ||
if err != nil { | ||
f.Fatal(err) | ||
} | ||
defer db.Close() | ||
|
||
f.Fuzz(func(t *testing.T, str string) { | ||
c, err := db.Conn(context.Background()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
c.Raw(func(driverConn any) error { | ||
conn := driverConn.(*conn).Conn | ||
stmt, tail, err := conn.Prepare(str) | ||
stmt.Close() | ||
|
||
// It's hard to be bug for bug compatible with SQLite. | ||
// We settle for somewhat less: | ||
// - if SQLite reports whitespace, we must too | ||
// - if we report whitespace, SQLite must not parse a statement | ||
if notWhitespace(str) { | ||
if stmt == nil && tail == "" && err == nil { | ||
t.Errorf("was whitespace: %q", str) | ||
} | ||
} else { | ||
if stmt != nil { | ||
t.Errorf("was not whitespace: %q (%v)", str, err) | ||
} | ||
} | ||
return nil | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters