fix: validate sparse array metadata#80
Conversation
Reject sparse array metadata when the declared length would truncate already decoded entries. Add regression coverage for small and maximum-size malformed sparse array metadata from issue #67.
Greptile SummaryThis PR fixes a silent data-truncation bug where a
Confidence Score: 5/5Safe to merge — the change is a targeted guard added at both resize call sites with no modifications to the unflattening or deserialization logic. The validation logic is correct: Object.keys on the decoded array returns only set numeric indices, the !Number.isInteger(index) branch correctly skips any non-numeric property keys, and the index < length comparison accurately distinguishes safe indices from ones that would be chopped off. Both the basic-truncation and exact-boundary edge cases are covered by the new tests. The resizeArray signature change is mechanical and all callers have been updated. No files require special attention.
|
| Filename | Overview |
|---|---|
| src/decode.ts | Adds validateSparseArrayLength helper that walks Object.keys before every array resize and throws a descriptive TypeError when a declared sparse-array length would truncate an existing index; resizeArray signature gains a path argument to thread into the error message. |
| test/roundtrip.test.ts | Adds two regression tests: one for the basic truncation case (array:2 vs index 5) and one for the exact-boundary case (array:100000 vs index 100000), both asserting the new TypeError message. |
| .changeset/validate-sparse-array-metadata.md | New patch-level changeset entry describing the decoder bug fix. |
Reviews (1): Last reviewed commit: "fix: validate sparse array metadata" | Re-trigger Greptile
Summary
array:<length>would truncate decoded entries.array:2withitems[5]andarray:100000withitems[100000].Closes #67.
Testing
bun fmtbun lint:fixbun testbun typecheck