Skip to content

[clang] Fix out-of-bounds memory access in ComputeLineNumbers #556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions clang/lib/Basic/SourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1216,23 +1216,18 @@ static void ComputeLineNumbers(DiagnosticsEngine &Diag, ContentCache *FI,

const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
const std::size_t BufLen = End - Buf;
unsigned I = 0;
while (true) {
// Skip over the contents of the line.
while (Buf[I] != '\n' && Buf[I] != '\r' && Buf[I] != '\0')
++I;

if (Buf[I] == '\n' || Buf[I] == '\r') {
while (I < BufLen) {
if (Buf[I] == '\n') {
LineOffsets.push_back(I + 1);
} else if (Buf[I] == '\r') {
// If this is \r\n, skip both characters.
if (Buf[I] == '\r' && Buf[I+1] == '\n')
if (I + 1 < BufLen && Buf[I + 1] == '\n')
++I;
++I;
LineOffsets.push_back(I);
} else {
// Otherwise, this is a NUL. If end of file, exit.
if (Buf+I == End) break;
++I;
LineOffsets.push_back(I + 1);
}
++I;
}

// Copy the offsets into the FileInfo structure.
Expand Down
24 changes: 24 additions & 0 deletions clang/unittests/Basic/SourceManagerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -200,6 +202,28 @@ TEST_F(SourceManagerTest, locationPrintTest) {
"</mainFile.cpp:1:1, /test-header.h:1:1>");
}

// 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) {
Expand Down