Skip to content

Commit 5805ef3

Browse files
committed
Don't allow LF or CR in the middle of TextFormat string literals.
Update the conformance failures list to match. Fixes apple#1085
1 parent fd75e26 commit 5805ef3

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
Required.Proto3.TextFormatInput.StringLiteralIncludesLFBytes
2-
Required.Proto3.TextFormatInput.StringLiteralIncludesLFString
1+
# No known failures

Sources/SwiftProtobuf/TextFormatScanner.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ internal struct TextFormatScanner {
350350
return count
351351
}
352352
switch byte {
353+
case asciiNewLine, asciiCarriageReturn:
354+
// Can't have a newline in the middle of a bytes string.
355+
throw TextFormatDecodingError.malformedText
353356
case asciiBackslash: // "\\"
354357
sawBackslash = true
355358
if p != end {
@@ -576,6 +579,10 @@ internal struct TextFormatScanner {
576579
sawBackslash = true
577580
p += 1
578581
}
582+
if c == asciiNewLine || c == asciiCarriageReturn {
583+
// Can't have a newline in the middle of a raw string.
584+
return nil
585+
}
579586
}
580587
return nil // Unterminated quoted string
581588
}

Tests/LinuxMain.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ extension Test_Conformance {
427427
("testString_surrogates", testString_surrogates),
428428
("testBytes_unicodeEscape", testBytes_unicodeEscape),
429429
("testBytes_surrogates", testBytes_surrogates),
430+
("test_LiteralIncludeLF", test_LiteralIncludeLF),
430431
("testMaps_TextFormatKeysSorted", testMaps_TextFormatKeysSorted)
431432
]
432433
}

Tests/SwiftProtobufTests/Test_Conformance.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ class Test_Conformance: XCTestCase, PBTestHelpers {
187187
assertTextFormatDecodeFails("optional_bytes: \"\\U0000D83D\\uDE01\"")
188188
}
189189

190+
func test_LiteralIncludeLF() {
191+
assertTextFormatDecodeFails("optional_string: 'first line\nsecond line'")
192+
assertTextFormatDecodeFails("optional_string: 'first line\rsecond line'")
193+
assertTextFormatDecodeFails("optional_bytes: 'first line\nsecond line'")
194+
assertTextFormatDecodeFails("optional_bytes: 'first line\rsecond line'")
195+
}
196+
190197
func testMaps_TextFormatKeysSorted() {
191198
assertTextFormatEncode("map_string_string {\n key: \"a\"\n value: \"value\"\n}\nmap_string_string {\n key: \"b\"\n value: \"value\"\n}\nmap_string_string {\n key: \"c\"\n value: \"value\"\n}\n") {(o: inout MessageTestType) in
192199
o.mapStringString = ["c":"value", "b":"value", "a":"value"]

0 commit comments

Comments
 (0)