[PATCH] D127183: [clang-format] Skip parsing a block if it's nested too deep

Owen Pan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 6 23:17:27 PDT 2022


owenpan created this revision.
owenpan added reviewers: MyDeveloperDay, curdeius, HazardyKnusperkeks.
owenpan added a project: clang-format.
Herald added a project: All.
owenpan requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Don't parse blocks that are nested hundreds of levels deep, e.g., https://github.com/llvm/llvm-project/blob/a7b154aa1770a2223be2e99735fab54c1c081007/clang/test/Parser/parser_overflow.c#L11.

Adding a pointer parameter to each of `parseLevel()` and `parseBlock()` would overflow the stack when formatting clang/test/Parser/parser_overflow.c, so adding an upper bound to the number of levels of nested blocks will provide a safeguard.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D127183

Files:
  clang/lib/Format/UnwrappedLineParser.cpp


Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -837,6 +837,10 @@
   unsigned InitialLevel = Line->Level;
   nextToken(/*LevelDifference=*/AddLevels);
 
+  // Bail out if there are too many levels. Otherwise, the stack might overflow.
+  if (Line->Level > 300)
+    return IfStmtKind::NotIf;
+
   if (MacroBlock && FormatTok->is(tok::l_paren))
     parseParens();
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127183.434697.patch
Type: text/x-patch
Size: 527 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220607/2dc26d9f/attachment.bin>


More information about the cfe-commits mailing list