rust: document safety requirements for fmt::Arguments dereference
PATCH 1/1 rust: document safety requirements for fmt::Arguments dereference
From: Riccardo Adami <[email protected]>
This patch documents the safety requirements for dereferencing fmt::Arguments in the kernel's vsnprintf formatting path. This clarifies why it is safe to perform the unsafe dereference in rust_fmt_argument and helps future maintainers understand the invariants, reducing the risk of incorrect modifications.
--- rust/kernel/print.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
rust/kernel/print.rs +4 -4
@@ -25,10 +25,10 @@// SAFETY: The C contract guarantees that `buf` is valid if it's less than `end`. let mut w =unsafe {RawFormatter ::from_ptrs (buf.cast (), end.cast ()) };//SAFETY: `ptr` is provided by the kernel formatting path for the `%pA` -// specifier during `vsnprintf` processing and always points to a valid -// `fmt::Arguments` value created by the caller. The value lives at least -// for the duration of this call and is only read here. It is not stored -// or accessed after returning, so dereferencing it is safe. +// specifier during `vsnprintf` processing and always points to a valid +// `fmt::Arguments` value created by the caller. The value lives at least +// for the duration of this call and is only read here. It is not stored +// or accessed after returning, so dereferencing it is safe. let _ = w.write_fmt (unsafe { *ptr.cast ::<fmt::Arguments <'_>>() }); w.pos ().cast ()
Signature · 1 line
2.52.0
Hi Riccardo,
Quoted 1 line · Riccardo
From: Riccardo Adami <[email protected]>
If you configure your mail tool to use Riccardo Adami <[email protected]> automatically in the From: ... (instead ofRiccardo <[email protected]> which is there at the moment) there is no need to add this From: ... manually here.
Quoted 5 lines
This patch documents the safety requirements for dereferencing
fmt::Argumentsin the kernel'svsnprintfformatting path. This clarifies why it is safe to perform the unsafe dereference inrust_fmt_argumentand helps future maintainers understand the invariants, reducing the risk of incorrect modifications.
Empty line between the description and the Signed-off-by please.
Quoted 1 line
Signed-off-by Riccardo <[email protected]>
Please use your "full" name here. Like you have done in the manually added From: ... above: Riccardo Adami <[email protected]>
Quoted 1 line
---
If you send newer versions of your patch please use some versioning v2, v3 etc. This has to go into the subject e.g. [PATCH v2] ... and needs a change log here below the --- e.g.
v2: Add Signed-off-by ....
Maybe you like to have a look to existing patches like
https://lore.kernel.org/rust-for-linux/[email protected]/
Quoted 9 lines
rust/kernel/print.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)rust/kernel/print.rs +0
-0@@ -25,10 +25,10 @@// SAFETY: The C contract guarantees that `buf` is valid if it's less than `end`.
rust/kernel/print.rs line 31
Quoted 10 lines
rust/kernel/print.rs +4
-4let mut w =unsafe {RawFormatter ::from_ptrs (buf.cast (), end.cast ()) };//SAFETY: `ptr` is provided by the kernel formatting path for the `%pA` -// specifier during `vsnprintf` processing and always points to a valid -// `fmt::Arguments` value created by the caller. The value lives at least -// for the duration of this call and is only read here. It is not stored -// or accessed after returning, so dereferencing it is safe. +// specifier during `vsnprintf` processing and always points to a valid +// `fmt::Arguments` value created by the caller. The value lives at least +// for the duration of this call and is only read here. It is not stored +// or accessed after returning, so dereferencing it is safe.
I have some difficulties matching the description in the commit message above with this change. For me it looks like you just fix the indentation but haven't changed anything on the SAFETY comment itself?
Looking at the original file
I wonder if in the end you want to update the // SAFETY: TODO. there? What would match the commit description? But that isn't done here. So if this is the case you might want to check if you sent an intermediate version of you work?
Thanks for working on this!
Dirk