fix(bindings): replace bare as usize casts in Tokio I/O callbacks#5780
Merged
WesleyRosenblum merged 1 commit intomainfrom Mar 10, 2026
Merged
fix(bindings): replace bare as usize casts in Tokio I/O callbacks#5780WesleyRosenblum merged 1 commit intomainfrom
WesleyRosenblum merged 1 commit intomainfrom
Conversation
alexw91
approved these changes
Mar 9, 2026
maddeleine
approved these changes
Mar 10, 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.
Goal
Replace unsafe
len as usizecasts with checkedlen.try_into().unwrap()in the Tokio I/O callbacks (recv_io_cbandsend_io_cb).Why
The
recv_io_cbandsend_io_cbcallbacks receive alen: u32from the C library and cast it directly tousizewithlen as usizewhen constructing slices viastd::slice::from_raw_parts_mutandstd::slice::from_raw_parts. On 64-bit platforms this is always safe, but on a hypothetical platform whereusizeis smaller thanu32, the cast would silently truncate, creating a slice smaller than the C library expects and leading to a buffer overread/overwrite.How
Changed both
recv_io_cbandsend_io_cbto convertlenusinglen.try_into().unwrap()instead oflen as usize. This will panic with a clear error message instead of silently truncating if the value ever doesn't fit inusize.This matches the existing pattern used throughout the s2n-tls Rust bindings (e.g.,
connection.rs,cert_chain.rs,renegotiate.rs).Callouts
u32always fits inusize, so the behavior is unchanged.unwrap()is acceptable here because a failure would indicate a fundamentally unsupported platform, and there is no reasonable recovery path inside anextern "C"callback.Testing
Existing tests cover these code paths. No new tests needed since the behavior is identical on all supported (64-bit) platforms.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.