From 21f8fdbb3226e5e28a1a2fffac3e0f3deec34bf0 Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Wed, 13 Oct 2021 12:53:26 -0400 Subject: [PATCH] Fix NSNumber() null pointer exception (#28) * Fix NSNumber() null pointer exception * Update FoundationTests.swift --- Sources/CustomDump/Dump.swift | 9 +++++++-- .../Conformances/FoundationTests.swift | 16 +++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Sources/CustomDump/Dump.swift b/Sources/CustomDump/Dump.swift index 1041c22..bd9b7fc 100644 --- a/Sources/CustomDump/Dump.swift +++ b/Sources/CustomDump/Dump.swift @@ -59,13 +59,18 @@ public func customDump( var visitedItems: Set = [] - func customDumpHelp( - _ value: Any, + func customDumpHelp( + _ 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 = "" diff --git a/Tests/CustomDumpTests/Conformances/FoundationTests.swift b/Tests/CustomDumpTests/Conformances/FoundationTests.swift index 46ac4b6..1499cce 100644 --- a/Tests/CustomDumpTests/Conformances/FoundationTests.swift +++ b/Tests/CustomDumpTests/Conformances/FoundationTests.swift @@ -432,7 +432,7 @@ final class FoundationTests: XCTestCase { func testNSNumber() { var dump = "" customDump( - NSNumber(booleanLiteral: true), + 1 as NSNumber, to: &dump ) XCTAssertNoDifference( @@ -441,6 +441,20 @@ final class FoundationTests: XCTestCase { 1 """ ) + + #if canImport(ObjectiveC) + dump = "" + customDump( + NSNumber(), + to: &dump + ) + XCTAssertNoDifference( + dump, + """ + (null pointer) + """ + ) + #endif } func testNSOrderedSet() {