Skip to content

Draft backward incompatible: proposal to address UB FixedInt - #30

Merged
dermesser merged 2 commits into
dermesser:masterfrom
jorgecarleitao:fix_ub
Sep 30, 2022
Merged

Draft backward incompatible: proposal to address UB FixedInt#30
dermesser merged 2 commits into
dermesser:masterfrom
jorgecarleitao:fix_ub

Conversation

@jorgecarleitao

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

@dermesser

Copy link
Copy Markdown
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
Copy Markdown
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
Comment thread src/fixed.rs
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
Copy Markdown
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
Copy Markdown
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.

Comment thread src/fixed.rs
dst.clone_from_slice(&self.to_le_bytes());
}

#[cfg(target_endian = "little")]

Copy link
Copy Markdown
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