Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft backward incompatible: proposal to address UB FixedInt #30

Merged
merged 2 commits into from
Sep 30, 2022

Conversation

jorgecarleitao
Copy link
Contributor

  • Remove FixedInt::REQUIRED_SPACE
  • Added FixedInt::Bytes
  • Remove fn required_space
  • Made switch_endianness implementation required
  • Add forbid(unsafe_code)

@dermesser
Copy link
Owner

Thank you, your changes clean up a bunch of old work-arounds back from (I checked blame) around Rust 1.13. It's definitely nicer like this.

@dermesser
Copy link
Owner

Hey - are you interested in getting this merged? I have one or another idea on how to improve this further, but I really like your proposed structure.

@dermesser dermesser marked this pull request as ready for review September 29, 2022 07:23

/// `FixedInt` provides encoding/decoding to and from fixed int representations.
///
/// The emitted bytestring contains the bytes of the integer in machine endianness.
pub trait FixedInt: Sized + Copy {
const REQUIRED_SPACE: usize;
/// Returns how many bytes are required to represent the given type.
fn required_space() -> usize;
Copy link
Owner

Choose a reason for hiding this comment

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

Why do you remove the required_space function/constant? I agree that it is slightly cludgey, but the intent is that it gives easy access to the size of an encoded integer when using the encode_fixed() and decode_fixed() methods (e.g. when incrementally parsing a packet). Or will we tell users to simply use mem::size_of?

Copy link
Owner

Choose a reason for hiding this comment

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

nevermind; mem::size_of probably takes care of this best.


dst.clone_from_slice(encoded);
assert_eq!(dst.len(), size_of::<Self>());
dst.clone_from_slice(&self.to_le_bytes());
}

#[cfg(target_endian = "little")]
Copy link
Owner

Choose a reason for hiding this comment

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

I believe it was a mistake to introduce the target-dependent methods here. The byte order of some encoded data doesn't need to match the target endianness after all. (This is a note to myself, nothing you need to fix here.)

@dermesser dermesser merged commit 60d869e into dermesser:master Sep 30, 2022
dermesser added a commit that referenced this pull request Jun 30, 2023
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.

2 participants