[clang] 14c30c7 - [clang-format] Avoid crash in LevelIndentTracker.

Marek Kurdej via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 7 01:15:49 PDT 2022


Author: Marek Kurdej
Date: 2022-07-07T10:15:45+02:00
New Revision: 14c30c70c4595d8f410c74a72357a5a31fdd5507

URL: https://github.com/llvm/llvm-project/commit/14c30c70c4595d8f410c74a72357a5a31fdd5507
DIFF: https://github.com/llvm/llvm-project/commit/14c30c70c4595d8f410c74a72357a5a31fdd5507.diff

LOG: [clang-format] Avoid crash in LevelIndentTracker.

Fixes https://github.com/llvm/llvm-project/issues/56352.

Reviewed By: HazardyKnusperkeks, owenpan, MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D129064

Added: 
    

Modified: 
    clang/lib/Format/UnwrappedLineFormatter.cpp
    clang/unittests/Format/FormatTestSelective.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 22509a5042465..9a6f8884fcec3 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -66,7 +66,6 @@ class LevelIndentTracker {
           (Style.PPIndentWidth >= 0) ? Style.PPIndentWidth : Style.IndentWidth;
       Indent = Line.Level * IndentWidth + AdditionalIndent;
     } else {
-      IndentForLevel.resize(Line.Level + 1);
       Indent = getIndent(Line.Level);
     }
     if (static_cast<int>(Indent) + Offset >= 0)
@@ -91,6 +90,7 @@ class LevelIndentTracker {
     unsigned LevelIndent = Line.First->OriginalColumn;
     if (static_cast<int>(LevelIndent) - Offset >= 0)
       LevelIndent -= Offset;
+    assert(Line.Level < IndentForLevel.size());
     if ((!Line.First->is(tok::comment) || IndentForLevel[Line.Level] == -1) &&
         !Line.InPPDirective) {
       IndentForLevel[Line.Level] = LevelIndent;

diff  --git a/clang/unittests/Format/FormatTestSelective.cpp b/clang/unittests/Format/FormatTestSelective.cpp
index 2725e4cf776f6..86ed7aba1913d 100644
--- a/clang/unittests/Format/FormatTestSelective.cpp
+++ b/clang/unittests/Format/FormatTestSelective.cpp
@@ -609,6 +609,14 @@ TEST_F(FormatTestSelective, DontAssert) {
                      "  return a == 8 ? 32 : 16;\n"
                      "}\n";
   EXPECT_EQ(Code, format(Code, 40, 0));
+
+  // https://llvm.org/PR56352
+  Style.CompactNamespaces = true;
+  Style.NamespaceIndentation = FormatStyle::NI_All;
+  Code = "\n"
+         "namespace ns1 { namespace ns2 {\n"
+         "}}";
+  EXPECT_EQ(Code, format(Code, 0, 0));
 }
 
 } // end namespace


        


More information about the cfe-commits mailing list