r261171 - Don't crash w/ a diagnostic range containing a null byte

David Majnemer via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 17 14:37:45 PST 2016


Author: majnemer
Date: Wed Feb 17 16:37:45 2016
New Revision: 261171

URL: http://llvm.org/viewvc/llvm-project?rev=261171&view=rev
Log:
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.

Added:
    cfe/trunk/test/Misc/diag-null-bytes-in-line.cpp
Modified:
    cfe/trunk/lib/Frontend/TextDiagnostic.cpp

Modified: cfe/trunk/lib/Frontend/TextDiagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/TextDiagnostic.cpp?rev=261171&r1=261170&r2=261171&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/TextDiagnostic.cpp (original)
+++ cfe/trunk/lib/Frontend/TextDiagnostic.cpp Wed Feb 17 16:37:45 2016
@@ -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);

Added: cfe/trunk/test/Misc/diag-null-bytes-in-line.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Misc/diag-null-bytes-in-line.cpp?rev=261171&view=auto
==============================================================================
Binary files cfe/trunk/test/Misc/diag-null-bytes-in-line.cpp (added) and cfe/trunk/test/Misc/diag-null-bytes-in-line.cpp Wed Feb 17 16:37:45 2016 differ




More information about the cfe-commits mailing list