Skip to content

Commit

Permalink
Fix NSNumber() null pointer exception (#28)
Browse files Browse the repository at this point in the history
* Fix NSNumber() null pointer exception

* Update FoundationTests.swift
  • Loading branch information
stephencelis authored Oct 13, 2021
1 parent 5fdf3d2 commit 21f8fdb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
9 changes: 7 additions & 2 deletions Sources/CustomDump/Dump.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,18 @@ public func customDump<T, TargetStream>(

var visitedItems: Set<ObjectIdentifier> = []

func customDumpHelp<TargetStream>(
_ value: Any,
func customDumpHelp<T, TargetStream>(
_ value: T,
to target: inout TargetStream,
name: String?,
indent: Int,
maxDepth: Int
) where TargetStream: TextOutputStream {
if T.self is AnyObject.Type, withUnsafeBytes(of: value, { $0.allSatisfy { $0 == 0 } }) {
target.write((name.map { "\($0): " } ?? "").appending("(null pointer)").indenting(by: indent))
return
}

let mirror = Mirror(customDumpReflecting: value)
var out = ""

Expand Down
16 changes: 15 additions & 1 deletion Tests/CustomDumpTests/Conformances/FoundationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ final class FoundationTests: XCTestCase {
func testNSNumber() {
var dump = ""
customDump(
NSNumber(booleanLiteral: true),
1 as NSNumber,
to: &dump
)
XCTAssertNoDifference(
Expand All @@ -441,6 +441,20 @@ final class FoundationTests: XCTestCase {
1
"""
)

#if canImport(ObjectiveC)
dump = ""
customDump(
NSNumber(),
to: &dump
)
XCTAssertNoDifference(
dump,
"""
(null pointer)
"""
)
#endif
}

func testNSOrderedSet() {
Expand Down

0 comments on commit 21f8fdb

Please sign in to comment.