Do not assume sqlite3_column_text is valid UTF-8

Fix impact on features
This commit is contained in:
gwenn 2019-07-24 20:46:53 +02:00
parent f0ae7b6e9b
commit 701389605d
4 changed files with 11 additions and 13 deletions

View File

@ -86,7 +86,7 @@ impl Sql {
self.push_real(r);
}
ValueRef::Text(s) => {
let s = std::str::from_utf8(s).expect("invalid UTF-8");
let s = std::str::from_utf8(s)?;
self.push_string_literal(s);
}
_ => {

View File

@ -17,7 +17,7 @@ impl ToSql for Value {
impl FromSql for Value {
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
match value {
ValueRef::Text(s) => serde_json::from_str(s),
ValueRef::Text(s) => serde_json::from_slice(s),
ValueRef::Blob(b) => serde_json::from_slice(b),
_ => return Err(FromSqlError::InvalidType),
}

View File

@ -14,10 +14,12 @@ impl ToSql for Url {
impl FromSql for Url {
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
match value {
ValueRef::Text(s) => Url::parse(s),
_ => return Err(FromSqlError::InvalidType),
ValueRef::Text(s) => {
let s = std::str::from_utf8(s).map_err(|e| FromSqlError::Other(Box::new(e)))?;
Url::parse(s).map_err(|e| FromSqlError::Other(Box::new(e)))
}
_ => Err(FromSqlError::InvalidType),
}
.map_err(|err| FromSqlError::Other(Box::new(err)))
}
}

View File

@ -54,10 +54,9 @@ impl<'a> ValueRef<'a> {
/// `Err(Error::InvalidColumnType)`.
pub fn as_str(&self) -> FromSqlResult<&'a str> {
match *self {
ValueRef::Text(t) => match std::str::from_utf8(t) {
Err(e) => Err(FromSqlError::Other(Box::new(e))),
Ok(r) => Ok(r),
},
ValueRef::Text(t) => {
std::str::from_utf8(t).map_err(|e| FromSqlError::Other(Box::new(e)))
}
_ => Err(FromSqlError::InvalidType),
}
}
@ -131,10 +130,7 @@ impl<'a> ValueRef<'a> {
);
let s = CStr::from_ptr(text as *const c_char);
// sqlite3_value_text returns UTF8 data, so our unwrap here should be fine.
let s = s
.to_str()
.expect("sqlite3_value_text returned invalid UTF-8");
let s = s.to_bytes();
ValueRef::Text(s)
}
ffi::SQLITE_BLOB => {