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:
Dan Peterson
2021-04-05 20:27:46 -03:00
parent 9ffbf7a0e1
commit e2b915c98c
2 changed files with 17 additions and 0 deletions

View File

@@ -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) {

View File

@@ -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"))
}