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/lib/Basic | |
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/lib/Basic')
-rw-r--r-- | clang/lib/Basic/SourceManager.cpp | 21 |
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. |