mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-22 16:29:20 +08:00
Do not assume sqlite3_column_text
is valid UTF-8
Fix impact on features
This commit is contained in:
parent
f0ae7b6e9b
commit
701389605d
@ -86,7 +86,7 @@ impl Sql {
|
|||||||
self.push_real(r);
|
self.push_real(r);
|
||||||
}
|
}
|
||||||
ValueRef::Text(s) => {
|
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);
|
self.push_string_literal(s);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
@ -17,7 +17,7 @@ impl ToSql for Value {
|
|||||||
impl FromSql for Value {
|
impl FromSql for Value {
|
||||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||||
match value {
|
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),
|
ValueRef::Blob(b) => serde_json::from_slice(b),
|
||||||
_ => return Err(FromSqlError::InvalidType),
|
_ => return Err(FromSqlError::InvalidType),
|
||||||
}
|
}
|
||||||
|
@ -14,10 +14,12 @@ impl ToSql for Url {
|
|||||||
impl FromSql for Url {
|
impl FromSql for Url {
|
||||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||||
match value {
|
match value {
|
||||||
ValueRef::Text(s) => Url::parse(s),
|
ValueRef::Text(s) => {
|
||||||
_ => return Err(FromSqlError::InvalidType),
|
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)))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,10 +54,9 @@ impl<'a> ValueRef<'a> {
|
|||||||
/// `Err(Error::InvalidColumnType)`.
|
/// `Err(Error::InvalidColumnType)`.
|
||||||
pub fn as_str(&self) -> FromSqlResult<&'a str> {
|
pub fn as_str(&self) -> FromSqlResult<&'a str> {
|
||||||
match *self {
|
match *self {
|
||||||
ValueRef::Text(t) => match std::str::from_utf8(t) {
|
ValueRef::Text(t) => {
|
||||||
Err(e) => Err(FromSqlError::Other(Box::new(e))),
|
std::str::from_utf8(t).map_err(|e| FromSqlError::Other(Box::new(e)))
|
||||||
Ok(r) => Ok(r),
|
}
|
||||||
},
|
|
||||||
_ => Err(FromSqlError::InvalidType),
|
_ => Err(FromSqlError::InvalidType),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -131,10 +130,7 @@ impl<'a> ValueRef<'a> {
|
|||||||
);
|
);
|
||||||
let s = CStr::from_ptr(text as *const c_char);
|
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_bytes();
|
||||||
let s = s
|
|
||||||
.to_str()
|
|
||||||
.expect("sqlite3_value_text returned invalid UTF-8");
|
|
||||||
ValueRef::Text(s)
|
ValueRef::Text(s)
|
||||||
}
|
}
|
||||||
ffi::SQLITE_BLOB => {
|
ffi::SQLITE_BLOB => {
|
||||||
|
Loading…
Reference in New Issue
Block a user