diff options
author | David Majnemer <david.majnemer@gmail.com> | 2016-02-17 22:37:45 +0000 |
---|---|---|
committer | David Majnemer <david.majnemer@gmail.com> | 2016-02-17 22:37:45 +0000 |
commit | 38a3dbdcbf065786a847e0848680c72e2abe19c5 (patch) | |
tree | 32d8fa63b8004c8f4ccc030dcbc13bad482bce15 /clang/lib/Frontend/TextDiagnostic.cpp | |
parent | 1c793ef3ddcd64e19461313271b5e7c36f0b1857 (diff) | |
download | bcm5719-llvm-38a3dbdcbf065786a847e0848680c72e2abe19c5.tar.gz bcm5719-llvm-38a3dbdcbf065786a847e0848680c72e2abe19c5.zip |
Don't crash w/ a diagnostic range containing a null byte
We prematurely ended the line at the null byte which caused us to crash
down stream because we tried to reason about columns beyond the end of
the line.
llvm-svn: 261171
Diffstat (limited to 'clang/lib/Frontend/TextDiagnostic.cpp')
-rw-r--r-- | clang/lib/Frontend/TextDiagnostic.cpp | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/clang/lib/Frontend/TextDiagnostic.cpp b/clang/lib/Frontend/TextDiagnostic.cpp index d4e156d4458..5dda151d558 100644 --- a/clang/lib/Frontend/TextDiagnostic.cpp +++ b/clang/lib/Frontend/TextDiagnostic.cpp @@ -1082,10 +1082,13 @@ void TextDiagnostic::emitSnippetAndCaret( // Get information about the buffer it points into. bool Invalid = false; - const char *BufStart = SM.getBufferData(FID, &Invalid).data(); + StringRef BufData = SM.getBufferData(FID, &Invalid); if (Invalid) return; + const char *BufStart = BufData.data(); + const char *BufEnd = BufStart + BufData.size(); + unsigned LineNo = SM.getLineNumber(FID, FileOffset); unsigned ColNo = SM.getColumnNumber(FID, FileOffset); @@ -1101,15 +1104,20 @@ void TextDiagnostic::emitSnippetAndCaret( // Compute the line end. Scan forward from the error position to the end of // the line. const char *LineEnd = TokPtr; - while (*LineEnd != '\n' && *LineEnd != '\r' && *LineEnd != '\0') + while (*LineEnd != '\n' && *LineEnd != '\r' && LineEnd != BufEnd) ++LineEnd; // Arbitrarily stop showing snippets when the line is too long. if (size_t(LineEnd - LineStart) > MaxLineLengthToPrint) return; + // Trim trailing null-bytes. + StringRef Line(LineStart, LineEnd - LineStart); + while (Line.size() > ColNo && Line.back() == '\0') + Line = Line.drop_back(); + // Copy the line of code into an std::string for ease of manipulation. - std::string SourceLine(LineStart, LineEnd); + std::string SourceLine(Line.begin(), Line.end()); // Build the byte to column map. const SourceColumnMap sourceColMap(SourceLine, DiagOpts->TabStop); |