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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

u8(255.0) yields Overflow #23

Closed
federicomenaquintero opened this issue Mar 5, 2021 · 1 comment 路 Fixed by #42
Closed

u8(255.0) yields Overflow #23

federicomenaquintero opened this issue Mar 5, 2021 · 1 comment 路 Fixed by #42

Comments

@federicomenaquintero
Copy link

use cast::u8;

fn main() {
    println!("{:?}", u8(255.0)); // yields Overflow
}

It should be Ok(255), of course 馃槂

federicomenaquintero added a commit to federicomenaquintero/cast.rs that referenced this issue Mar 5, 2021
It looks like all of {u8, u16, u32}::MAX values as f64 yield Overflow
when cast back to their original types.
federicomenaquintero added a commit to federicomenaquintero/cast.rs that referenced this issue Mar 5, 2021
It looks like all of {u8, u16, u32}::MAX values as f64 yield Overflow
when cast back to their original types.
gnomesysadmins pushed a commit to GNOME/librsvg that referenced this issue Mar 5, 2021
Aaaaaaaaand, we found a bug in the cast crate!
japaric/cast.rs#23
@oherrala
Copy link

This conversion error happens on line 299 of lib.rs where the transmutes returns a value 254.99999999999997 for max and then on line 301 src is compared to max as 255 > 254.99999999999997 which of course is true and the whole cast functions return Error::Overflow on line 303.

cast.rs/src/lib.rs

Lines 293 to 304 in cd85b66

} else if {
// we subtract 1 ULP (unit of least precision) here because some
// lossy conversions like `u64::MAX as f64` round *up* and we want
// to avoid this evaluating to false in that case
use core::mem::transmute;
let max = unsafe {
transmute::<_, $src>(transmute::<_, $usrc>($dst::MAX as $src) - 1)
};
src > max
} {
Error::Overflow
} else if $dst::MIN == 0 {

This bug was introduced in 3f0bf8b to fix issue #15.

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 a pull request may close this issue.

2 participants