[LLVMdev] Language lawyer question

Daniel Berlin dberlin at dberlin.org
Wed Mar 12 08:43:45 PDT 2008


On Tue, Mar 11, 2008 at 11:42 PM, Dale Johannesen <dalej at apple.com> wrote:
>
> Looking through the gcc testsuite turned up an interesting edge case.  Let's
> assume our target leaves a hole for alignment in struct x, as do x86 and
> powerpc.  Do you think the following code can validly abort?
>
>
>   struct x { char c; short s; };
>   int i;    char *p;
>   memset(&X, 0, sizeof(struct x));
>   memset(&Y, 22, sizeof(struct x));
>   X = Y;
>   for (i=0, p=(char *)&X; i<sizeof(struct x); i++, p++)
>     if (*p != 22)
>       abort();
>
> The memset's and char-by-char comparison are clearly valid references; the
> questionable bit is the struct copy, which llvm-gcc currently does
> field-by-field, skipping the hole.  C99 says
>
>
> In simple assignment (=), the value of the right operand is converted to the
> type of the
> assignment expression and replaces the value stored in the object designated
> by the left
> operand.
>

Padding is allowed to be skipped in structures in this case.
See 6.2.6.1.
Even further, all padding is allowed to take any value no matter how
you try to set it (IE it always allowed to have an undefined value,
even if you memset it).


We happen to allow memset to clear padding bits, but we don't have to, AFAIK.

--Dan



More information about the llvm-dev mailing list