-
Notifications
You must be signed in to change notification settings - Fork 25
fix: replace transmute UB in error constructors with safe alternatives #58
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
Changes from 1 commit
480d584
a91605d
f031067
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,35 @@ | ||
| //! Module with hacks for creating error variants for standard library errors | ||
| //! without public interfaces. | ||
| //! Module with safe helpers for creating error variants for standard library | ||
| //! errors without public constructors. | ||
|
|
||
| use core::{ | ||
| mem, | ||
| num::{IntErrorKind, ParseIntError, TryFromIntError}, | ||
| }; | ||
| use core::num::{IntErrorKind, ParseIntError, TryFromIntError}; | ||
|
|
||
| /// Returns a `ParseIntError` from an `IntErrorKind`. | ||
| pub const fn pie(kind: IntErrorKind) -> ParseIntError { | ||
| unsafe { mem::transmute(kind) } | ||
| /// Returns a `ParseIntError` with the specified `IntErrorKind`. | ||
| pub fn pie(kind: IntErrorKind) -> ParseIntError { | ||
| match kind { | ||
| IntErrorKind::Empty => u8_parse_error(""), | ||
| IntErrorKind::InvalidDigit => u8_parse_error("?"), | ||
| IntErrorKind::PosOverflow => u8_parse_error("256"), | ||
| IntErrorKind::NegOverflow => i8_parse_error("-129"), | ||
| IntErrorKind::Zero => zero_parse_error(), | ||
| _ => u8_parse_error("?"), // fallback for future variants | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, switched to |
||
| } | ||
| } | ||
|
|
||
| fn u8_parse_error(s: &str) -> ParseIntError { | ||
| s.parse::<u8>().unwrap_err() | ||
| } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we use const fn u8_parse_error(s: &str) -> std::num::ParseIntError {
let Err(err) = u8::from_str_radix(s, 10) else {
panic!("not a parse error!");
};
err
}
fn main() {
let x = u8_parse_error("-1");
println!("{x:?}!");
}Same for the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice, |
||
|
|
||
| fn i8_parse_error(s: &str) -> ParseIntError { | ||
| s.parse::<i8>().unwrap_err() | ||
| } | ||
|
|
||
| fn zero_parse_error() -> ParseIntError { | ||
| "0".parse::<core::num::NonZeroU8>().unwrap_err() | ||
| } | ||
|
|
||
| /// Returns a `TryFromIntError`. | ||
| pub const fn tfie() -> TryFromIntError { | ||
| unsafe { mem::transmute(()) } | ||
| pub fn tfie() -> TryFromIntError { | ||
| u8::try_from(-1i8).unwrap_err() | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Zerois only used in the test below, so it can be removed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed. Also dropped the
NonZeroU32import that was only there for the Zero test.