mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-28 22:02:37 +01:00
Validate source text size and fix empty buffer bugs. (#1113)
There's currently a bug with empty files, in that it initializes SourceBuffer with an invalid StringRef that results in a crash. That got me looking at the std::optional TODO, but the issue is that there are really three states: - Buffered - mmapped (not buffered) - Moved out of (no longer initialized) Technically an optional could work if we initialize the buffer on move out, indicating the mmap is gone. But the mode setup felt better to me. And then this also adds the size check. Which is really how I started looking at this.
This commit is contained in:
@@ -15,17 +15,17 @@ namespace Carbon::Testing {
|
||||
namespace {
|
||||
|
||||
TEST(SourceBufferTest, StringRep) {
|
||||
SourceBuffer buffer =
|
||||
SourceBuffer::CreateFromText(llvm::Twine("Hello") + " World");
|
||||
|
||||
EXPECT_EQ("/text", buffer.Filename());
|
||||
EXPECT_EQ("Hello World", buffer.Text());
|
||||
auto buffer = SourceBuffer::CreateFromText(llvm::Twine("Hello") + " World");
|
||||
EXPECT_EQ("/text", buffer->Filename());
|
||||
EXPECT_EQ("Hello World", buffer->Text());
|
||||
}
|
||||
|
||||
TEST(SourceBufferText, StringRepWithFilename) {
|
||||
// Give a custom filename.
|
||||
auto buffer2 =
|
||||
auto buffer =
|
||||
SourceBuffer::CreateFromText("Hello World Again!", "/custom/text");
|
||||
EXPECT_EQ("/custom/text", buffer2.Filename());
|
||||
EXPECT_EQ("Hello World Again!", buffer2.Text());
|
||||
EXPECT_EQ("/custom/text", buffer->Filename());
|
||||
EXPECT_EQ("Hello World Again!", buffer->Text());
|
||||
}
|
||||
|
||||
auto CreateTestFile(llvm::StringRef text) -> std::string {
|
||||
@@ -58,5 +58,18 @@ TEST(SourceBufferTest, FileRep) {
|
||||
EXPECT_EQ("Hello World", buffer.Text());
|
||||
}
|
||||
|
||||
TEST(SourceBufferTest, FileRepEmpty) {
|
||||
auto test_file_path = CreateTestFile("");
|
||||
|
||||
auto expected_buffer = SourceBuffer::CreateFromFile(test_file_path);
|
||||
ASSERT_TRUE(static_cast<bool>(expected_buffer))
|
||||
<< "Error message: " << toString(expected_buffer.takeError());
|
||||
|
||||
SourceBuffer& buffer = *expected_buffer;
|
||||
|
||||
EXPECT_EQ(test_file_path, buffer.Filename());
|
||||
EXPECT_EQ("", buffer.Text());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace Carbon::Testing
|
||||
|
||||
Reference in New Issue
Block a user