From 16175c13894d954d85d3d5d271c2677289a788b9 Mon Sep 17 00:00:00 2001 From: Catena cyber <35799796+catenacyber@users.noreply.github.com> Date: Mon, 15 Feb 2021 14:57:26 +0100 Subject: [PATCH] Adds a fuzz target (#908) * Adds a fuzz target * Fixes memory leak --- _example/fuzz/fuzz_openexec.go | 30 ++++++++++++++++++++++++++++++ sqlite3.go | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 _example/fuzz/fuzz_openexec.go diff --git a/_example/fuzz/fuzz_openexec.go b/_example/fuzz/fuzz_openexec.go new file mode 100644 index 0000000..5326044 --- /dev/null +++ b/_example/fuzz/fuzz_openexec.go @@ -0,0 +1,30 @@ +package sqlite3_fuzz + +import ( + "bytes" + "database/sql" + "io/ioutil" + + _ "github.com/mattn/go-sqlite3" +) + +func FuzzOpenExec(data []byte) int { + sep := bytes.IndexByte(data, 0) + if sep <= 0 { + return 0 + } + err := ioutil.WriteFile("/tmp/fuzz.db", data[sep+1:], 0644) + if err != nil { + return 0 + } + db, err := sql.Open("sqlite3", "/tmp/fuzz.db") + if err != nil { + return 0 + } + defer db.Close() + _, err = db.Exec(string(data[:sep-1])) + if err != nil { + return 0 + } + return 1 +} diff --git a/sqlite3.go b/sqlite3.go index 552a2ab..5ac9570 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -1676,7 +1676,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) { // // Because default is NORMAL this statement is always executed if err := exec(fmt.Sprintf("PRAGMA synchronous = %s;", synchronousMode)); err != nil { - C.sqlite3_close_v2(db) + conn.Close() return nil, err }