-
Notifications
You must be signed in to change notification settings - Fork 654
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
Invalid transmute from std::net::Ipv4Addr
to libc::in_addr
#2053
Comments
Looks like these functions will have to be rewritten to perform a more expensive conversion rather than simply transmuting. |
Annoying. I guess we'll have to change it. However, it looks like the standard library's new layout is still the same as the C layout, at least on common platforms. Do you know of any real platforms where it isn't? |
Rust's standard library no longer guarantees that Ipv4Addr and Ipv6Addr are wrappers around the C types (though for now at least, they are identical on all platforms I'm aware of). So do the conversions explicitly instead of transmuting. Fixes nix-rust#2053
Rust's standard library no longer guarantees that Ipv4Addr and Ipv6Addr are wrappers around the C types (though for now at least, they are identical on all platforms I'm aware of). So do the conversions explicitly instead of transmuting. Fixes nix-rust#2053
I don't know of any. Rust is free to do whatever it wants for the layout (and the layout isn't guaranteed to be the same even across builds), but it's probably unlikely that the rust representation of But on the other hand, I would expect libc::in_addr {
s_addr: u32::from_be_bytes(addr.octets()).to_be()
} to get optimized to essentially a no-op anyway without a need for transmute. For example on godbolt: pub fn convert(addr: std::net::Ipv4Addr) -> u32 {
u32::from_be_bytes(addr.octets()).to_be()
} gets compiled to just
|
2061: For invalid IP address conversions with future Rust versions r=asomers a=asomers Rust's standard library no longer guarantees that Ipv4Addr and Ipv6Addr are wrappers around the C types (though for now at least, they are identical on all platforms I'm aware of). So do the conversions explicitly instead of transmuting. Fixes #2053 Co-authored-by: Alan Somers <asomers@gmail.com>
2061: For invalid IP address conversions with future Rust versions r=asomers a=asomers Rust's standard library no longer guarantees that Ipv4Addr and Ipv6Addr are wrappers around the C types (though for now at least, they are identical on all platforms I'm aware of). So do the conversions explicitly instead of transmuting. Fixes nix-rust#2053 Co-authored-by: Alan Somers <asomers@gmail.com>
2061: For invalid IP address conversions with future Rust versions r=asomers a=asomers Rust's standard library no longer guarantees that Ipv4Addr and Ipv6Addr are wrappers around the C types (though for now at least, they are identical on all platforms I'm aware of). So do the conversions explicitly instead of transmuting. Fixes nix-rust#2053 Co-authored-by: Alan Somers <asomers@gmail.com>
2061: For invalid IP address conversions with future Rust versions r=asomers a=asomers Rust's standard library no longer guarantees that Ipv4Addr and Ipv6Addr are wrappers around the C types (though for now at least, they are identical on all platforms I'm aware of). So do the conversions explicitly instead of transmuting. Fixes nix-rust#2053 Co-authored-by: Alan Somers <asomers@gmail.com>
The functions
ipv4addr_to_libc
andipv6addr_to_libc
transmute fromstd::net::Ipv4Addr
tolibc::in_addr
, but this is unsound.nix/src/sys/socket/addr.rs
Lines 39 to 54 in 89b4976
The rust socket address types do not have the same layouts as their corresponding libc types, even if their size is the same.
See rust-lang/rust#78802 for details.
The text was updated successfully, but these errors were encountered: