From 95050f10a848cd6861047715f65cf3c421024043 Mon Sep 17 00:00:00 2001 From: gwenn Date: Mon, 13 Jun 2016 20:22:21 +0200 Subject: [PATCH 1/2] Add test with empty blob (issue #174). --- src/types/mod.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/types/mod.rs b/src/types/mod.rs index 758eb5d..61deaf6 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -129,6 +129,17 @@ mod test { assert_eq!(v, v1234); } + #[test] + fn test_empty_blob() { + let db = checked_memory_handle(); + + let empty = vec![]; + db.execute("INSERT INTO foo(b) VALUES (?)", &[&empty]).unwrap(); + + let v: Vec = db.query_row("SELECT b FROM foo", &[], |r| r.get(0)).unwrap(); + assert_eq!(v, empty); + } + #[test] fn test_str() { let db = checked_memory_handle(); From 9f05147660096c9d68a4c60cfc88bba6861dd015 Mon Sep 17 00:00:00 2001 From: gwenn Date: Mon, 13 Jun 2016 20:32:39 +0200 Subject: [PATCH 2/2] Fix issue jgallagher/rusqlite#174 --- src/lib.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c571704..50cf7e9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1123,12 +1123,17 @@ impl<'a> ValueRef<'a> { } ffi::SQLITE_BLOB => { let blob = ffi::sqlite3_column_blob(raw, col); - assert!(!blob.is_null(), "unexpected SQLITE_BLOB column type with NULL data"); let len = ffi::sqlite3_column_bytes(raw, col); assert!(len >= 0, "unexpected negative return from sqlite3_column_bytes"); + if len > 0 { + assert!(!blob.is_null(), "unexpected SQLITE_BLOB column type with NULL data"); + ValueRef::Blob(from_raw_parts(blob as *const u8, len as usize)) + } else { + // The return value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer. + ValueRef::Blob(&[]) + } - ValueRef::Blob(from_raw_parts(blob as *const u8, len as usize)) } _ => unreachable!("sqlite3_column_type returned invalid value") }