Skip to content

Commit 21f8fdb

Browse files
authored
Fix NSNumber() null pointer exception (#28)
* Fix NSNumber() null pointer exception * Update FoundationTests.swift
1 parent 5fdf3d2 commit 21f8fdb

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

Sources/CustomDump/Dump.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,18 @@ public func customDump<T, TargetStream>(
5959

6060
var visitedItems: Set<ObjectIdentifier> = []
6161

62-
func customDumpHelp<TargetStream>(
63-
_ value: Any,
62+
func customDumpHelp<T, TargetStream>(
63+
_ value: T,
6464
to target: inout TargetStream,
6565
name: String?,
6666
indent: Int,
6767
maxDepth: Int
6868
) where TargetStream: TextOutputStream {
69+
if T.self is AnyObject.Type, withUnsafeBytes(of: value, { $0.allSatisfy { $0 == 0 } }) {
70+
target.write((name.map { "\($0): " } ?? "").appending("(null pointer)").indenting(by: indent))
71+
return
72+
}
73+
6974
let mirror = Mirror(customDumpReflecting: value)
7075
var out = ""
7176

Tests/CustomDumpTests/Conformances/FoundationTests.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ final class FoundationTests: XCTestCase {
432432
func testNSNumber() {
433433
var dump = ""
434434
customDump(
435-
NSNumber(booleanLiteral: true),
435+
1 as NSNumber,
436436
to: &dump
437437
)
438438
XCTAssertNoDifference(
@@ -441,6 +441,20 @@ final class FoundationTests: XCTestCase {
441441
1
442442
"""
443443
)
444+
445+
#if canImport(ObjectiveC)
446+
dump = ""
447+
customDump(
448+
NSNumber(),
449+
to: &dump
450+
)
451+
XCTAssertNoDifference(
452+
dump,
453+
"""
454+
(null pointer)
455+
"""
456+
)
457+
#endif
444458
}
445459

446460
func testNSOrderedSet() {

0 commit comments

Comments
 (0)