Skip to content

Commit 79bd7e5

Browse files
authored
rdar://139080443 (Fix incorrect unicode escape sequence encodings) (#1036)
1 parent 31fef58 commit 79bd7e5

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

Sources/FoundationEssentials/JSON/JSONWriter.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ internal struct JSONWriter {
140140
appendAccumulatedBytes(from: mark, to: cursor, followedByContentsOf: [._backslash, UInt8(ascii: "t")])
141141
case 0x0...0xf:
142142
appendAccumulatedBytes(from: mark, to: cursor, followedByContentsOf: [._backslash, UInt8(ascii: "u"), UInt8(ascii: "0"), UInt8(ascii: "0"), UInt8(ascii: "0")])
143-
writer(ascii: valueToASCII(cursor.pointee / 16))
143+
writer(ascii: valueToASCII(cursor.pointee))
144144
case 0x10...0x1f:
145145
appendAccumulatedBytes(from: mark, to: cursor, followedByContentsOf: [._backslash, UInt8(ascii: "u"), UInt8(ascii: "0"), UInt8(ascii: "0")])
146-
writer(ascii: valueToASCII(cursor.pointee % 16))
147146
writer(ascii: valueToASCII(cursor.pointee / 16))
147+
writer(ascii: valueToASCII(cursor.pointee % 16))
148148
default:
149149
// Accumulate this byte
150150
cursor += 1

Tests/FoundationEssentialsTests/JSONEncoderTests.swift

+11-1
Original file line numberDiff line numberDiff line change
@@ -1159,12 +1159,22 @@ final class JSONEncoderTests : XCTestCase {
11591159
"\"\\u00E9\"" : "é",
11601160

11611161
// G-clef (UTF16 surrogate pair) 0x1D11E
1162-
"\"\\uD834\\uDD1E\"" : "𝄞"
1162+
"\"\\uD834\\uDD1E\"" : "𝄞",
11631163
]
11641164
for (input, expectedOutput) in testCases {
11651165
_test(JSONString: input, to: expectedOutput)
11661166
}
11671167
}
1168+
1169+
func test_encodingJSONHexUnicodeEscapes() throws {
1170+
let testCases = [
1171+
"\u{0001}\u{0002}\u{0003}": "\"\\u0001\\u0002\\u0003\"",
1172+
"\u{0010}\u{0018}\u{001f}": "\"\\u0010\\u0018\\u001f\"",
1173+
]
1174+
for (string, json) in testCases {
1175+
_testRoundTrip(of: string, expectedJSON: Data(json.utf8))
1176+
}
1177+
}
11681178

11691179
func test_JSONBadUnicodeEscapes() {
11701180
let badCases = ["\\uD834", "\\uD834hello", "hello\\uD834", "\\uD834\\u1221", "\\uD8", "\\uD834x\\uDD1E"]

0 commit comments

Comments
 (0)