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();
 


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();
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to