rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: fmt: fix {:p} formatting for raw pointers
@ 2025-12-18  3:27 Ke Sun
  2025-12-18  8:04 ` Alice Ryhl
  0 siblings, 1 reply; 2+ messages in thread
From: Ke Sun @ 2025-12-18  3:27 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	Tamir Duberstein, rust-for-linux, Ke Sun

The fmt! macro wraps arguments as Adapter(&arg), causing raw pointers
to be wrapped as Adapter(&*mut T). The generic Pointer implementation
then formats the reference address (stack address of the variable)
instead of the pointer value.

Add specialized Pointer implementations for Adapter<&*mut T> and
Adapter<&*const T> that dereference before formatting.

Example:
    let ptr: *mut SomeStruct = 0x12345678 as *mut _;
    dev_info!(dev, "ptr: {:p}", ptr);

Before (wrong): prints stack address of `ptr` variable, e.g. 0xffffc90001234560
After (correct): prints pointer value, e.g. 0x12345678

Signed-off-by: Ke Sun <sunke@kylinos.cn>
---
 rust/kernel/fmt.rs | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/rust/kernel/fmt.rs b/rust/kernel/fmt.rs
index 84d634201d90a..2aebc782fedf9 100644
--- a/rust/kernel/fmt.rs
+++ b/rust/kernel/fmt.rs
@@ -28,7 +28,27 @@ fn fmt(&self, f: &mut Formatter<'_>) -> Result {
 }
 
 use core::fmt::{Binary, LowerExp, LowerHex, Octal, Pointer, UpperExp, UpperHex};
-impl_fmt_adapter_forward!(Debug, LowerHex, UpperHex, Octal, Binary, Pointer, LowerExp, UpperExp);
+impl_fmt_adapter_forward!(Debug, LowerHex, UpperHex, Octal, Binary, LowerExp, UpperExp);
+
+// Special handling for raw pointers: when Adapter wraps a reference to a raw pointer,
+// we want to print the pointer value itself, not the reference's address.
+// This fixes the issue where {:p} prints the stack address of the pointer variable
+// instead of the address the pointer points to.
+impl<T> Pointer for Adapter<&*mut T> {
+    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
+        let Self(t) = self;
+        // Dereference the reference to get the raw pointer, then format it
+        Pointer::fmt(*t, f)
+    }
+}
+
+impl<T> Pointer for Adapter<&*const T> {
+    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
+        let Self(t) = self;
+        // Dereference the reference to get the raw pointer, then format it
+        Pointer::fmt(*t, f)
+    }
+}
 
 /// A copy of [`core::fmt::Display`] that allows us to implement it for foreign types.
 ///
-- 
2.43.0


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

* Re: [PATCH] rust: fmt: fix {:p} formatting for raw pointers
  2025-12-18  3:27 [PATCH] rust: fmt: fix {:p} formatting for raw pointers Ke Sun
@ 2025-12-18  8:04 ` Alice Ryhl
  0 siblings, 0 replies; 2+ messages in thread
From: Alice Ryhl @ 2025-12-18  8:04 UTC (permalink / raw)
  To: Ke Sun, Tamir Duberstein
  Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Trevor Gross, Danilo Krummrich,
	rust-for-linux

On Thu, Dec 18, 2025 at 11:27:09AM +0800, Ke Sun wrote:
> The fmt! macro wraps arguments as Adapter(&arg), causing raw pointers
> to be wrapped as Adapter(&*mut T). The generic Pointer implementation
> then formats the reference address (stack address of the variable)
> instead of the pointer value.
> 
> Add specialized Pointer implementations for Adapter<&*mut T> and
> Adapter<&*const T> that dereference before formatting.
> 
> Example:
>     let ptr: *mut SomeStruct = 0x12345678 as *mut _;
>     dev_info!(dev, "ptr: {:p}", ptr);
> 
> Before (wrong): prints stack address of `ptr` variable, e.g. 0xffffc90001234560
> After (correct): prints pointer value, e.g. 0x12345678
> 
> Signed-off-by: Ke Sun <sunke@kylinos.cn>

Ahh ... the goal here is that trying to print a raw pointer should not
compile at all. It's unfortunate if that's not what actually happens.

Alice

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

end of thread, other threads:[~2025-12-18  8:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-18  3:27 [PATCH] rust: fmt: fix {:p} formatting for raw pointers Ke Sun
2025-12-18  8:04 ` Alice Ryhl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).