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

Fix badness around fixed-size array decoding #29

Open
jsgf opened this issue Aug 2, 2017 · 0 comments
Open

Fix badness around fixed-size array decoding #29

jsgf opened this issue Aug 2, 2017 · 0 comments
Assignees

Comments

@jsgf
Copy link
Owner

jsgf commented Aug 2, 2017

The generated code for fixed size arrays is currently:

    let mut buf: [#ty; #value as usize] = unsafe { ::std::mem::uninitialized() };
    let sz = xdr_codec::unpack_array(input, &mut buf[..], #value as usize, None)?;
    (buf, sz)

This is unsafe because the array is uninitialized, and unpack_array will replace each field as it unpacks each element - ie, it will try to destruct the old (uninitialized) value.

The safe way to do this is with std::ptr::write, which simply overwrites the pointer without dropping it:

    fn uninit_ptr_setter<T>(p: &mut T, v: T) {
        unsafe { ::std::ptr::write(p as *mut T, v) }
    }
    let mut buf: [#ty; #value as usize] = unsafe { ::std::mem::uninitialized() };
    let sz = xdr_codec::unpack_array_with(input, &mut buf[..], #value as usize, uninit_ptr_setter, None)?;
    (buf, sz)

but this still has problems if unpack_array_with() fails and leaves the array partially initialized - it's Drop will end up trying to destruct uninitialized elements.

@jsgf jsgf self-assigned this Aug 2, 2017
jsgf added a commit that referenced this issue Aug 2, 2017
Partial fix, still problem on failure.

Issue #29
jsgf added a commit that referenced this issue Aug 3, 2017
Partial fix, still problem on failure.

Issue #29
jsgf added a commit that referenced this issue Aug 3, 2017
Partial fix, still problem on failure.

Issue #29
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