Commit 1169724
fix index overflow in enumerate with large start
Summary:
**Index overflow in `enumerate` with a large start**
The per-element index is built as `k as i32 + start`, so when the caller passes a `start` near `i32::MAX` the addition overflows on the first elements rather than at some unreachable list length. Cause is doing the arithmetic in `i32` even though Starlark integers are arbitrary precision, so the correct result does not fit. Fix widens the running index to `i64` before adding the offset.
`enumerate(['a', 'b'], 2147483647)` panics with "attempt to add with overflow" in debug and wraps to a negative index in release; it should yield `[(2147483647, 'a'), (2147483648, 'b')]`. Added a regression test alongside the existing builtin tests.
Should be safe as the index can no longer exceed `i64` for any in-memory iterable, though a reviewer may prefer matching whatever width the range work settled on.
X-link: facebook/starlark-rust#203
Reviewed By: JakobDegen
Differential Revision: D108556073
fbshipit-source-id: 2d2b80304aba92dc806f4338e8227c143820fc691 parent 237a56c commit 1169724
1 file changed
Lines changed: 9 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
166 | 166 | | |
167 | 167 | | |
168 | 168 | | |
169 | | - | |
| 169 | + | |
170 | 170 | | |
171 | 171 | | |
172 | 172 | | |
| |||
413 | 413 | | |
414 | 414 | | |
415 | 415 | | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
416 | 424 | | |
417 | 425 | | |
418 | 426 | | |
| |||
0 commit comments