-
Notifications
You must be signed in to change notification settings - Fork 94
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
Deserialize arrays: avoid leaking partially allocated memory when failing #825
Conversation
fuel-types/src/canonical.rs
Outdated
let final_ptr = &mut uninit as *mut _ as *mut [T; N]; | ||
// SAFETY: All array elements have been initialized above. | ||
let init = unsafe { uninit.assume_init() }; | ||
let init = unsafe { final_ptr.read() }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is ugly, but mem::transmute
cannot be used for arrays with generic length.
See rust-lang/rust#61956
(The solution advocated there also mentions calling mem::forget(uninit)
, but I don't think in this case is necessary and apparently it causes a compilation error.
@@ -430,23 +430,40 @@ impl<const N: usize, T: Deserialize> Deserialize for [T; N] { | |||
} else { | |||
// Spec doesn't say how to deserialize arrays with unaligned | |||
// primitives(as `u16`, `u32`, `usize`), so unpad them. | |||
let mut uninit = <MaybeUninit<[T; N]>>::uninit(); | |||
let mut uninit: [MaybeUninit<T>; N] = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May I suggest this pattern instead:
fuel-vm/fuel-compression/src/impls.rs
Lines 128 to 154 in dfab064
// which do not require initialization. | |
let mut tmp: [MaybeUninit<T>; S] = unsafe { MaybeUninit::uninit().assume_init() }; | |
let mut i = 0; | |
while i < c.len() { | |
match T::decompress_with(&c[i], ctx).await { | |
Ok(value) => { | |
// SAFETY: MaybeUninit can be safely overwritten. | |
tmp[i].write(value); | |
} | |
Err(e) => { | |
// Drop the already initialized elements, so we don't leak the memory | |
for initialized_item in tmp.iter_mut().take(i) { | |
// Safety: First i elements have been initialized successfully. | |
unsafe { | |
initialized_item.assume_init_drop(); | |
} | |
} | |
return Err(e); | |
} | |
} | |
i += 1; | |
} | |
// SAFETY: Every element is initialized. | |
let result = tmp.map(|v| unsafe { v.assume_init() }); | |
Ok(result) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 741007f
fuel-types/src/canonical.rs
Outdated
@@ -491,7 +505,7 @@ impl<'a> Input for &'a [u8] { | |||
|
|||
fn peek(&self, into: &mut [u8]) -> Result<(), Error> { | |||
if into.len() > self.len() { | |||
return Err(Error::BufferIsTooShort) | |||
return Err(Error::BufferIsTooShort); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you revert all changes related to ;
please?:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This happens automatically when I run cargo +nightly fmt --all.
Is there anything I need to add to the rustfmt configuration?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done (it also caught a trailing semicolon in another file, I will keep that change). See 81ebf29
Closes #811
[Short description of the changes.]
Checklist
Before requesting review
After merging, notify other teams
[Add or remove entries as needed]