Repository navigation
fix(arrays): throw TypeError for negative indices in quickSelect - #342816
Joaquín Ruales (jruales) merged 7 commits into
Conversation
|
Hi Alexandru Dima (@alexdima) Benjamin Pasero (@bpasero) Johannes Rieken (@jrieken) roblourens, Could you please take a quick look at this small fix when you have a moment? This PR fixes Thank you! |
|
Looping in Sandeep Somavarapu (@sandy081) Ulugbek Abdullaev (@ulugbekna) Joaquín Ruales (@jruales) Alexandru Dima (@alexdima) Benjamin Pasero (@bpasero) Johannes Rieken (@jrieken) roblourens for visibility on this quick fix for |
Ulugbek Abdullaev (ulugbekna)
left a comment
There was a problem hiding this comment.
Please validate nth before coercing it. nth = nth | 0 turns negative fractions such as -0.5 into 0, so the new nth < 0 check does not uphold the updated bounds contract. Move the negative bounds check before coercion and add a test for a negative fractional value.
| if (nth < 0) { | ||
| throw new TypeError('invalid index'); | ||
| } | ||
|
|
||
| nth = nth | 0; | ||
|
|
||
| if (nth < 0 || nth >= data.length) { | ||
| if (nth >= data.length) { | ||
| throw new TypeError('invalid index'); | ||
| } |
There was a problem hiding this comment.
we should check if nth is valid in a single if statement and then do coercion
There was a problem hiding this comment.
Done! Unified the bounds validation into a single statement (if (nth < 0 || nth >= data.length)) before integer coercion, and added unit tests covering negative fractions (-0.5) as well. Thanks Ulugbek Abdullaev (@ulugbekna)!
|
Hi Ulugbek Abdullaev (@ulugbekna), I have updated the PR according to your feedback:
Whenever you get a chance, please let me know if this looks good. Thank you! |
| throw new TypeError('invalid index'); | ||
| } | ||
|
|
||
| nth = nth | 0; |
There was a problem hiding this comment.
Why was this moved from before the if until after it? If nth is NaN, before it was treated as 0 for the purpose of checking bounds, and now it skips the bound check and only later becomes zero, so if you pass index NaN of an empty array. Before it used to throw with this error, but now it will not
There was a problem hiding this comment.
Just saw the other comment as to why it's in that order.
In that case actually better to check if nth is NaN and also throw in that case.
There was a problem hiding this comment.
Addressed in commit 74b228c. Explicitly added Number.isNaN(nth) to the pre-coercion bounds guard (if (Number.isNaN(nth) || nth < 0 || nth >= data.length)) and added regression tests for quickSelect(NaN, [], ...) and quickSelect(NaN, [1, 2, 3], ...). Thanks Joaquín Ruales (@jruales)!
Joaquín Ruales (jruales)
left a comment
There was a problem hiding this comment.
See previous comment
|
Hi Joaquín Ruales (@jruales) Ulugbek Abdullaev (@ulugbekna), I have updated the PR in commit 74b228c to address the feedback:
Ready for review! Thank you! |
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
3 open issues
The function still coercesnthto an integer via| 0, which means non-integer positive values… · New The function still coercesnthto an integer via| 0, which means non-integer positive values… · Newnth = nth | 0coerces to a signed 32-bit int, which can wrap large valid indices (>= 2^31) into… · New
Review effort: Lite
| throw new TypeError('invalid index'); | ||
| } | ||
|
|
||
| nth = nth | 0; |
There was a problem hiding this comment.
Addressed in 5b5486b. The JSDoc now explicitly documents that non-negative fractional indices are truncated toward zero, and the test suite includes a 0.9 regression case.
|
Joaquín Ruales (@jruales) Ulugbek Abdullaev (@ulugbekna) Hi both, I addressed the three latest review findings in 5b5486b. quickSelect now uses Math.trunc(nth) instead of the signed 32-bit | 0 coercion, so large indices no longer wrap. The JSDoc now explicitly documents the existing behavior for non-negative fractional indices, and a regression test confirms 0.9 selects index 0. The earlier negative fractional and NaN validation coverage remains in place. I also ran whitespace checks and direct runtime assertions for truncation and NaN handling; the full VS Code unit suite requires generated build output that is not present in this sparse checkout, and the PR's VS Code check is running. Could you please take another look when you have a moment? Thank you! |
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
5 open issues
Number.isNaN(nth)doesn’t catch non-number values likeundefined(ornull), which can slip… · Newnth = nth | 0coerces to a signed 32-bit int, which can wrap large valid indices (>= 2^31) into… The function still coercesnthto an integer via| 0, which means non-integer positive values… The function still coercesnthto an integer via| 0, which means non-integer positive values… These new assertions are appended to the median-focused test, which makes the test’s purpose less… · New
Review effort: Lite
| if (Number.isNaN(nth) || nth < 0 || nth >= data.length) { | ||
| throw new TypeError('invalid index'); | ||
| } | ||
|
|
||
| nth = Math.trunc(nth); | ||
|
|
There was a problem hiding this comment.
Addressed in 4164e3b. The guard now uses Number.isFinite(nth), so undefined, null, NaN, and infinities are rejected before truncation. Added regression coverage for each runtime-invalid case.
|
|
||
| const compare = (a: number, b: number) => a - b; | ||
| assert.strictEqual(arrays.quickSelect(0.9, [2, 1, 3], compare), 1); | ||
| assert.throws(() => arrays.quickSelect(-1, [1, 2, 3], compare), TypeError); | ||
| assert.throws(() => arrays.quickSelect(-0.5, [1, 2, 3], compare), TypeError); | ||
| assert.throws(() => arrays.quickSelect(-10, [1, 2, 3], compare), TypeError); | ||
| assert.throws(() => arrays.quickSelect(3, [1, 2, 3], compare), TypeError); | ||
| assert.throws(() => arrays.quickSelect(NaN, [1, 2, 3], compare), TypeError); | ||
| assert.throws(() => arrays.quickSelect(NaN, [], compare), TypeError); | ||
| assert.throws(() => arrays.quickSelect(0, [], compare), TypeError); |
There was a problem hiding this comment.
Addressed in 4164e3b. I moved the fractional and invalid-index assertions into a dedicated quickSelect validates nth test, leaving the median test focused on selection behavior.
|
Joaquín Ruales (@jruales) Ulugbek Abdullaev (@ulugbekna) Hi both, I addressed the remaining open review items in 4164e3b. quickSelect now rejects all non-finite runtime values with Number.isFinite before truncation, and the validation cases now live in their own dedicated test. The three earlier items remain addressed: Math.trunc prevents 32-bit wrapping, positive fractional behavior is documented and tested, and NaN is rejected. I replied directly to each new thread and ran focused runtime assertions plus git diff --check. The full VS Code unit harness requires generated build output not present in this sparse checkout; the PR check is running. Could you please take another look when you have a moment? Thank you! |
|
Thank you for taking the time to review this PR and for your feedback. When you have a chance, could you please consider merging it if everything looks good? I appreciate your time and help! |
|
Alexandru Dima (@alexdima) Please take a look. |



Summary
In
src/vs/base/common/arrays.ts,quickSelect<T>(nth: number, data: T[], compare: Compare<T>)originally validated the index after bitwise coercion:When
nthis negative, such asquickSelect(-1, [1, 2, 3], ...), the upper-bound check passes becausenth >= data.lengthevaluates tofalse. The algorithm then continues recursively with an invalid index, potentially causing infinite recursion and eventually throwingRangeError: Maximum call stack size exceeded.Additionally, bitwise coercion (
nth | 0) truncates fractional values toward zero. For example,-0.5 | 0evaluates to0, masking the invalid negative input instead of rejecting it.The index should be validated before the algorithm proceeds, ensuring that invalid negative and out-of-range indices fail explicitly rather than causing recursive failures.