diff options
author | Jan Korous <jkorous@apple.com> | 2020-01-10 11:04:22 -0800 |
---|---|---|
committer | Jan Korous <jkorous@apple.com> | 2020-01-10 11:22:41 -0800 |
commit | f28972facc1fce9589feab9803e3e8cfad01891c (patch) | |
tree | d4e98e4e4bbe0992665b3006a1c76e2454ae7bf7 /clang/unittests/Basic/SourceManagerTest.cpp | |
parent | 815a3f54331c39f2b400776f448dd29b3b03243b (diff) | |
download | bcm5719-llvm-f28972facc1fce9589feab9803e3e8cfad01891c.tar.gz bcm5719-llvm-f28972facc1fce9589feab9803e3e8cfad01891c.zip |
[clang] Fix out-of-bounds memory access in ComputeLineNumbers
Differential Revision: https://reviews.llvm.org/D72409
Diffstat (limited to 'clang/unittests/Basic/SourceManagerTest.cpp')
-rw-r--r-- | clang/unittests/Basic/SourceManagerTest.cpp | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/clang/unittests/Basic/SourceManagerTest.cpp b/clang/unittests/Basic/SourceManagerTest.cpp index 465f7a06f71..07c72e27f9f 100644 --- a/clang/unittests/Basic/SourceManagerTest.cpp +++ b/clang/unittests/Basic/SourceManagerTest.cpp @@ -20,7 +20,9 @@ #include "clang/Lex/PreprocessorOptions.h" #include "llvm/ADT/SmallString.h" #include "llvm/Config/llvm-config.h" +#include "llvm/Support/Process.h" #include "gtest/gtest.h" +#include <cstddef> using namespace clang; @@ -241,6 +243,28 @@ TEST_F(SourceManagerTest, getInvalidBOM) { "UTF-32 (LE)"); } +// Regression test - there was an out of bound access for buffers not terminated by zero. +TEST_F(SourceManagerTest, getLineNumber) { + const unsigned pageSize = llvm::sys::Process::getPageSizeEstimate(); + std::unique_ptr<char[]> source(new char[pageSize]); + for(unsigned i = 0; i < pageSize; ++i) { + source[i] = 'a'; + } + + std::unique_ptr<llvm::MemoryBuffer> Buf = + llvm::MemoryBuffer::getMemBuffer( + llvm::MemoryBufferRef( + llvm::StringRef(source.get(), 3), "whatever" + ), + false + ); + + FileID mainFileID = SourceMgr.createFileID(std::move(Buf)); + SourceMgr.setMainFileID(mainFileID); + + ASSERT_NO_FATAL_FAILURE(SourceMgr.getLineNumber(mainFileID, 1, nullptr)); +} + #if defined(LLVM_ON_UNIX) TEST_F(SourceManagerTest, getMacroArgExpandedLocation) { |