Re: [PATCH] Re: [ipchains] logging tcp/udp port numbers

H. Peter Anvin (hpa@transmeta.com)
19 Nov 1998 08:34:16 GMT


Followup to: <Pine.LNX.3.95.981118082435.3309A-100000@penguin.transmeta.com>
By author: Linus Torvalds <torvalds@transmeta.com>
In newsgroup: linux.dev.kernel
>
> On Wed, 18 Nov 1998, Paul Rusty Russell wrote:
> >
> > The %h printk modifier is used in 5 source files, and doesn't work on
> > ppc and sparc (vararg passing a short is broken on these platforms it
> > seems).
>
> If varargs is broken on them, then I would complain to the gcc people. I
> can get rid of the 'h' qualifier - I don't think it really means anything
> in the C language with the promotion rules - but I want to do so only
> after I've heard from people that gcc is going to be fixed /or/ somebody
> tells me that due to C promotion rules I really should get an int and cast
> it (ie instead of using
>

'h' only makes sense for scanf, not printf.

>
> num = va_arg(args, short);
>
> it might be proper to use
>
> num = (short) va_args(args, int);
>
> But I _really_ don't want to just silently drop a documented feature.
>
> Why is it broken on ppc/sparc? I assume it's because they have the wrong
> byte order, but why does this bite only the Linux kernel - it should have
> bitten other things too?
>

The problem is the way stdarg is typically implemented (the va_arg
thing casts something magically to a pointer whose type is taken from
the second argument.) I am 99.9% sure that using a promoted type in
va_arg is incorrect, and that the second form is the correct one.

The error occurs on bigendian machines because you're effectively
doing the moral equivalent of:

foo = *(short *)(&args[n--]);

instead of

foo = (short) *(int *)(&args[n--]);

... which is the same of littleendian boxen but not bigendian.

-hpa

-- 
    PGP: 2047/2A960705 BA 03 D3 2C 14 A8 A8 BD  1E DF FE 69 EE 35 BD 74
    See http://www.zytor.com/~hpa/ for web page and full PGP public key
        I am Bahá'í -- ask me about it or see http://www.bahai.org/
   "To love another person is to see the face of God." -- Les Misérables

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/