mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-23 00:39:20 +08:00
Memory leak when using Statement.expanded_sql (#553)
Memory leak when using Statement.expanded_sql
This commit is contained in:
parent
d87a1bbf7e
commit
4db226c0df
@ -114,15 +114,14 @@ impl RawStatement {
|
|||||||
unsafe { ffi::sqlite3_stmt_readonly(self.0) != 0 }
|
unsafe { ffi::sqlite3_stmt_readonly(self.0) != 0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `CStr` must be freed
|
||||||
#[cfg(feature = "bundled")]
|
#[cfg(feature = "bundled")]
|
||||||
pub fn expanded_sql(&self) -> Option<&CStr> {
|
pub unsafe fn expanded_sql(&self) -> Option<&CStr> {
|
||||||
unsafe {
|
let ptr = ffi::sqlite3_expanded_sql(self.0);
|
||||||
let ptr = ffi::sqlite3_expanded_sql(self.0);
|
if ptr.is_null() {
|
||||||
if ptr.is_null() {
|
None
|
||||||
None
|
} else {
|
||||||
} else {
|
Some(CStr::from_ptr(ptr))
|
||||||
Some(CStr::from_ptr(ptr))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -550,11 +550,16 @@ impl Statement<'_> {
|
|||||||
/// Returns a string containing the SQL text of prepared statement with
|
/// Returns a string containing the SQL text of prepared statement with
|
||||||
/// bound parameters expanded.
|
/// bound parameters expanded.
|
||||||
#[cfg(feature = "bundled")]
|
#[cfg(feature = "bundled")]
|
||||||
pub fn expanded_sql(&self) -> Option<&str> {
|
pub fn expanded_sql(&self) -> Option<String> {
|
||||||
unsafe {
|
unsafe {
|
||||||
self.stmt
|
match self.stmt.expanded_sql() {
|
||||||
.expanded_sql()
|
Some(s) => {
|
||||||
.map(|s| str::from_utf8_unchecked(s.to_bytes()))
|
let sql = str::from_utf8_unchecked(s.to_bytes()).to_owned();
|
||||||
|
ffi::sqlite3_free(s.as_ptr() as *mut _);
|
||||||
|
Some(sql)
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -975,7 +980,7 @@ mod test {
|
|||||||
let db = Connection::open_in_memory().unwrap();
|
let db = Connection::open_in_memory().unwrap();
|
||||||
let stmt = db.prepare("SELECT ?").unwrap();
|
let stmt = db.prepare("SELECT ?").unwrap();
|
||||||
stmt.bind_parameter(&1, 1).unwrap();
|
stmt.bind_parameter(&1, 1).unwrap();
|
||||||
assert_eq!(Some("SELECT 1"), stmt.expanded_sql());
|
assert_eq!(Some("SELECT 1".to_owned()), stmt.expanded_sql());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
Reference in New Issue
Block a user