Skip to content

Commit 51d49c5

Browse files
authored
Merge pull request stephencelis#562 from stephencelis/issue-561
Issue 561
2 parents bcc03e9 + dd0aa4b commit 51d49c5

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
========================================
33

44
* Fixed SQLCipher integration with read-only databases ([#559][])
5+
* Fixed null pointer when fetching an empty BLOB ([#561][])
56

67
0.11.1 (06-12-2016), [diff][diff-0.11.1]
78
========================================
@@ -25,3 +26,4 @@
2526
[#546]: https://github.com/stephencelis/SQLite.swift/issues/546
2627
[#553]: https://github.com/stephencelis/SQLite.swift/pull/553
2728
[#559]: https://github.com/stephencelis/SQLite.swift/pull/559
29+
[#561]: https://github.com/stephencelis/SQLite.swift/issues/561

SQLite/Core/Statement.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,9 @@ public struct Cursor {
244244
let length = Int(sqlite3_column_bytes(handle, Int32(idx)))
245245
return Blob(bytes: pointer, length: length)
246246
} else {
247-
fatalError("sqlite3_column_blob returned NULL")
247+
// The return value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
248+
// https://www.sqlite.org/c3ref/column_blob.html
249+
return Blob(bytes: [])
248250
}
249251
}
250252

SQLiteTests/StatementTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,13 @@ class StatementTests : SQLiteTestCase {
1414
let blob = statement.row[0] as Blob
1515
XCTAssertEqual("[email protected]", String(bytes: blob.bytes, encoding: .utf8)!)
1616
}
17+
18+
func test_zero_sized_blob_returns_null() {
19+
let blobs = Table("blobs")
20+
let blobColumn = Expression<Blob>("blob_column")
21+
try! db.run(blobs.create { $0.column(blobColumn) })
22+
try! db.run(blobs.insert(blobColumn <- Blob(bytes: [])))
23+
let blobValue = try! db.scalar(blobs.select(blobColumn).limit(1, offset: 0))
24+
XCTAssertEqual([], blobValue.bytes)
25+
}
1726
}

0 commit comments

Comments
 (0)