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

Serialization is potentially unsound since Vec::set_len is called before the vector is initialized #78

Open
udoprog opened this issue Dec 26, 2024 · 0 comments

Comments

@udoprog
Copy link

udoprog commented Dec 26, 2024

Hi.

So I'm worried about this code location, and places like it:

vec.set_len( capacity );

The safety per-requisite for calling set_len is that the elements at old_len..new_len must be initialized. We could possibly overlook this since we have full control of the vector, however calling as_mut_slice is also potentially problematic, since internally it uses from_raw_parts_mut which also requires all elements to be properly initialized. Note that miri only triggers if we observe uninitialized bytes but that doesn't mean writing to uninitialized memory like this is sound.

That leaves me with my final concern. If the writer errors for any reason the final call to set_len will not happen. I haven't investigated where this might happen in practice, but since Writable is a public trait it doesn't really matter since anyone can cause it to error.

A simple solution would be to have BufferCollector operate over pointers instead and use methods such as copy_to_nonoverlapping which does not require the destination memory to be initialized. If that is not an option the region can be initialized by using Vec::resize instead of set_len.

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