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

Conversation

ahejlsberg
Copy link
Member

Fixes #871.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a missing bounds check in isReachableFlowNodeWorker to prevent potential index out of range errors during flow analysis.

  • Added a new test case for assertion with no argument.
  • Updated baseline reference files for types and symbols.
  • Modified internal/checker/flow.go to add a bounds check for predicate.parameterIndex.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
testdata/tests/cases/compiler/assertionWithNoArgument.ts Adds a test function and test calls for assertion behavior.
testdata/baselines/reference/compiler/assertionWithNoArgument.types Updates baseline for types to reflect the new assertion function.
testdata/baselines/reference/compiler/assertionWithNoArgument.symbols Updates symbol baseline to include the new assertion function.
internal/checker/flow.go Adds a bounds check for predicate.parameterIndex to prevent index out of range errors.

return false
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) {
Copy link
Preview

Copilot AI May 16, 2025

Choose a reason for hiding this comment

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

The added bounds check prevents potential index out of range errors. Consider whether additional handling (e.g., logging) is needed if predicate.parameterIndex is unexpectedly out of bounds.

Copilot uses AI. Check for mistakes.

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) {
if predicateArgument := arguments[predicate.parameterIndex]; predicateArgument != nil && c.isFalseExpression(predicateArgument) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think you need predicateArgument != nil once you have the bounds check.

Suggested change
if predicateArgument := arguments[predicate.parameterIndex]; predicateArgument != nil && c.isFalseExpression(predicateArgument) {
if predicateArgument := arguments[predicate.parameterIndex]; c.isFalseExpression(predicateArgument) {

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed.

Comment on lines +7 to +8
assertWeird();
assertWeird("hello");
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.

@DanielRosenwasser
Copy link
Member

Any idea why two calls are necessary to trigger this crash?

@ahejlsberg
Copy link
Member Author

Any idea why two calls are necessary to trigger this crash?

You just need some other statement after the argument-less call to trigger the situation. It's because in the call to isReachableFlowNode from checkSourceElementWorker, we're only going to examine the flow node graph for nodes that precede the current node.

@ahejlsberg ahejlsberg added this pull request to the merge queue May 16, 2025
Merged via the queue into main with commit abd526f May 16, 2025
23 checks passed
@jakebailey jakebailey deleted the fix-871 branch June 2, 2025 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash on assertion predicate with optional argument
3 participants