mirror of
https://github.com/glebarez/go-sqlite.git
synced 2025-10-04 23:42:40 +08:00
driver: set libc environment in init
modernc.org/libc.Start does this when wrapping funcs main to seed data for libc.Xgetenv and friends. However, sqlite doesn't use libc.Start. It sets libc bits up in an init func. This leaves the libc view of the enivorment empty/null. When the sqlite "localtime" modifier used with datetime/strftime/etc, sqlite eventually calls libc.Xlocaltime which wants to read TZ from the environment. With an empty/null libc enivornment, this segfaults. To fix that, call libc.SetEnviron in func init like libc.Start does. Fixes https://gitlab.com/cznic/sqlite/-/issues/49
This commit is contained in:
13
all_test.go
13
all_test.go
@@ -1178,6 +1178,19 @@ func TestTimeScan(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// https://gitlab.com/cznic/sqlite/-/issues/49
|
||||
func TestTimeLocaltime(t *testing.T) {
|
||||
db, err := sql.Open(driverName, "file::memory:")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
if _, err := db.Exec("select datetime('now', 'localtime')"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// https://sqlite.org/lang_expr.html#varparam
|
||||
// https://gitlab.com/cznic/sqlite/-/issues/42
|
||||
func TestBinding(t *testing.T) {
|
||||
|
@@ -14,6 +14,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"os"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strconv"
|
||||
@@ -119,6 +120,9 @@ var (
|
||||
|
||||
func init() {
|
||||
tls := libc.NewTLS()
|
||||
|
||||
libc.SetEnviron(tls, os.Environ())
|
||||
|
||||
if sqlite3.Xsqlite3_threadsafe(tls) == 0 {
|
||||
panic(fmt.Errorf("sqlite: thread safety configuration error"))
|
||||
}
|
||||
|
Reference in New Issue
Block a user