mirror of
				https://github.com/isar/rusqlite.git
				synced 2025-10-31 05:48:56 +08:00 
			
		
		
		
	Do not assume sqlite3_column_text is valid UTF-8
				
					
				
			Fix impact on features
This commit is contained in:
		| @@ -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), | ||||
|         } | ||||
|   | ||||
| @@ -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))) | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -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 => { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user