Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler): avoid errors with extremely long instruction chains #45574

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/compiler/src/render3/view/util.ts
Expand Up @@ -50,6 +50,13 @@ export const NON_BINDABLE_ATTR = 'ngNonBindable';
/** Name for the variable keeping track of the context returned by `ɵɵrestoreView`. */
export const RESTORED_VIEW_CONTEXT_NAME = 'restoredCtx';

/**
* Maximum length of a single instruction chain. Because our output AST uses recursion, we're
* limited in how many expressions we can nest before we reach the call stack limit. This
* length is set very conservatively in order to reduce the chance of problems.
*/
const MAX_CHAIN_LENGTH = 500;

/** Instructions that support chaining. */
const CHAINABLE_INSTRUCTIONS = new Set([
R3.element,
Expand Down Expand Up @@ -309,6 +316,7 @@ export function getInstructionStatements(instructions: Instruction[]): o.Stateme
const statements: o.Statement[] = [];
let pendingExpression: o.Expression|null = null;
let pendingExpressionType: o.ExternalReference|null = null;
let chainLength = 0;

for (const current of instructions) {
const resolvedParams =
Expand All @@ -318,16 +326,18 @@ export function getInstructionStatements(instructions: Instruction[]): o.Stateme

// If the current instruction is the same as the previous one
// and it can be chained, add another call to the chain.
if (pendingExpressionType === current.reference &&
if (chainLength < MAX_CHAIN_LENGTH && pendingExpressionType === current.reference &&
CHAINABLE_INSTRUCTIONS.has(pendingExpressionType)) {
// We'll always have a pending expression when there's a pending expression type.
pendingExpression = pendingExpression!.callFn(params, pendingExpression!.sourceSpan);
chainLength++;
} else {
if (pendingExpression !== null) {
statements.push(pendingExpression.toStmt());
}
pendingExpression = invokeInstruction(current.span, current.reference, params);
pendingExpressionType = current.reference;
chainLength = 0;
}
}

Expand Down