-
Notifications
You must be signed in to change notification settings - Fork 742
Fix panic on negative parameterIndex in type predicate flow analysis #2122
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
Conversation
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
RyanCavanaugh
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot you have two code changes but only one test case. Add a second test that hits both cases (or prove to me that both changes were needed)
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
Added second test case
I verified by temporarily removing the second fix - it causes a panic on the second test case. Both test cases now converted to testdata files in commit cc15e4a. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
There was a problem hiding this 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 panic that occurred when a type predicate references a non-existent parameter name. When core.FindIndex cannot find a matching parameter, it returns -1, which was then used to index into arrays without bounds checking, causing a panic.
Key changes:
- Added
parameterIndex >= 0check ingetTypePredicateArgumentbefore array access - Added
parameterIndex >= 0check inisReachableFlowNodeWorkerfor assertion predicates - Added comprehensive test coverage for both regular and assertion predicate scenarios
Reviewed Changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| internal/checker/flow.go | Added bounds checking for parameterIndex in two functions to prevent panic when type predicate parameter names don't match actual function parameters |
| testdata/tests/cases/compiler/typePredicateParameterMismatch.ts | Test case for regular type predicate with mismatched parameter name |
| testdata/tests/cases/compiler/assertsPredicateParameterMismatch.ts | Test case for assertion predicate with mismatched parameter name |
| testdata/baselines/reference/compiler/typePredicateParameterMismatch.* | Expected baseline outputs (types, symbols, errors) for regular type predicate test |
| testdata/baselines/reference/compiler/assertsPredicateParameterMismatch.* | Expected baseline outputs (types, symbols, errors) for assertion predicate test |
|
Fixes #2121 |
jakebailey
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, these are both cases where the old code relied on out of bounds accesses returning undefined
Yay JS
Fixes #2121
When a type predicate references a non-existent parameter name,
parameterIndexis set to -1. Flow analysis code attempted to index into arguments arrays without bounds checking, causing a panic.Changes
getTypePredicateArgument: AddedparameterIndex >= 0check before array access (handles regular type predicates likevalue is Type)isReachableFlowNodeWorker: AddedparameterIndex >= 0check in assertion predicate handling (handles assertion predicates likeasserts conditionwhen checking for unreachable code)typePredicateParameterMismatch.tsandassertsPredicateParameterMismatch.tstest cases intestdata/tests/cases/compiler/to verify both code pathsExamples
Regular type predicate:
Assertion predicate:
Before:
panic: runtime error: index out of range [-1]After:
error TS1225: Cannot find parameter 'value'.(matches tsc behavior)Original prompt
This section details on the original issue you should resolve
<issue_title>Panic: index out of range [-1] on type predicate parameter mismatch</issue_title>
<issue_description>## Description
tsgopanics when a type predicate references a parameter name that doesn't match the function parameter.Reproduction
Run:
tsgo --noEmitExpected
TypeScript error or graceful handling (like
tscdoes).Actual