Skip to content

Commit 50a8b88

Browse files
Merge pull request #556 from jkorous-apple/fix-ComputeLineNumbers
[clang] Fix out-of-bounds memory access in ComputeLineNumbers
2 parents 5f29877 + ba7dc9c commit 50a8b88

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

clang/lib/Basic/SourceManager.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,23 +1216,18 @@ static void ComputeLineNumbers(DiagnosticsEngine &Diag, ContentCache *FI,
12161216

12171217
const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
12181218
const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
1219+
const std::size_t BufLen = End - Buf;
12191220
unsigned I = 0;
1220-
while (true) {
1221-
// Skip over the contents of the line.
1222-
while (Buf[I] != '\n' && Buf[I] != '\r' && Buf[I] != '\0')
1223-
++I;
1224-
1225-
if (Buf[I] == '\n' || Buf[I] == '\r') {
1221+
while (I < BufLen) {
1222+
if (Buf[I] == '\n') {
1223+
LineOffsets.push_back(I + 1);
1224+
} else if (Buf[I] == '\r') {
12261225
// If this is \r\n, skip both characters.
1227-
if (Buf[I] == '\r' && Buf[I+1] == '\n')
1226+
if (I + 1 < BufLen && Buf[I + 1] == '\n')
12281227
++I;
1229-
++I;
1230-
LineOffsets.push_back(I);
1231-
} else {
1232-
// Otherwise, this is a NUL. If end of file, exit.
1233-
if (Buf+I == End) break;
1234-
++I;
1228+
LineOffsets.push_back(I + 1);
12351229
}
1230+
++I;
12361231
}
12371232

12381233
// Copy the offsets into the FileInfo structure.

clang/unittests/Basic/SourceManagerTest.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
#include "clang/Lex/PreprocessorOptions.h"
2121
#include "llvm/ADT/SmallString.h"
2222
#include "llvm/Config/llvm-config.h"
23+
#include "llvm/Support/Process.h"
2324
#include "gtest/gtest.h"
25+
#include <cstddef>
2426

2527
using namespace clang;
2628

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

205+
// Regression test - there was an out of bound access for buffers not terminated by zero.
206+
TEST_F(SourceManagerTest, getLineNumber) {
207+
const unsigned pageSize = llvm::sys::Process::getPageSizeEstimate();
208+
std::unique_ptr<char[]> source(new char[pageSize]);
209+
for(unsigned i = 0; i < pageSize; ++i) {
210+
source[i] = 'a';
211+
}
212+
213+
std::unique_ptr<llvm::MemoryBuffer> Buf =
214+
llvm::MemoryBuffer::getMemBuffer(
215+
llvm::MemoryBufferRef(
216+
llvm::StringRef(source.get(), 3), "whatever"
217+
),
218+
false
219+
);
220+
221+
FileID mainFileID = SourceMgr.createFileID(std::move(Buf));
222+
SourceMgr.setMainFileID(mainFileID);
223+
224+
ASSERT_NO_FATAL_FAILURE(SourceMgr.getLineNumber(mainFileID, 1, nullptr));
225+
}
226+
203227
#if defined(LLVM_ON_UNIX)
204228

205229
TEST_F(SourceManagerTest, getMacroArgExpandedLocation) {

0 commit comments

Comments
 (0)