Re: ANSI C clarifications, with citations (was Re: [patch-2.4

Larry McVoy (lm@bitmover.com)
Tue, 27 Jun 2000 09:49:45 -0700


Sandy said:
> Rusty said:
> > Larry, sorry to disappoint. Albert's full list fairly closely
> > corresponds with existing kernel assumptions. Attacking the messenger
> > won't change this;
>
> Perhaps, but he presented it as "Moderately portable code assumes a
> sane compiler and sane hardware." That is a very different thing
> than describing current kernel assumptions.
>
> I don't doubt those assumptions are (at least close to) a good
> description of what the kernel code currently assumes. On the
> other, as a basis for portable code design, Larry's quite right.
> Some of them are awful.

I'm sorry if my point was not clear; I was strongly reaction to Albert's
assertion that his list of assumptions were reasonable for "a sane
compiler and sane hardware". It is certainly not.

It is not even true for the Linux kernel. As Sandy points out:

> The assumption that really cracks me up is:
>
> > > > 7. You can freely cast between any two pointer types
>
> Cast a char * to int * on any architecture with strong alignmenmt
> restrictions and watch your code commit messy suicide.

Sandy is exactly correct. To make this perfectly clear:

main()
{
int i, *ip, ints[2];
char *cp;

cp = (char*)ints;
cp += 1; /* unalign it */
ip = (int*)cp;
i = ip[1]; /* derereference an unaligned pointer */
}

$ uname -a
Linux sparc 2.2.12-42 #1 Fri Nov 12 13:03:33 EST 1999 sparc64 unknown
$ cc align.c
$ a.out
Bus error (core dumped)

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