Skip to content

Commit ff826e3

Browse files
authored
[Parse] Limit max nesting level of StructureMarkerRAII (#7330)
The threshold is 256, for now.
1 parent f2d426f commit ff826e3

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ NOTE(opening_angle,none,
5252
ERROR(extra_rbrace,none,
5353
"extraneous '}' at top level", ())
5454

55+
ERROR(structure_overflow,Fatal,
56+
"structure nesting level exceeded maximum of %0", (unsigned))
57+
5558
ERROR(unexpected_conditional_compilation_block_terminator,none,
5659
"unexpected conditional compilation block terminator", ())
5760
ERROR(expected_conditional_compilation_expression,none,

include/swift/Parse/Parser.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ class Parser {
165165

166166
/// \brief The location of the previous token.
167167
SourceLoc PreviousLoc;
168+
169+
/// Stop parsing immediately.
170+
void cutOffParsing() {
171+
// Cut off parsing by acting as if we reached the end-of-file.
172+
Tok.setKind(tok::eof);
173+
}
168174

169175
/// A RAII object for temporarily changing CurDeclContext.
170176
class ContextChange {
@@ -255,11 +261,22 @@ class Parser {
255261
class StructureMarkerRAII {
256262
Parser &P;
257263

264+
/// Max nesting level
265+
// TODO: customizable.
266+
enum { MaxDepth = 256 };
267+
268+
void diagnoseOverflow();
269+
258270
public:
259271
StructureMarkerRAII(Parser &parser, SourceLoc loc,
260272
StructureMarkerKind kind)
261273
: P(parser) {
262274
P.StructureMarkers.push_back({loc, kind, None});
275+
276+
if (P.StructureMarkers.size() >= MaxDepth) {
277+
diagnoseOverflow();
278+
P.cutOffParsing();
279+
}
263280
}
264281

265282
StructureMarkerRAII(Parser &parser, const Token &tok);

lib/Parse/Parser.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,11 @@ Parser::StructureMarkerRAII::StructureMarkerRAII(Parser &parser,
549549
}
550550
}
551551

552+
void Parser::StructureMarkerRAII::diagnoseOverflow() {
553+
auto Loc = P.StructureMarkers.back().Loc;
554+
P.diagnose(Loc, diag::structure_overflow, MaxDepth);
555+
}
556+
552557
//===----------------------------------------------------------------------===//
553558
// Primitive Parsing
554559
//===----------------------------------------------------------------------===//

test/Parse/structure_overflow.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: not %target-swift-frontend -typecheck %s 2>&1 | %FileCheck %s
2+
3+
// level 512.
4+
// CHECK: :[[@LINE+1]]:256: error: structure nesting level exceeded maximum of 256
5+
[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[({[(
6+
7+
// CHECK-NOT: error
8+
;blah blah blah

validation-test/compiler_crashers/28616-swift-parser-parseexprsequence-swift-diag-bool-bool.swift renamed to validation-test/compiler_crashers_fixed/28616-swift-parser-parseexprsequence-swift-diag-bool-bool.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,5 @@
55
// See https://swift.org/LICENSE.txt for license information
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// REQUIRES: deterministic-behavior
9-
10-
// RUN: not --crash %target-swift-frontend %s -emit-ir
8+
// RUN: not %target-swift-frontend %s -emit-ir
119
let x = [{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{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