Skip to content

Commit bf2b3c9

Browse files
committed
Check the rest once and scan block comments in linear time
SQLite skips leading empty statements itself and stops after the first statement, so one check of the rest after the first prepare is enough; prepare no longer prepares the rest after an empty statement. The block comment scan searched the whole rest of the string for a NUL at every comment, which is quadratic in the number of comments. It now searches the comment only. The README no longer says nothing of a refused string runs: a first statement that acts when it is prepared still does. The tests check that Query and Prepare refuse the string themselves, and cover a line comment followed by a second statement, CR in a line comment, a semicolon in a string literal and a NUL.
1 parent c908a51 commit bf2b3c9

4 files changed

Lines changed: 19 additions & 25 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Without a current time, a column declared `NOT NULL DEFAULT CURRENT_TIMESTAMP` c
3232
- `sqlite3.go` defines the SQLCipher compile options, turns off the default logging of SQLCipher to stderr, and adds the `_crypto_key` DSN parameter.
3333
- `Exec` without arguments reports missing arguments again, which go-sqlite3 v1.14.52 does not.
3434
- `sqlite3_nocurrenttime.go` adds the `nocurrenttime` VFS, and `sqlite3_opt_omit_localtime.go` the `sqlite_omit_localtime` tag.
35-
- `SQLiteDriver.SingleStatement` refuses a query string that holds more than one statement with `ErrMultipleStatements`, before any of it runs.
35+
- `SQLiteDriver.SingleStatement` refuses a query string that holds more than one statement with `ErrMultipleStatements`, without stepping it or preparing its second statement.
3636
- `sqlite3_opt_icu.go` adds the libraries that a static link of ICU needs.
3737
- The CIFuzz workflow, which builds the OSS-Fuzz project of go-sqlite3, is removed.
3838

‎sqlite3.go‎

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2216,25 +2216,15 @@ func (c *SQLiteConn) prepare(ctx context.Context, query string) (driver.Stmt, er
22162216
}
22172217
var t string
22182218
if c.singleStatement {
2219-
// Check the rest of the string as SQLite reads it, and never prepare
2220-
// it once a statement has been prepared.
2221-
for {
2222-
var rest string
2223-
if tail != nil {
2224-
rest = query[uintptr(unsafe.Pointer(tail))-uintptr(unsafe.Pointer(pquery)):]
2225-
}
2226-
if onlySQLiteTrivia(rest) {
2227-
break
2228-
}
2229-
if s != nil {
2230-
C.sqlite3_finalize(s)
2231-
return nil, ErrMultipleStatements
2232-
}
2233-
// Only empty statements so far: the rest starts with the first one.
2234-
rv = C._sqlite3_prepare_v2_internal(c.db, tail, C.int(len(rest)), &s, &tail)
2235-
if rv != C.SQLITE_OK {
2236-
return nil, c.lastError()
2237-
}
2219+
// SQLite skips leading empty statements and stops after the first
2220+
// statement. Check the rest as SQLite reads it, without preparing it.
2221+
var rest string
2222+
if tail != nil {
2223+
rest = query[uintptr(unsafe.Pointer(tail))-uintptr(unsafe.Pointer(pquery)):]
2224+
}
2225+
if !onlySQLiteTrivia(rest) {
2226+
C.sqlite3_finalize(s)
2227+
return nil, ErrMultipleStatements
22382228
}
22392229
} else if tail != nil && *tail != '\000' {
22402230
t = strings.TrimSpace(C.GoString(tail))

‎sqlite3_single_statement.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func onlySQLiteTrivia(s string) bool {
3737
// Up to the first "*/" or the end; comments do not nest. A "/*"
3838
// at the very end is a slash and a star, not a comment.
3939
j := strings.Index(s[i+2:], "*/")
40-
if n := strings.IndexByte(s[i+2:], 0); j < 0 || n >= 0 && n < j {
40+
if j < 0 || strings.IndexByte(s[i+2:i+2+j], 0) >= 0 {
4141
return true
4242
}
4343
i += 2 + j + 2

‎sqlite3_single_statement_test.go‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,24 @@ func singleStatementCalls(db *sql.DB, query string, args ...any) map[string]erro
4646
"QueryContext": func() (*sql.Rows, error) { return db.QueryContext(ctx, query, args...) },
4747
} {
4848
rows, err := run()
49+
errs[name] = err
4950
if err == nil {
5051
for rows.Next() {
5152
}
52-
err = rows.Err()
53+
errs[name+" rows"] = rows.Err()
5354
rows.Close()
5455
}
55-
errs[name] = err
5656
}
5757
for name, prepare := range map[string]func() (*sql.Stmt, error){
5858
"Prepare": func() (*sql.Stmt, error) { return db.Prepare(query) },
5959
"PrepareContext": func() (*sql.Stmt, error) { return db.PrepareContext(ctx, query) },
6060
} {
6161
stmt, err := prepare()
62+
errs[name] = err
6263
if err == nil {
63-
_, err = stmt.Exec(args...)
64+
_, errs[name+" Exec"] = stmt.Exec(args...)
6465
stmt.Close()
6566
}
66-
errs[name] = err
6767
}
6868
tx, err := db.BeginTx(ctx, nil)
6969
if err == nil {
@@ -97,6 +97,7 @@ func TestSingleStatement(t *testing.T) {
9797
{"INSERT INTO r VALUES (?); INSERT INTO r VALUES (?)", []any{1, 2}},
9898
{"INSERT INTO r VALUES (1); x", nil},
9999
{"INSERT INTO r VALUES (1); /* outer /* inner */ SELECT 2; -- */", nil},
100+
{"INSERT INTO r VALUES (1); --x\nSELECT 2", nil},
100101
{"INSERT INTO r VALUES (1); /*", nil},
101102
{"INSERT INTO r VALUES (1);\v", nil},
102103
{"CREATE TABLE d (v); SELECT 1", nil},
@@ -124,6 +125,9 @@ func TestSingleStatement(t *testing.T) {
124125
"INSERT INTO r VALUES (?); /*x",
125126
"INSERT INTO r VALUES (?); \v",
126127
"INSERT INTO r VALUES (?);\xef\xbb\xbf",
128+
"INSERT INTO r VALUES (?); --x\rSELECT 2",
129+
"INSERT INTO r VALUES (? || '; SELECT 2');",
130+
"INSERT INTO r VALUES (?);\x00SELECT 2",
127131
} {
128132
before := count()
129133
for call, err := range singleStatementCalls(db, query, 1) {

0 commit comments

Comments
 (0)