Skip to content

Commit

Permalink
Disable double-quoted string literals
Browse files Browse the repository at this point in the history
  • Loading branch information
zombiezen committed Aug 15, 2021
1 parent 2ed604b commit ed99db8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Properly clean up WAL when using `sqlitex.Pool`
([#14](https://github.com/zombiezen/go-sqlite/issues/14))
- Disabled double-quoted string literals.

## [0.5.0][] - 2021-05-22

Expand Down
30 changes: 30 additions & 0 deletions sqlite.go
Expand Up @@ -82,6 +82,36 @@ func OpenConn(path string, flags ...OpenFlags) (*Conn, error) {
return nil, err
}

// Disable double-quoted string literals.
varArgs := libc.Xmalloc(c.tls, ptrSize)
if varArgs == 0 {
c.Close()
return nil, fmt.Errorf("sqlite: open %q: cannot allocate memory", path)
}
defer libc.Xfree(c.tls, varArgs)
res := ResultCode(lib.Xsqlite3_db_config(
c.tls,
c.conn,
lib.SQLITE_DBCONFIG_DQS_DDL,
libc.VaList(varArgs, int32(0), uintptr(0)),
))
if err := reserr(res); err != nil {
// Making error opaque because it's not part of the primary connection
// opening and reflects an internal error.
return nil, fmt.Errorf("sqlite: open %q: disable double-quoted string literals: %v", path, err)
}
res = ResultCode(lib.Xsqlite3_db_config(
c.tls,
c.conn,
lib.SQLITE_DBCONFIG_DQS_DML,
libc.VaList(varArgs, int32(0), uintptr(0)),
))
if err := reserr(res); err != nil {
// Making error opaque because it's not part of the primary connection
// opening and reflects an internal error.
return nil, fmt.Errorf("sqlite: open %q: disable double-quoted string literals: %v", path, err)
}

if openFlags&OpenWAL != 0 {
stmt, _, err := c.PrepareTransient("PRAGMA journal_mode=wal;")
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions sqlite_test.go
Expand Up @@ -123,6 +123,16 @@ func TestConn(t *testing.T) {
if !reflect.DeepEqual(wantInts, gotInts) {
t.Errorf("SELECT foo2: got %v, want %v", gotInts, wantInts)
}

if err := stmt.Finalize(); err != nil {
t.Error(err)
}

stmt, err = c.Prepare(`SELECT "foo" = 'foo';`)
if err == nil {
stmt.Finalize()
t.Error("Double-quoted string literals are permitted")
}
}

func TestEarlyInterrupt(t *testing.T) {
Expand Down

0 comments on commit ed99db8

Please sign in to comment.