public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] rust: print: add safety comments for %pA formatting
@ 2026-02-05  4:21 Muchamad Coirul Anwar
  2026-02-05  8:19 ` Alice Ryhl
  2026-02-10 15:00 ` Miguel Ojeda
  0 siblings, 2 replies; 5+ messages in thread
From: Muchamad Coirul Anwar @ 2026-02-05  4:21 UTC (permalink / raw)
  To: ojeda
  Cc: boqun, gary, bjorn3_gh, lossin, a.hindborg, aliceryhl, tmgross,
	dakr, tamird, gregkh, fujita.tomonori, andrewjballance,
	rust-for-linux, linux-kernel, Muchamad Coirul Anwar

The safety comments in `rust_fmt_argument` and `call_printk` were
previously marked as TODO.

This patch adds the missing safety documentation explaining why
dereferencing the pointers and calling the C `_printk` function
is safe in these contexts. It clarifies the contract between
`lib/vsprintf.c` and the Rust implementation regarding the `%pA`
format specifier.

Signed-off-by: Muchamad Coirul Anwar <muchamadcoirulanwar@gmail.com>
---
 rust/kernel/print.rs | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/rust/kernel/print.rs b/rust/kernel/print.rs
index 6fd84389a858..3e6ff8f42d95 100644
--- a/rust/kernel/print.rs
+++ b/rust/kernel/print.rs
@@ -29,7 +29,11 @@
     use fmt::Write;
     // 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: TODO.
+    // SAFETY: The C implementation of `vsprintf` (in `lib/vsprintf.c`) specifically
+    // calls this function ONLY when processing the `%pA` format specifier.
+    // On the Rust side (`call_printk`), we guarantee that `%pA` is always paired
+    // with a valid pointer to `fmt::Arguments`.
+    // Therefore, dereferencing `ptr` here is safe.
     let _ = w.write_fmt(unsafe { *ptr.cast::<fmt::Arguments<'_>>() });
     w.pos().cast()
 }
@@ -109,7 +113,9 @@ pub unsafe fn call_printk(
 ) {
     // `_printk` does not seem to fail in any path.
     #[cfg(CONFIG_PRINTK)]
-    // SAFETY: TODO.
+    // SAFETY: The format string is constructed to use `%pA`, which corresponds to the
+    // pointer to `fmt::Arguments` passed as the third argument.
+    // Since `args` is a valid reference, casting it to a pointer is safe.
     unsafe {
         bindings::_printk(
             format_string.as_ptr(),
-- 
2.50.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-02-11 18:29 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-05  4:21 [PATCH] rust: print: add safety comments for %pA formatting Muchamad Coirul Anwar
2026-02-05  8:19 ` Alice Ryhl
2026-02-10 15:00 ` Miguel Ojeda
     [not found]   ` <CAO26r3TawBQQ6994h+wDTdiobuT8cwU1jGKDs1mh7HdUp=KpLA@mail.gmail.com>
     [not found]     ` <CABLcDQBjuZc0edQnoD7SfWj8qEH=ciognT6RPfQPO4r7gcODBA@mail.gmail.com>
2026-02-11 13:44       ` Muchamad Coirul Anwar
2026-02-11 18:27         ` [PATCH v2] rust: print: add safety documentation for %pA handling Shivendra Sharma

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox