Skip to content

Add missing bounds check in isReachableFlowNodeWorker #873

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

Merged
merged 4 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions internal/checker/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2484,12 +2484,9 @@ func (c *Checker) isReachableFlowNodeWorker(f *FlowState, flow *ast.FlowNode, no
case flags&(ast.FlowFlagsAssignment|ast.FlowFlagsCondition|ast.FlowFlagsArrayMutation) != 0:
flow = flow.Antecedent
case flags&ast.FlowFlagsCall != 0:
signature := c.getEffectsSignature(flow.Node)
if signature != nil {
predicate := c.getTypePredicateOfSignature(signature)
if predicate != nil && predicate.kind == TypePredicateKindAssertsIdentifier && predicate.t == nil {
predicateArgument := flow.Node.Arguments()[predicate.parameterIndex]
if predicateArgument != nil && c.isFalseExpression(predicateArgument) {
if signature := c.getEffectsSignature(flow.Node); signature != nil {
if predicate := c.getTypePredicateOfSignature(signature); predicate != nil && predicate.kind == TypePredicateKindAssertsIdentifier && predicate.t == nil {
if arguments := flow.Node.Arguments(); int(predicate.parameterIndex) < len(arguments) && c.isFalseExpression(arguments[predicate.parameterIndex]) {
return false
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//// [tests/cases/compiler/assertionWithNoArgument.ts] ////

=== assertionWithNoArgument.ts ===
export function assertWeird(value?: string): asserts value {
>assertWeird : Symbol(assertWeird, Decl(assertionWithNoArgument.ts, 0, 0))
>value : Symbol(value, Decl(assertionWithNoArgument.ts, 0, 28))
>value : Symbol(value, Decl(assertionWithNoArgument.ts, 0, 28))
}

assertWeird();
>assertWeird : Symbol(assertWeird, Decl(assertionWithNoArgument.ts, 0, 0))

assertWeird("hello");
>assertWeird : Symbol(assertWeird, Decl(assertionWithNoArgument.ts, 0, 0))

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//// [tests/cases/compiler/assertionWithNoArgument.ts] ////

=== assertionWithNoArgument.ts ===
export function assertWeird(value?: string): asserts value {
>assertWeird : (value?: string | undefined) => asserts value
>value : string | undefined
}

assertWeird();
>assertWeird() : void
>assertWeird : (value?: string | undefined) => asserts value

assertWeird("hello");
>assertWeird("hello") : void
>assertWeird : (value?: string | undefined) => asserts value
>"hello" : "hello"

8 changes: 8 additions & 0 deletions testdata/tests/cases/compiler/assertionWithNoArgument.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @strict: true
// @noemit: true

export function assertWeird(value?: string): asserts value {
}

assertWeird();
assertWeird("hello");
Comment on lines +7 to +8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth having another test that swaps the order?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, isn't really relevant. See my comment below.