From 5f4ac97702b99d425844240611eecaa650b42429 Mon Sep 17 00:00:00 2001 From: Manuel Klimek Date: Thu, 11 Jun 2015 10:14:13 +0000 Subject: [PATCH] Fix crash in clang-format. The following example used to crash clang-format. #define a\ /**/} Adjusting the indentation level cache for the line starting with the comment would lead to an out-of-bounds array read. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@239521 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/UnwrappedLineFormatter.cpp | 6 ++++-- unittests/Format/FormatTest.cpp | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/Format/UnwrappedLineFormatter.cpp b/lib/Format/UnwrappedLineFormatter.cpp index cbf8c6c922..ee81b509d3 100644 --- a/lib/Format/UnwrappedLineFormatter.cpp +++ b/lib/Format/UnwrappedLineFormatter.cpp @@ -51,11 +51,13 @@ public: /// next. void nextLine(const AnnotatedLine &Line) { Offset = getIndentOffset(*Line.First); + // Update the indent level cache size so that we can rely on it + // having the right size in adjustToUnmodifiedline. + while (IndentForLevel.size() <= Line.Level) + IndentForLevel.push_back(-1); if (Line.InPPDirective) { Indent = Line.Level * Style.IndentWidth + AdditionalIndent; } else { - while (IndentForLevel.size() <= Line.Level) - IndentForLevel.push_back(-1); IndentForLevel.resize(Line.Level + 1); Indent = getIndent(IndentForLevel, Line.Level); } diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 5b3980bdb5..46be32bf4b 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -10576,7 +10576,10 @@ TEST_F(FormatTest, DisableRegions) { " int k;")); } -TEST_F(FormatTest, DoNotCrashOnInvalidInput) { format("? ) ="); } +TEST_F(FormatTest, DoNotCrashOnInvalidInput) { + format("? ) ="); + verifyNoCrash("#define a\\\n /**/}"); +} } // end namespace tooling } // end namespace clang -- 2.40.0