Skip to content

Assert nbytes <= 8 in sign_extend - #862

Open
princejain756 wants to merge 1 commit into
tokio-rs:masterfrom
princejain756:fix-sign-extend-assert
Open

princejain756 wants to merge 1 commit into
tokio-rs:masterfrom
princejain756:fix-sign-extend-assert

Conversation

@princejain756

Copy link
Copy Markdown

Fixes #832

sign_extend computes (8 - nbytes) * 8, which underflows when nbytes > 8. All callers (get_int, get_int_le, try_get_int, try_get_int_le) already bound nbytes to at most 8 via get_uint / try_get_uint, while nbytes == 0 is handled by the existing early return.

This change adds debug_assert!(nbytes <= 8, "nbytes must be in [0, 8]") in sign_extend along with unit tests covering nbytes == 0 and nbytes > 8 in debug builds.

Add a debug_assert!(nbytes <= 8) check in sign_extend so out-of-range byte counts fail immediately in debug builds instead of underflowing 8 - nbytes, while keeping the nbytes == 0 branch intact.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sign_extend() potential shift underflow for nbytes > 8

1 participant