From bbecb3eeeb122074bc23eee2ac11c8fa0657e08b Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 15 May 2026 10:07:37 +0300 Subject: [PATCH] db.sqlite: pass initialized C strings to sqlite --- vlib/db/sqlite/sqlite.c.v | 17 ++++++++++++++++- vlib/db/sqlite/vfs_lowlevel.c.v | 13 +++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/vlib/db/sqlite/sqlite.c.v b/vlib/db/sqlite/sqlite.c.v index 35483f19f..3712da811 100644 --- a/vlib/db/sqlite/sqlite.c.v +++ b/vlib/db/sqlite/sqlite.c.v @@ -132,6 +132,17 @@ pub fn (r &Row) get_int(col_name string) int { pub type Params = []string | [][]string +fn sqlite_cstring(s string) &char { + cstr := unsafe { malloc_noscan(s.len + 1) } + unsafe { + if s.len > 0 { + vmemcpy(cstr, s.str, s.len) + } + cstr[s.len] = 0 + } + return &char(cstr) +} + // fn C.sqlite3_open(&char, &&C.sqlite3) i32 @@ -179,7 +190,11 @@ fn C.sqlite3_changes(&C.sqlite3) i32 // connect Opens the connection with a database. pub fn connect(path string) !DB { db := &C.sqlite3(unsafe { nil }) - code := C.sqlite3_open(&char(path.str), &db) + path_cstr := sqlite_cstring(path) + defer { + unsafe { free(path_cstr) } + } + code := C.sqlite3_open(path_cstr, &db) if code != 0 { return &SQLError{ msg: unsafe { cstring_to_vstring(&char(C.sqlite3_errmsg(db))) } diff --git a/vlib/db/sqlite/vfs_lowlevel.c.v b/vlib/db/sqlite/vfs_lowlevel.c.v index 9386d6009..3b5820967 100644 --- a/vlib/db/sqlite/vfs_lowlevel.c.v +++ b/vlib/db/sqlite/vfs_lowlevel.c.v @@ -200,6 +200,10 @@ pub enum OpenModeFlag { // (which is equivalent to 'win32' on Windows, and 'unix' on !Windows systems). pub fn connect_full(path string, mode_flags []OpenModeFlag, vfs_name string) !DB { db := &C.sqlite3(unsafe { nil }) + path_cstr := sqlite_cstring(path) + defer { + unsafe { free(path_cstr) } + } mut flags := 0 @@ -209,9 +213,14 @@ pub fn connect_full(path string, mode_flags []OpenModeFlag, vfs_name string) !DB mut pstr := unsafe { &char(0) } if vfs_name != '' { - pstr = vfs_name.str + pstr = sqlite_cstring(vfs_name) + } + defer { + if vfs_name != '' { + unsafe { free(pstr) } + } } - code := C.sqlite3_open_v2(&char(path.str), &db, flags, pstr) + code := C.sqlite3_open_v2(path_cstr, &db, flags, pstr) if code != 0 { return &SQLError{ msg: unsafe { cstring_to_vstring(&char(C.sqlite3_errstr(code))) } -- 2.39.5