SourceMgr diagnotics printing: fix a bug where printing a fixit for a source
authorDmitri Gribenko <gribozavr@gmail.com>
Fri, 27 Sep 2013 21:24:36 +0000 (21:24 +0000)
committerDmitri Gribenko <gribozavr@gmail.com>
Fri, 27 Sep 2013 21:24:36 +0000 (21:24 +0000)
range that includes a tab character will cause out-of-bounds access to the
fixit string.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@191563 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/SourceMgr.cpp
unittests/Support/SourceMgrTest.cpp

index fbcd8f980c168fd8bb0ad3406b25f0c79469320c..d4b94f8cd5d7613f8cc4b50e91d7448232a42343 100644 (file)
@@ -470,7 +470,7 @@ void SMDiagnostic::print(const char *ProgName, raw_ostream &S,
   if (FixItInsertionLine.empty())
     return;
   
-  for (size_t i = 0, e = FixItInsertionLine.size(), OutCol = 0; i != e; ++i) {
+  for (size_t i = 0, e = FixItInsertionLine.size(), OutCol = 0; i < e; ++i) {
     if (i >= LineContents.size() || LineContents[i] != '\t') {
       S << FixItInsertionLine[i];
       ++OutCol;
index 9bc0cf67b754c758964e5fba2e0a0986af3f635d..2b69fe984445ead0da586d1fb5ae4224d256ab79 100644 (file)
@@ -160,3 +160,15 @@ TEST_F(SourceMgrTest, BasicFixit) {
             Output);
 }
 
+TEST_F(SourceMgrTest, FixitForTab) {
+  setMainBuffer("aaa\tbbb\nccc ddd\n", "file.in");
+  printMessage(getLoc(3), SourceMgr::DK_Error, "message", None,
+               makeArrayRef(SMFixIt(getRange(3, 1), "zzz")));
+
+  EXPECT_EQ("file.in:1:4: error: message\n"
+            "aaa     bbb\n"
+            "   ^^^^^\n"
+            "   zzz\n",
+            Output);
+}
+