Skip to content
This repository has been archived by the owner on Oct 26, 2022. It is now read-only.

Various netlink-proto Encoder impl fixes #168

Merged
merged 3 commits into from
Jun 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 11 additions & 29 deletions netlink-proto/src/codecs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt::Debug, io, marker::PhantomData, slice};
use std::{fmt::Debug, io, marker::PhantomData};

use bytes::{BufMut, BytesMut};
use netlink_packet_core::{
Expand Down Expand Up @@ -147,43 +147,25 @@ where

fn encode(&mut self, msg: NetlinkMessage<T>, buf: &mut BytesMut) -> Result<(), Self::Error> {
let msg_len = msg.buffer_len();
// FIXME: we should have a max length for the buffer
while buf.remaining_mut() < msg_len {
let new_len = buf.len() + 2048;
buf.resize(new_len, 0);
}
let size = msg.buffer_len();
if buf.remaining_mut() < size {
if buf.remaining_mut() < msg_len {
// BytesMut can expand till usize::MAX... unlikely to hit this one.
return Err(io::Error::new(
io::ErrorKind::Other,
format!(
"message is {} bytes, but only {} bytes left in the buffer",
size,
msg_len,
buf.remaining_mut()
),
));
}

// Safety: we initialize the buffer we're passing to
// NetlinkMessage::serialize(). In theory, `serialize()`
// should be safe because it's not supposed to _read_ from
// the buffer, which is potentially
// un-initialized. However, since we delegate the actual
// implementation to users, we cannot guarantee
// anything. Therefore we have to initialize the buffer
// here.
let bytes = &mut buf.chunk_mut()[..size];
for i in 0..bytes.len() {
bytes.write_byte(i, 0);
}

unsafe {
let initialized_bytes = slice::from_raw_parts_mut(bytes.as_mut_ptr(), bytes.len());

msg.serialize(initialized_bytes);
trace!(">>> {:?}", msg);
buf.advance_mut(size);
}
// As NetlinkMessage::serialize needs an initialized buffer anyway
// no need for any `unsafe` magic.
let old_len = buf.len();
let new_len = old_len + msg_len;
buf.resize(new_len, 0);
msg.serialize(&mut buf[old_len..][..msg_len]);
trace!(">>> {:?}", msg);
Ok(())
}
}