Skip to content

Commit cb72454

Browse files
committed
Pass length of source file to C parsing actions
Previously, the string was implicitly terminated by the first null character. But a source file might contain a null character which shouldn't terminate it.
1 parent af58d21 commit cb72454

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

Sources/SwiftSyntax/SyntaxParser.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,11 @@ public enum SyntaxParser {
192192
swiftparse_parser_set_diagnostic_handler(c_parser, diagHandler)
193193
}
194194

195-
let c_top = swiftparse_parse_string(c_parser, source)
195+
let c_top = source.withCString { buf in
196+
swiftparse_parse_string(c_parser, buf, source.utf8.count)
197+
}
196198

197-
// Get ownership back from the C parser.
199+
// Get ownership back from the C parser.
198200
return RawSyntax.moveFromOpaque(c_top)!
199201
}
200202
}

Tests/SwiftSyntaxTest/SyntaxTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,10 @@ public class SyntaxTests: XCTestCase {
151151
let sourceNsString = "var 🎉 = 2" as NSString
152152
_ = try? SyntaxParser.parse(source: sourceNsString as String)
153153
}
154+
155+
public func testParseFileWithNullCharacter() throws {
156+
let source = "var x = 1\0\nvar y = 2"
157+
let tree = try SyntaxParser.parse(source: source)
158+
XCTAssertEqual(tree.description, source)
159+
}
154160
}

0 commit comments

Comments
 (0)