-
Notifications
You must be signed in to change notification settings - Fork 441
Remove redundant unsafe in talpid-net
#9452
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
Conversation
hulthe
left a comment
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.
Reviewable status: 0 of 1 files reviewed, 1 unresolved discussion (waiting on @MarkusPettersson98)
talpid-net/src/unix.rs line 59 at r1 (raw file):
let interface_name = interface.as_bytes(); if interface_name.len() > nix::libc::IF_NAMESIZE { return Err(invalid_input("Interface name too long"));
I think, based on this SO-thread i found, that you need to leave space for a null bytes at the end. So this is the correct comparison (note the >=):
// Check that the name (plus a null byte) fits into IF_NAMESIZE
if interface_name.len() >= nix::libc::IF_NAMESIZE {
return Err(invalid_input("Interface name too long"));
}
hulthe
left a comment
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.
Reviewable status: 0 of 1 files reviewed, 2 unresolved discussions (waiting on @MarkusPettersson98)
talpid-net/src/unix.rs line 64 at r1 (raw file):
let mut ifr: ifreq = unsafe { mem::zeroed() }; // SAFETY: `interface_name.len()` does not exceed IF_NAMESIZE and `interface_name` only // contains ASCII.
Write something about the null terminator here also maybe
MarkusPettersson98
left a comment
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.
Reviewable status: 0 of 1 files reviewed, 1 unresolved discussion (waiting on @hulthe)
talpid-net/src/unix.rs line 59 at r1 (raw file):
Previously, hulthe (Joakim Hulthe) wrote…
I think, based on this SO-thread i found, that you need to leave space for a null bytes at the end. So this is the correct comparison (note the
>=):// Check that the name (plus a null byte) fits into IF_NAMESIZE if interface_name.len() >= nix::libc::IF_NAMESIZE { return Err(invalid_input("Interface name too long")); }
Good catch! I interpreted the netdevice source code the same way: https://www.man7.org/linux/man-pages/man7/netdevice.7.html
talpid-net/src/unix.rs line 64 at r1 (raw file):
Previously, hulthe (Joakim Hulthe) wrote…
Write something about the null terminator here also maybe
Done
hulthe
left a comment
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.
I feel like this could even be upstreamed to nix
@hulthe reviewed 1 of 1 files at r2, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @MarkusPettersson98)
talpid-net/src/unix.rs line 74 at r2 (raw file):
Domain::IPV4
I know you didn't change this, but does it really work for ipv6?
e2da9d9 to
e4cf13a
Compare
MarkusPettersson98
left a comment
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.
<3 I might look into it! It would be a very welcome addition:-)
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @hulthe)
talpid-net/src/unix.rs line 74 at r2 (raw file):
Previously, hulthe (Joakim Hulthe) wrote…
Domain::IPV4
I know you didn't change this, but does it really work for ipv6?
That's a great question. I'll run some checks and try to dig into it.
This PR aims to write
talpid-net/src/unix.rsin a more idiomatic way by wrapping the call toioctlin a struct with a vetted constructor (IfReq::new) and safe methods. I managed to consolidate someunsafeblocks as well, which reduced the number ofunsafeblocks from 7 to 5.:)This change is