Repository navigation
fix(cli): reject a --batch-size that is not a positive integer - #1384
alex-powell wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
The focused validation addresses the reported failures, preserves default handling, and includes targeted regression tests.
No open issues 🎉
What changed in this PR
Adds early validation for --batch-size to prevent invalid values from causing runaway memory allocation or misleading OSV errors.
Changes:
- Rejects zero, negative, fractional, and non-numeric values with a clear error.
- Preserves the default behavior for omitted or empty values.
- Adds regression tests for invalid inputs and valid positive integers.
| File | Description |
|---|---|
| tests/validate.test.ts | Adds rejection and acceptance cases for batch-size validation. |
| src/cli/validate.ts | Validates non-empty batch-size values before scanning. |
Review effort: Balanced
sonukapoor
left a comment
There was a problem hiding this comment.
Approving. The failure mode here is worse than it reads on paper.
--batch-size 0 and --batch-size -1 do not error, they exhaust the heap. The chunker steps its index by whatever size it is given, so zero stays where it is and a negative walks backward, and both loop until the process dies at exit code 134. --batch-size abc is stranger: it exits 1 having printed advice about installing a corporate certificate authority, so the first thing a user sees after a typo is an instruction to reconfigure their network.
Your guard rejects all of that up front with one message that names the flag, and a valid value still scans normally. I also tried 1.5 and 10abc, which the digits-only test catches, and an empty value, which correctly falls through to the default.
The comment explaining why both zero and a negative loop is the part I would have left out myself, and it is the reason the next person will not weaken this into a simple non-zero check.
Merge result clean against current main at 177 suites and 2,471 tests. Removing the guard fails 5 of the 41 tests in that file and leaves 36 passing.
One note rather than a change request: the certificate advice appearing at all is not something #1336 should have caught, because that path genuinely routes as a network error rather than a flag error. Stopping it at the parser instead is the right answer.
Fixes #1383
What
validateOptionsnow rejects a--batch-sizethat is not a positive integer, with--batch-size must be a positive integerand exit 1. An omitted or empty value still falls back to the default of 100.Why
chunk()advances withi += size, so a size of0never advances and a negative size walks backward. Either one pushes slices until Node aborts with a heap out of memory crash. Non-numeric input becameNaNand ended in a misleading OSV/CA certificate error.Before
After
Tests
tests/validate.test.ts:0,-1,abc,1.5,10abcrejected;1,100accepted. They failed before the change.npm run buildand fullnpm testpass.The check lives in
validate.tsrather thanargs.ts, so it does not overlap with #1382.