2006-10-10 21:53:00

by Al Viro

[permalink] [raw]
Subject: [PATCH] use %p for pointers


Signed-off-by: Al Viro <[email protected]>
---
drivers/sbus/char/uctrl.c | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/drivers/sbus/char/uctrl.c b/drivers/sbus/char/uctrl.c
index ddc0681..b30372f 100644
--- a/drivers/sbus/char/uctrl.c
+++ b/drivers/sbus/char/uctrl.c
@@ -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;
--
1.4.2.GIT


2006-10-10 21:59:37

by David Miller

[permalink] [raw]
Subject: Re: [PATCH] use %p for pointers

From: Al Viro <[email protected]>
Date: Tue, 10 Oct 2006 22:49:57 +0100

>
> Signed-off-by: Al Viro <[email protected]>

Signed-off-by: David S. Miller <[email protected]>

2006-10-11 11:18:19

by Jan Engelhardt

[permalink] [raw]
Subject: Re: [PATCH] use %p for pointers


>diff --git a/drivers/sbus/char/uctrl.c b/drivers/sbus/char/uctrl.c
>index ddc0681..b30372f 100644
>--- a/drivers/sbus/char/uctrl.c
>+++ b/drivers/sbus/char/uctrl.c
>@@ -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'
--

2006-10-11 14:54:46

by Al Viro

[permalink] [raw]
Subject: Re: [PATCH] use %p for pointers

On Wed, Oct 11, 2006 at 01:16:56PM +0200, Jan Engelhardt wrote:
>
> >diff --git a/drivers/sbus/char/uctrl.c b/drivers/sbus/char/uctrl.c
> >index ddc0681..b30372f 100644
> >--- a/drivers/sbus/char/uctrl.c
> >+++ b/drivers/sbus/char/uctrl.c
> >@@ -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).

%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...

2006-10-11 18:45:50

by H. Peter Anvin

[permalink] [raw]
Subject: Re: [PATCH] use %p for pointers

Al Viro wrote:
>
> %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

2006-10-11 19:15:20

by Jan Engelhardt

[permalink] [raw]
Subject: Re: [PATCH] use %p for pointers


>> %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 *).

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

ack. Make a bug report perhaps?

-`J'
--

2006-10-11 19:16:44

by H. Peter Anvin

[permalink] [raw]
Subject: Re: [PATCH] use %p for pointers

Jan Engelhardt wrote:
>>> %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.

>> 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

2006-10-11 19:30:35

by Jan Engelhardt

[permalink] [raw]
Subject: Re: [PATCH] use %p for pointers


>> > > %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'
--

2006-10-11 20:28:35

by Jakub Jelinek

[permalink] [raw]
Subject: Re: [PATCH] use %p for pointers

On Wed, Oct 11, 2006 at 11:45:10AM -0700, H. Peter Anvin wrote:
> Al Viro wrote:
> >
> >%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

2006-10-11 20:39:28

by H. Peter Anvin

[permalink] [raw]
Subject: Re: [PATCH] use %p for pointers

Jakub Jelinek wrote:
> On Wed, Oct 11, 2006 at 11:45:10AM -0700, H. Peter Anvin wrote:
>> Al Viro wrote:
>>> %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

2006-10-12 11:04:03

by Nikita Danilov

[permalink] [raw]
Subject: Re: [PATCH] use %p for pointers

H. Peter Anvin writes:
> Al Viro wrote:
> >
> > %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

Signed-off-by: Nikita Danilov <[email protected]>

Index: git-linux/lib/vsprintf.c
===================================================================
--- git-linux.orig/lib/vsprintf.c
+++ 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;



2006-10-12 11:49:10

by Andreas Schwab

[permalink] [raw]
Subject: Re: [PATCH] use %p for pointers

Nikita Danilov <[email protected]> writes:

> 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.

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

The standard makes it completely implementation defined.

Andreas.

--
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."

2006-10-12 12:02:44

by Nikita Danilov

[permalink] [raw]
Subject: Re: [PATCH] use %p for pointers

Andreas Schwab writes:
> Nikita Danilov <[email protected]> writes:
>
> > 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.
>
> > 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.

>
> Andreas.

Nikita.

2006-10-12 12:38:03

by Jan Engelhardt

[permalink] [raw]
Subject: Re: [PATCH] use %p for pointers

> > > 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.
> >
> > > 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'
--

2006-10-15 15:43:09

by Jan Engelhardt

[permalink] [raw]
Subject: Re: [PATCH] use %p for pointers


>so %p already has to output '0x', it's lib/vsprintf.c to blame for
>non-conforming behavior. What about
>
>Signed-off-by: Nikita Danilov <[email protected]>
>
>Index: git-linux/lib/vsprintf.c

Seems like the mail I wrote did not arrive. In that case, I'll give you
an URL: http://jengelh.hopto.org/f/nikita-printf-p.diff.bz2 (compile
tested)
Signed-off-by: Jan Engelhardt <[email protected]>


-`J'
--