Fix signed integer min/max bounds in PrimitiveType - #556
Open
arpitjain099 wants to merge 1 commit into
Open
Conversation
For an n-bit two's complement integer the valid range is [-2**(n-1), 2**(n-1) - 1], but the signed branch of PrimitiveType.__init__ set max to 2**(n-1) and min to -(2**(n-1) - 1), which is one step too high on both ends (I8 reported [-127, 128] instead of [-128, 127], and likewise for the 16/32/64-bit signed types). Since validate() uses these bounds, validate(128) on I8 returned True even though encode(128) then fails in struct.pack, and validate(-128) returned False even though -128 is a legal int8. Set max to 2**(n-1) - 1 and derive min as -(max + 1). Added testSignedRange to cover the bounds, validate(), and encode/decode round-trip for each signed type. Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
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.
I do supply-chain security work and was reading through the dtype module when the signed-integer bounds in
PrimitiveType.__init__looked off by one:For an n-bit two's complement integer the range is
[-2**(n-1), 2**(n-1) - 1], so for I8 this reports[-127, 128]instead of[-128, 127](and likewise for the 16/32/64-bit signed types).validate()uses these bounds directly, so today:validate(128)on I8 returns True, butencode(128)then fails instruct.pack(thebformat requires -128..127). validate is meant to catch that.validate(-128)on I8 returns False, even though -128 is a legal int8 that encodes fine.The fix sets max to
2**(n-1) - 1and derives min as-(max + 1). The unsigned and float branches are unchanged.I added
tests/ait/core/test_dtype.py::testSignedRange, which checks the bounds for each signed type and confirms the true min and max both validate and round-trip through encode/decode, while one past each end is rejected. Nothing in the existing dtype tests asserts these bounds, so this does not change any current expectation.pytest tests/ait/core/test_dtype.pyis green with the new test included.