Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NSNumber() null pointer exception #28

Merged
merged 2 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 } }) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if there's a better check than this 😕

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