Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check sqlite3_reset result #1419

Merged
merged 3 commits into from Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/lib.rs
Expand Up @@ -1494,7 +1494,7 @@ mod test {

#[test]
#[cfg(feature = "extra_check")]
fn test_execute_select() {
fn test_execute_select_with_no_row() {
let db = checked_memory_handle();
let err = db.execute("SELECT 1 WHERE 1 < ?1", [1i32]).unwrap_err();
assert_eq!(
Expand All @@ -1504,6 +1504,13 @@ mod test {
);
}

#[test]
fn test_execute_select_with_row() {
let db = checked_memory_handle();
let err = db.execute("SELECT 1", []).unwrap_err();
assert_eq!(err, Error::ExecuteReturnedResults);
}

#[test]
#[cfg(feature = "extra_check")]
fn test_execute_multiple() {
Expand Down
13 changes: 8 additions & 5 deletions src/row.rs
Expand Up @@ -14,9 +14,11 @@ pub struct Rows<'stmt> {

impl<'stmt> Rows<'stmt> {
#[inline]
fn reset(&mut self) {
fn reset(&mut self) -> Result<()> {
if let Some(stmt) = self.stmt.take() {
stmt.reset();
stmt.reset()
} else {
Ok(())
}
}

Expand Down Expand Up @@ -105,6 +107,7 @@ impl<'stmt> Rows<'stmt> {
}

impl Drop for Rows<'_> {
#[allow(unused_must_use)]
#[inline]
fn drop(&mut self) {
self.reset();
Expand Down Expand Up @@ -217,12 +220,12 @@ impl<'stmt> FallibleStreamingIterator for Rows<'stmt> {
Ok(())
}
Ok(false) => {
self.reset();
let r = self.reset();
self.row = None;
Ok(())
r
}
Err(e) => {
self.reset();
let _ = self.reset(); // prevents infinite loop on error
self.row = None;
Err(e)
}
Expand Down
16 changes: 11 additions & 5 deletions src/statement.rs
Expand Up @@ -650,9 +650,12 @@
fn execute_with_bound_parameters(&mut self) -> Result<usize> {
self.check_update()?;
let r = self.stmt.step();
self.stmt.reset();
let rr = self.stmt.reset();
match r {
ffi::SQLITE_DONE => Ok(self.conn.changes() as usize),
ffi::SQLITE_DONE => match rr {
ffi::SQLITE_OK => Ok(self.conn.changes() as usize),
_ => Err(self.conn.decode_result(rr).unwrap_err()),

Check warning on line 657 in src/statement.rs

View check run for this annotation

Codecov / codecov/patch

src/statement.rs#L657

Added line #L657 was not covered by tests
},
ffi::SQLITE_ROW => Err(Error::ExecuteReturnedResults),
_ => Err(self.conn.decode_result(r).unwrap_err()),
}
Expand Down Expand Up @@ -847,8 +850,11 @@
}

#[inline]
pub(super) fn reset(&self) -> c_int {
self.stmt.reset()
pub(super) fn reset(&self) -> Result<()> {
match self.stmt.reset() {
ffi::SQLITE_OK => Ok(()),
code => Err(self.conn.decode_result(code).unwrap_err()),
}
}
}

Expand Down Expand Up @@ -1274,7 +1280,7 @@
assert_eq!(0, stmt.column_count());
stmt.parameter_index("test").unwrap();
stmt.step().unwrap_err();
stmt.reset();
stmt.reset().unwrap(); // SQLITE_OMIT_AUTORESET = false
stmt.execute([]).unwrap_err();
Ok(())
}
Expand Down