Skip to content

Commit c204f8e

Browse files
authored
Merge pull request #1722 from ignatz/fix_empty_blob
Fix panic in unsafe code when unpacking empty blobs.
2 parents d9f7a46 + 93b3f30 commit c204f8e

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

libsql/src/value.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,11 @@ impl From<libsql_sys::Value> for Value {
244244
assert!(len >= 0, "unexpected negative bytes value from sqlite3");
245245

246246
let mut v = Vec::with_capacity(len as usize);
247-
248-
let slice: &[u8] =
249-
unsafe { std::slice::from_raw_parts(blob as *const u8, len as usize) };
250-
v.extend_from_slice(slice);
247+
if !blob.is_null() {
248+
let slice: &[u8] =
249+
unsafe { std::slice::from_raw_parts(blob as *const u8, len as usize) };
250+
v.extend_from_slice(slice);
251+
}
251252
Value::Blob(v)
252253
}
253254
}

libsql/tests/integration_tests.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,17 @@ async fn blob() {
526526

527527
let out = row.get::<Vec<u8>>(1).unwrap();
528528
assert_eq!(&out, &bytes);
529+
530+
let empty: Vec<u8> = vec![];
531+
let mut rows = conn
532+
.query(
533+
"INSERT INTO bbb (data) VALUES (?1) RETURNING *",
534+
[Value::Blob(empty.clone())],
535+
)
536+
.await
537+
.unwrap();
538+
let row = rows.next().await.unwrap().unwrap();
539+
assert_eq!(row.get::<Vec<u8>>(1).unwrap(), empty);
529540
}
530541

531542
#[tokio::test]

0 commit comments

Comments
 (0)