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

Panic unwinding in Buffer::execute. #55

Open
leunga opened this issue May 3, 2024 · 0 comments
Open

Panic unwinding in Buffer::execute. #55

leunga opened this issue May 3, 2024 · 0 comments

Comments

@leunga
Copy link

leunga commented May 3, 2024

This crate went through a safety review at work and I received the following minor comments from a Rust expert:

#[cfg(target_endian = "big")]
#[inline]
fn execute<F: FnOnce(&mut [u8])>(&mut self, offset: usize, len: usize, f: F) {
    fn swap_endianess(buffer: &mut [u64]) {
        for item in buffer {
            *item = item.swap_bytes();
        }
    }

    let start = offset / 8;
    let end = (offset + len + 7) / 8;
    swap_endianess(&mut self.0[start..end]);
    let buffer: &mut [u8; WORDS * 8] = unsafe { core::mem::transmute(&mut self.0) };  
    f(&mut buffer[offset..][..len]);
    swap_endianess(&mut self.0[start..end]);

"... they do an in-place swap twice instead of just copying the buffer. But I guess the borrow checker makes it impossible for it to be a data race?

I guess if f panics it'll leave this object in an inconsistent state if things unwind. That seems bad, but it's not a safety issue per se. Probably still worth reporting that upstream?"

Just want you to know in case it is an issue.

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