Skip to content

Commit

Permalink
Merge pull request #1722 from ignatz/fix_empty_blob
Browse files Browse the repository at this point in the history
Fix panic in unsafe code when unpacking empty blobs.
  • Loading branch information
MarinPostma authored Sep 9, 2024
2 parents d9f7a46 + 93b3f30 commit c204f8e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
9 changes: 5 additions & 4 deletions libsql/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,11 @@ impl From<libsql_sys::Value> 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)
}
}
Expand Down
11 changes: 11 additions & 0 deletions libsql/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,17 @@ async fn blob() {

let out = row.get::<Vec<u8>>(1).unwrap();
assert_eq!(&out, &bytes);

let empty: Vec<u8> = 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::<Vec<u8>>(1).unwrap(), empty);
}

#[tokio::test]
Expand Down

0 comments on commit c204f8e

Please sign in to comment.