From fd725c060bf727f8967eef96dd6a72213fe93887 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Wed, 21 Jan 2015 17:35:29 +0000 Subject: [PATCH] clang-format: Fix use-heap-after-free bug. Discovered by the awesome test case and ASAN. llvm-svn: 226678 --- clang/lib/Format/TokenAnnotator.h | 12 +++++++----- clang/unittests/Format/FormatTest.cpp | 2 ++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.h b/clang/lib/Format/TokenAnnotator.h index ff8e32a..8aa163a 100644 --- a/clang/lib/Format/TokenAnnotator.h +++ b/clang/lib/Format/TokenAnnotator.h @@ -59,11 +59,8 @@ public: I->Tok->Previous = Current; Current = Current->Next; Current->Children.clear(); - for (SmallVectorImpl::const_iterator - I = Node.Children.begin(), - E = Node.Children.end(); - I != E; ++I) { - Children.push_back(new AnnotatedLine(*I)); + for (const auto& Child : Node.Children) { + Children.push_back(new AnnotatedLine(Child)); Current->Children.push_back(Children.back()); } } @@ -75,6 +72,11 @@ public: for (unsigned i = 0, e = Children.size(); i != e; ++i) { delete Children[i]; } + FormatToken *Current = First; + while (Current) { + Current->Children.clear(); + Current = Current->Next; + } } FormatToken *First; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 436835b..3aa5346 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -2610,6 +2610,8 @@ TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { getLLVMStyleWithColumns(28)); verifyFormat("#d, = };"); verifyFormat("#if \"a"); + + verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); } TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { -- 2.7.4