Skip to contentSkip to conversation

use %p for pointers

PATCH In review Al Viro 7 participants 15 messages Oct 10, 2006 – Oct 15, 2006

Subsystems (3)
lore
Share & tools
lore

Al Viro

author
#0 lore raw

PATCH 1/1 use %p for pointers

---
 drivers/sbus/char/uctrl.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/sbus/char/uctrl.c +1 -1
index ddc0681..b30372f 100644
@@ -400,7 +400,7 @@ static int __init ts102_uctrl_init(void)
 	}
 
 	driver->regs->uctrl_intr = UCTRL_INTR_RXNE_REQ|UCTRL_INTR_RXNE_MSK;
-	printk("uctrl: 0x%x (irq %d)\n", driver->regs, driver->irq);
+	printk("uctrl: 0x%p (irq %d)\n", driver->regs, driver->irq);
 	uctrl_get_event_status();
 	uctrl_get_external_status();
         return 0;
Signature · 1 line
1.4.2.GIT
#2 lore raw
In reply to Al Viro

drivers/sbus/char/uctrl.c static int __init ts102_uctrl_init(void) · line 403

Quoted 11 lines
drivers/sbus/char/uctrl.c +1 -1
index ddc0681..b30372f 100644
@@ -400,7 +400,7 @@ static int __init ts102_uctrl_init(void)
 	}
 
 	driver->regs->uctrl_intr = UCTRL_INTR_RXNE_REQ|UCTRL_INTR_RXNE_MSK;
-	printk("uctrl: 0x%x (irq %d)\n", driver->regs, driver->irq);
+	printk("uctrl: 0x%p (irq %d)\n", driver->regs, driver->irq);

So what's the difference, except that %p will evaluate to (nil) or (null) when the argument is 0 [this is the case with glibc]? That would print 0x(nil).

	-`J'
author
#3 lore raw
Quoted 8 lines · Jan Engelhardt
Quoted 8 lines
drivers/sbus/char/uctrl.c +0 -0
index ddc0681..b30372f 100644
@@ -400,7 +400,7 @@ static int __init ts102_uctrl_init(void)
 	}
 
Quoted 7 lines

drivers/sbus/char/uctrl.c static int __init ts102_uctrl_init(void) · line 403

Quoted 3 lines
drivers/sbus/char/uctrl.c +1 -1
 	driver->regs->uctrl_intr = UCTRL_INTR_RXNE_REQ|UCTRL_INTR_RXNE_MSK;
-	printk("uctrl: 0x%x (irq %d)\n", driver->regs, driver->irq);
+	printk("uctrl: 0x%p (irq %d)\n", driver->regs, driver->irq);

So what's the difference, except that %p will evaluate to (nil) or (null) when the argument is 0 [this is the case with glibc]? That would print 0x(nil).

%p will do no such thing in the kernel. As for the difference... %x might happen to work on some architectures (where sizeof(void *)==sizeof(int)), but it's not portable _and_ not right. %p is proper C for that...

#4 lore raw
Quoted 4 lines · Al Viro

%p will do no such thing in the kernel. As for the difference... %x might happen to work on some architectures (where sizeof(void *)==sizeof(int)), but it's not portable _and_ not right. %p is proper C for that...

It's really too bad gcc bitches about %#p, because that's arguably The Right Thing.

-hpa

#5 lore raw
Quoted 5 lines
Quoted 5 lines

%p will do no such thing in the kernel. As for the difference... %x might happen to work on some architectures (where sizeof(void *)==sizeof(int)),
but it's not portable _and_ not right. %p is proper C for that...

Ah I see your point, but then again, %lx could have been used. Unless there is some arch where sizeof(long) != sizeof(void *).

Quoted 2 lines

It's really too bad gcc bitches about %#p, because that's arguably The Right Thing.

ack. Make a bug report perhaps?

	-`J'
#6 lore raw
Quoted 6 lines · Jan Engelhardt
Quoted 4 lines
Quoted 4 lines

%p will do no such thing in the kernel. As for the difference... %x might happen to work on some architectures (where sizeof(void *)==sizeof(int)),
but it's not portable _and_ not right. %p is proper C for that...

Ah I see your point, but then again, %lx could have been used. Unless there is some arch where sizeof(long) != sizeof(void *).

That really makes gcc bitch, *and* it's wrong for a whole bunch of reasons.

Quoted 4 lines
Quoted 2 lines

It's really too bad gcc bitches about %#p, because that's arguably The Right Thing.

