Skip to content

Commit 401229c

Browse files
committed
[JSON] Fix JSONMapValue.equals(to:)
In some platforms memcmp don't accept optional value
1 parent 488c434 commit 401229c

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

Sources/SwiftCompilerPluginMessageHandling/JSON/JSONDecoding.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -591,10 +591,17 @@ extension JSONMapValue {
591591
/// instantiate 'Swift.String' unless there are escaped characters.
592592
func equals(to str: String) -> Bool {
593593
if self.is(.simpleString) {
594-
let buffer = valueBuffer()
594+
let lhs = valueBuffer()
595595
var str = str
596-
return str.withUTF8 { utf8 in
597-
utf8.count == buffer.count && memcmp(utf8.baseAddress, buffer.baseAddress, utf8.count) == 0
596+
return str.withUTF8 { rhs in
597+
if lhs.count != rhs.count {
598+
return false
599+
}
600+
guard let lBase = lhs.baseAddress, let rBase = rhs.baseAddress else {
601+
// If either `baseAddress` is `nil`, both are empty so returns `true`.
602+
return true
603+
}
604+
return memcmp(lBase, rBase, lhs.count) == 0
598605
}
599606
}
600607
return self.asString() == str

0 commit comments

Comments
 (0)