From f78ac1f2cf1393f35ba35de7bca7f9796d292113 Mon Sep 17 00:00:00 2001 From: gwenn Date: Mon, 22 Jul 2019 21:07:53 +0200 Subject: [PATCH] Do not assume `sqlite3_column_text` is valid UTF-8. Fix Statement::value_ref --- src/row.rs | 4 ++-- src/statement.rs | 9 ++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/row.rs b/src/row.rs index 9847a76..67c7c87 100644 --- a/src/row.rs +++ b/src/row.rs @@ -221,7 +221,7 @@ impl<'stmt> Row<'stmt> { /// 16 bytes, `Error::InvalidColumnType` will also be returned. pub fn get(&self, idx: I) -> Result { let idx = idx.idx(self.stmt)?; - let value = self.stmt.value_ref(idx); + let value = self.stmt.value_ref(idx)?; FromSql::column_result(value).map_err(|err| match err { FromSqlError::InvalidType => { Error::InvalidColumnType(idx, self.stmt.column_name(idx).into(), value.data_type()) @@ -262,7 +262,7 @@ impl<'stmt> Row<'stmt> { // returns) to `ValueRef<'a>` is needed because it's only valid until // the next call to sqlite3_step. let val_ref = self.stmt.value_ref(idx); - Ok(val_ref) + val_ref } /// Get the value of a particular column of the result row as a `ValueRef`, diff --git a/src/statement.rs b/src/statement.rs index d3be4c4..19837f0 100644 --- a/src/statement.rs +++ b/src/statement.rs @@ -601,10 +601,10 @@ impl Statement<'_> { Statement { conn, stmt } } - pub(crate) fn value_ref(&self, col: usize) -> ValueRef<'_> { + pub(crate) fn value_ref(&self, col: usize) -> Result> { let raw = unsafe { self.stmt.ptr() }; - match self.stmt.column_type(col) { + Ok(match self.stmt.column_type(col) { ffi::SQLITE_NULL => ValueRef::Null, ffi::SQLITE_INTEGER => { ValueRef::Integer(unsafe { ffi::sqlite3_column_int64(raw, col as c_int) }) @@ -624,8 +624,7 @@ impl Statement<'_> { // sqlite3_column_text returns UTF8 data, so our unwrap here should be fine. let s = s - .to_str() - .expect("sqlite3_column_text returned invalid UTF-8"); + .to_str()?; ValueRef::Text(s) } ffi::SQLITE_BLOB => { @@ -653,7 +652,7 @@ impl Statement<'_> { } } _ => unreachable!("sqlite3_column_type returned invalid value"), - } + }) } pub(crate) fn step(&self) -> Result {