ack. Make a bug report perhaps?

Maybe. They'll probably say "the C standard says so" :-/

-hpa

#7 lore raw
Quoted 11 lines
Quoted 9 lines
Quoted 6 lines
Quoted 6 lines

%p will do no such thing in the kernel. As for the difference... %x
might happen to work on some architectures (where sizeof(void *)==sizeof(int)),
but it's not portable _and_ not right. %p is proper C for that...

Ah I see your point, but then again, %lx could have been used. Unless there is some arch where sizeof(long) != sizeof(void *).

That really makes gcc bitch, *and* it's wrong for a whole bunch of reasons.

Ah my bad. Thanks for the slap reminder. :)

	-`J'
#8 lore raw
In reply to H. Peter Anvin
Quoted 9 lines · H. Peter Anvin
Quoted 5 lines · Al Viro

%p will do no such thing in the kernel. As for the difference... %x might happen to work on some architectures (where sizeof(void *)==sizeof(int)),
but it's not portable _and_ not right. %p is proper C for that...

It's really too bad gcc bitches about %#p, because that's arguably The Right Thing.

It is correct that gcc warns about %#p, that invokes undefined behavior in ISO C99.

Jakub

#9 lore raw
Quoted 10 lines · Jakub Jelinek
Quoted 5 lines · H. Peter Anvin
Quoted 3 lines · Al Viro

%p will do no such thing in the kernel. As for the difference... %x might happen to work on some architectures (where sizeof(void *)==sizeof(int)), but it's not portable _and_ not right. %p is proper C for that...

It's really too bad gcc bitches about %#p, because that's arguably The Right Thing.

It is correct that gcc warns about %#p, that invokes undefined behavior in ISO C99.

Yes, it's a bug in the standard.

-hpa

#10 lore raw
In reply to H. Peter Anvin
Quoted 8 lines · H. Peter Anvin
Quoted 4 lines · Al Viro

%p will do no such thing in the kernel. As for the difference... %x might happen to work on some architectures (where sizeof(void *)==sizeof(int)), but it's not portable _and_ not right. %p is proper C for that...

It's really too bad gcc bitches about %#p, because that's arguably The Right Thing.

Hm...

man 3 printf:

p The void * pointer argument is printed in hexadeci-
mal (as if by %#x or %#lx).

so %p already has to output '0x', it's lib/vsprintf.c to blame for non-conforming behavior. What about

lib/vsprintf.c +1 -1
Index: git-linux/lib/vsprintf.c
===================================================================
@@ -408,7 +408,7 @@ int vsnprintf(char *buf, size_t size, co
 				}
 				str = number(str, end,
 						(unsigned long) va_arg(args, void *),
-						16, field_width, precision, flags);
+						16, field_width, precision, flags|SPECIAL);
 				continue;
 
 
#11 lore raw
Quoted 6 lines · Nikita Danilov

man 3 printf:

p The void * pointer argument is printed in hexadeci-
mal (as if by %#x or %#lx).

so %p already has to output '0x',

That is an detail of this particular implementation.

Quoted 1 line

it's lib/vsprintf.c to blame for non-conforming behavior.

The standard makes it completely implementation defined.

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#12 lore raw
Quoted 7 lines · Andreas Schwab
Quoted 5 lines · Nikita Danilov

man 3 printf:

p The void * pointer argument is printed in hexadeci-
mal (as if by %#x or %#lx).

Quoted 7 lines
Quoted 1 line

so %p already has to output '0x',

That is an detail of this particular implementation.

Quoted 1 line

it's lib/vsprintf.c to blame for non-conforming behavior.

The standard makes it completely implementation defined.

Yes, but POSIX/SUS aside, at least we might make kernel version closer to Linux user-level.

Quoted 2 lines

Andreas.

Nikita.

#13 lore raw
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

man 3 printf:

p The void * pointer argument is printed in hexadeci-
mal (as if by %#x or %#lx).

so %p already has to output '0x',

Quoted 8 lines
Quoted 5 lines

That is an detail of this particular implementation.

Quoted 1 line

it's lib/vsprintf.c to blame for non-conforming behavior.

The standard makes it completely implementation defined.

Yes, but POSIX/SUS aside, at least we might make kernel version closer to Linux user-level.

I do agree.

#include <stdio.h>

int main(void) {
    return printf("%p %p\n", main, NULL);
}

In glibc will print "0x7555562c (nil)" which seems ok enough.

	-`J'