fix: doc comments regarding safety for Number.toU32()#27
Merged
Conversation
Discovered this while working on ChainSafe/lodestar-z#349. `napi_get_value_uint32` makes no guarantee of erroring on values out of bounds: > Let int32bit be int modulo 2**32. Source: https://tc39.es/ecma262/#sec-touint32 This little code snippet PoC when added to `BeaconStateView.test.ts` in lodestar-z passes: ```ts it("toU32 silently wraps around", () => { let i = 0; for (i = 0; i < 100; i++) { const a = state.getBalance(0 + i); const b = state.getBalance(2 ** 32 + i); expect(a).toBe(b); } }); ``` While this fails, but only because we're access the SSZ list index out of bounds: ```ts state.getBalance(-1) ``` ``` FAIL bindings/test/beaconStateView.test.ts > BeaconStateView > probe truncation behavior Error: IndexOutOfBounds ``` Other conversion methods might behave the same; i haven't fully checked, but we should be careful of this
nazarhussain
approved these changes
May 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Discovered this while working on ChainSafe/lodestar-z#349.
napi_get_value_uint32makes no guarantee of erroring on values out of bounds:Source: https://tc39.es/ecma262/#sec-touint32
This little code snippet PoC when added to
BeaconStateView.test.tsin lodestar-z passes:While this fails, but only because we're access the SSZ list index out of bounds:
Other conversion methods might behave the same; i haven't fully checked, but we should be careful of this