Skip to content

Commit 130c135

Browse files
authored
[clang] Fix crash with multiple non-parenthsized sizeof (#101297)
There are 5 unary operators that can be followed by a non-parenthesized expression: `sizeof`, `__datasizeof`, `__alignof`, `alignof`, `_Alignof`. When we nest them too deep, `BalancedDelimiterTracker` does not help, because there are no parentheses, and we crash. Instead, this patch recognize chains of those operators, and parse them with sufficient stack space. Fixes #45061
1 parent b933517 commit 130c135

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ Miscellaneous Clang Crashes Fixed
183183
- Fixed a crash in C due to incorrect lookup that members in nested anonymous struct/union
184184
can be found as ordinary identifiers in struct/union definition. (#GH31295)
185185

186+
- Fixed a crash caused by long chains of ``sizeof`` and other similar operators
187+
that can be followed by a non-parenthesized expression. (#GH45061)
188+
186189
OpenACC Specific Changes
187190
------------------------
188191

clang/lib/Parse/ParseExpr.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2479,7 +2479,19 @@ Parser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok,
24792479
return ExprError();
24802480
}
24812481

2482-
Operand = ParseCastExpression(UnaryExprOnly);
2482+
// If we're parsing a chain that consists of keywords that could be
2483+
// followed by a non-parenthesized expression, BalancedDelimiterTracker
2484+
// is not going to help when the nesting is too deep. In this corner case
2485+
// we continue to parse with sufficient stack space to avoid crashing.
2486+
if (OpTok.isOneOf(tok::kw_sizeof, tok::kw___datasizeof, tok::kw___alignof,
2487+
tok::kw_alignof, tok::kw__Alignof) &&
2488+
Tok.isOneOf(tok::kw_sizeof, tok::kw___datasizeof, tok::kw___alignof,
2489+
tok::kw_alignof, tok::kw__Alignof))
2490+
Actions.runWithSufficientStackSpace(Tok.getLocation(), [&] {
2491+
Operand = ParseCastExpression(UnaryExprOnly);
2492+
});
2493+
else
2494+
Operand = ParseCastExpression(UnaryExprOnly);
24832495
} else {
24842496
// If it starts with a '(', we know that it is either a parenthesized
24852497
// type-name, or it is a unary-expression that starts with a compound

0 commit comments

Comments
 (0)