mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-23 06:10:02 +08:00
Encoding an array key added each element's size to a running length with unchecked arithmetic, and wrote into a fixed stack buffer whenever the sum fit. A sparse array with a large length (2^29 elements or more) wrapped the sum on 32-bit targets, so the write landed before the buffer; on every target the encoder also visited every index of such an array. Array keys now have at most 65536 elements, and their encoding at most 2^31 - 1 bytes. The size is computed first with sums that cannot wrap, the buffer is sized to it, and keys over either limit make the lookup or insert throw a TypeError before anything is read or written. The Node.js implementation throws the same TypeError for keys of more than 65536 elements. Sparse elements are walked in one pass in index order, and the index growth computation checks its size arithmetic too.