summaryrefslogtreecommitdiffstats
path: root/clang/lib/Basic
diff options
context:
space:
mode:
authorJan Korous <jkorous@apple.com>2020-01-10 11:04:22 -0800
committerJan Korous <jkorous@apple.com>2020-01-10 11:22:41 -0800
commitf28972facc1fce9589feab9803e3e8cfad01891c (patch)
treed4e98e4e4bbe0992665b3006a1c76e2454ae7bf7 /clang/lib/Basic
parent815a3f54331c39f2b400776f448dd29b3b03243b (diff)
downloadbcm5719-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/lib/Basic')
-rw-r--r--clang/lib/Basic/SourceManager.cpp21
1 files changed, 8 insertions, 13 deletions
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index 5f457d6f9e3..73f2ae96d4a 100644
--- a/clang/lib/Basic/SourceManager.cpp
+++ b/clang/lib/Basic/SourceManager.cpp
@@ -1250,23 +1250,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.
OpenPOWER on IntegriCloud