Re: [PATCH] rust: alloc: check the length precondition in Vec::from_raw_parts
From: Andreas Hindborg
Date: Mon Nov 16 2026 - 08:08:26 EST
"Peder Bergan via B4 Relay" <devnull+peder.bergan.gmail.com@xxxxxxxxxx>
writes:
> From: Peder Bergan <peder.bergan@xxxxxxxxx>
>
> Vec::from_raw_parts() requires length to be no greater than capacity for
> nonzero-sized elements. Check this precondition with
> unsafe_precondition_assert!() before constructing the vector so debug
> builds diagnose invalid raw parts at this boundary.
>
> Keep the check in the nonzero-sized branch because zero-sized vectors
> ignore the capacity argument. Add a KUnit test for reconstructing a
> nonempty zero-sized vector with a zero capacity argument.
>
> Suggested-by: Miguel Ojeda <ojeda@xxxxxxxxxx>
> Link: https://github.com/Rust-for-Linux/linux/issues/1232
> Signed-off-by: Peder Bergan <peder.bergan@xxxxxxxxx>
Reviewed-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
Best regards,
Andreas Hindborg