diff options
author | Simon Thoby <git@nightmared.fr> | 2023-02-26 00:09:39 +0100 |
---|---|---|
committer | Simon Thoby <git@nightmared.fr> | 2023-02-26 00:09:39 +0100 |
commit | e5c2b423473bb147763c8f6a73aec73212980e4b (patch) | |
tree | 456763343d97fba976f9c3aa69033ad3dfa5e999 /src/parser.rs | |
parent | c7ef0c961cc8c7a7955754a55df3118c74e8bef7 (diff) |
reduce the amount of unsafe code
Diffstat (limited to 'src/parser.rs')
-rw-r--r-- | src/parser.rs | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/src/parser.rs b/src/parser.rs index 6ea34c1..c8667e3 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -105,14 +105,10 @@ pub fn parse_nlmsg<'a>(buf: &'a [u8]) -> Result<(nlmsghdr, NlMsg<'a>), DecodeErr /// Write the attribute, preceded by a `libc::nlattr` // rewrite of `mnl_attr_put` -pub unsafe fn write_attribute<'a>( - ty: NetlinkType, - obj: &impl NfNetlinkAttribute, - mut buf: *mut u8, -) { - let header_len = pad_netlink_object::<libc::nlattr>(); +pub fn write_attribute<'a>(ty: NetlinkType, obj: &impl NfNetlinkAttribute, mut buf: &mut [u8]) { + let header_len = pad_netlink_object::<nlattr>(); // copy the header - *(buf as *mut nlattr) = nlattr { + let header = nlattr { // nla_len contains the header size + the unpadded attribute length nla_len: (header_len + obj.get_size() as usize) as u16, nla_type: if obj.is_nested() { @@ -121,7 +117,12 @@ pub unsafe fn write_attribute<'a>( ty }, }; - buf = buf.offset(pad_netlink_object::<nlattr>() as isize); + + unsafe { + *(buf.as_mut_ptr() as *mut nlattr) = header; + } + + buf = &mut buf[header_len..]; // copy the attribute data itself obj.write_payload(buf); } |