Re: [PATCH v2 1/2] rust: str: tie Formatter to its backing buffer

From: Alice Ryhl

Date: Thu Nov 19 2026 - 03:54:08 EST


On Tue, Nov 17, 2026 at 6:57 PM Peder Bergan via B4 Relay
<devnull+peder.bergan.gmail.com@xxxxxxxxxx> wrote:
>
> From: Peder Bergan <peder.bergan@xxxxxxxxx>
>
> Formatter::new() returns a Formatter whose lifetime is independent of the
> borrow of its backing buffer. Safe code can therefore drop the buffer and
> then write through the formatter, causing a use-after-free.
>
> Tie the buffer borrow to the formatter's lifetime so that the borrow
> checker rejects such callers and prevents overlapping mutable access.
>
> Fixes: 87482d6d9104 ("rust: str: allow `str::Formatter` to format into `&mut [u8]`.")
> Cc: stable@xxxxxxxxxxxxxxx
> Reported-by: Georgios Androutsopoulos <georgeandrout13@xxxxxxxxx>
> Closes: https://github.com/Rust-for-Linux/linux/issues/1261
> Signed-off-by: Peder Bergan <peder.bergan@xxxxxxxxx>

Reviewed-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>