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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The way to guarantee the the safety of alignment issues #1096

Open
shinmao opened this issue Aug 18, 2023 · 0 comments
Open

The way to guarantee the the safety of alignment issues #1096

shinmao opened this issue Aug 18, 2023 · 0 comments

Comments

@shinmao
Copy link

shinmao commented Aug 18, 2023

grcov/src/reader.rs

Lines 169 to 177 in 322fc39

($ty: ty, $buf: expr) => {{
let size = std::mem::size_of::<$ty>();
let start = $buf.pos;
$buf.pos += size;
if $buf.pos <= $buf.buffer.len() {
let val: $ty = unsafe {
// data are aligned so it's safe to do that
#[allow(clippy::transmute_ptr_to_ptr)]
*std::mem::transmute::<*const u8, *const $ty>($buf.buffer[start..].as_ptr())

Hi, I am interested in the way you guarantee the safety of alignment issues. Take read_u32 for example, we are worried that the data is aligned to 1 byte rather than 4 bytes here. Do you use the offset starting from 4 bytes with buffer[start..] to solve the issue?

In other words, if we use transmute($buf.buffer.as_ptr()), then it is still dangerous?

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

No branches or pull requests

1 participant