From 93b3f30262466864197e3030e5067cf258f770cb Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sun, 8 Sep 2024 12:06:52 +0200 Subject: [PATCH] Fix panic in unsafe code when unpacking empty blobs. --- libsql/src/value.rs | 9 +++++---- libsql/tests/integration_tests.rs | 11 +++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/libsql/src/value.rs b/libsql/src/value.rs index fd3719b8ce..7d50265eed 100644 --- a/libsql/src/value.rs +++ b/libsql/src/value.rs @@ -244,10 +244,11 @@ impl From for Value { assert!(len >= 0, "unexpected negative bytes value from sqlite3"); let mut v = Vec::with_capacity(len as usize); - - let slice: &[u8] = - unsafe { std::slice::from_raw_parts(blob as *const u8, len as usize) }; - v.extend_from_slice(slice); + if !blob.is_null() { + let slice: &[u8] = + unsafe { std::slice::from_raw_parts(blob as *const u8, len as usize) }; + v.extend_from_slice(slice); + } Value::Blob(v) } } diff --git a/libsql/tests/integration_tests.rs b/libsql/tests/integration_tests.rs index cdb0a985c3..92d8d358d8 100644 --- a/libsql/tests/integration_tests.rs +++ b/libsql/tests/integration_tests.rs @@ -526,6 +526,17 @@ async fn blob() { let out = row.get::>(1).unwrap(); assert_eq!(&out, &bytes); + + let empty: Vec = vec![]; + let mut rows = conn + .query( + "INSERT INTO bbb (data) VALUES (?1) RETURNING *", + [Value::Blob(empty.clone())], + ) + .await + .unwrap(); + let row = rows.next().await.unwrap().unwrap(); + assert_eq!(row.get::>(1).unwrap(), empty); } #[tokio::test]