Fix handling of null StringRef file buffers (#5428)

The current behavior hits UBSAN and ASAN issues.

Note, `RequiresNullTerminator` is already set to `false` in
`source_buffer.cpp`; setting it in `compile_helper.cpp` is making things
more consistent. The related logic is an [assert
fail](https://github.com/llvm/llvm-project/blob/main/llvm/lib/Support/MemoryBuffer.cpp#L52).

This was fuzzer-discovered.
This commit is contained in:
Jon Ross-Perkins authored and GitHub committed 2025-05-07 22:42:58 +00:00
1 parent 66caff2c26
commit ae16332a11
4 files changed
+38 -8

No files matched your search

+8
View File
@@ -50,6 +50,14 @@ TEST_F(LexerTest, HandlesEmptyBuffer) {
{.kind = TokenKind::FileEnd}}));
}
TEST_F(LexerTest, NullStringRef) {
auto& buffer = compile_helper_.GetTokenizedBuffer(llvm::StringRef());
EXPECT_FALSE(buffer.has_errors());
EXPECT_THAT(buffer, HasTokens(llvm::ArrayRef<ExpectedToken>{
{.kind = TokenKind::FileStart},
{.kind = TokenKind::FileEnd}}));
}
TEST_F(LexerTest, TracksLinesAndColumns) {
auto& buffer = compile_helper_.GetTokenizedBuffer(
"\n ;;\n ;;;\n x\"foo\" '''baz\n a\n ''' y");