Skip to content

Assert nbytes <= 8 in sign_extend - #862

Merged
GlenDC merged 1 commit into
tokio-rs:masterfrom
princejain756:fix-sign-extend-assert
Nov 9, 2026
Merged

GlenDC merged 1 commit into
tokio-rs:masterfrom
princejain756:fix-sign-extend-assert

Conversation

@princejain756

Copy link
Copy Markdown
Contributor

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.

@GlenDC GlenDC left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@GlenDC

GlenDC commented Nov 9, 2026

Copy link
Copy Markdown
Member

@princejain756 can you rebase your branch onto master ? It's a required check and that CI problem is fixed in master. Do let me know if you have no time for this any longer, I would understand.

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.
@princejain756
princejain756 force-pushed the fix-sign-extend-assert branch from ea33f37 to bad9986 Compare November 9, 2026 21:10
@princejain756

Copy link
Copy Markdown
Contributor Author

Thanks for the review @GlenDC! Just rebased the branch onto the latest master so the updated CI workflow picks it up.

@GlenDC
GlenDC merged commit f2cf483 into tokio-rs:master Nov 9, 2026
18 checks passed
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

2 participants