Skip to content

Commit 148f1e5

Browse files
committed
Fix an infinite recursion when printing the redacted description of SourceKitLSPOptions
1 parent 72d6a7f commit 148f1e5

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

Sources/SKLogging/CustomLogStringConvertible.swift

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,6 @@ public func recursiveRedactedDescription(of value: Any) -> String {
115115
return value.description
116116
case let value as String:
117117
return value.hashForLogging
118-
case let value as any CustomLogStringConvertible:
119-
return value.redactedDescription
120118
case let value as any OptionalProtocol:
121119
if let value = value.asOptional {
122120
return recursiveRedactedDescription(of: value)
@@ -129,11 +127,17 @@ public func recursiveRedactedDescription(of value: Any) -> String {
129127

130128
let children = Mirror(reflecting: value).children.sorted { $0.label ?? "" < $1.label ?? "" }
131129
if !children.isEmpty {
132-
return "{"
133-
+ children.map { (label, value) -> String in
134-
"\(label ?? "<nil>"): \(recursiveRedactedDescription(of: value))"
135-
}.joined(separator: ", ")
136-
+ "}"
130+
let childrenKeyValuePairs = children.map { (label, value) -> String in
131+
let label = label ?? "<nil>"
132+
let value =
133+
if let value = value as? any CustomLogStringConvertible {
134+
value.redactedDescription
135+
} else {
136+
recursiveRedactedDescription(of: value)
137+
}
138+
return "\(label): \(value)"
139+
}
140+
return "{" + childrenKeyValuePairs.joined(separator: ", ") + "}"
137141
}
138142
return "<private>"
139143
}

Tests/SKLoggingTests/LoggingTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,5 +237,21 @@ final class LoggingTests: XCTestCase {
237237
recursiveRedactedDescription(of: ("abc" as String?) as Any),
238238
"MD5 digest: 900150983cd24fb0d6963f7d28e17f72"
239239
)
240+
241+
struct Something: CustomLogStringConvertible {
242+
let x = 1
243+
let y = "hi"
244+
245+
var redactedDescription: String {
246+
recursiveRedactedDescription(of: self)
247+
}
248+
249+
var description: String { "" }
250+
}
251+
252+
XCTAssertEqual(
253+
Something().redactedDescription,
254+
"{x: 1, y: MD5 digest: 49f68a5c8493ec2c0bf489821c21fc3b}"
255+
)
240256
}
241257
}

0 commit comments

Comments
 (0)