https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92486

--- Comment #9 from Alexander Cherepanov <ch3root at openwall dot com> ---
> Now as an exercise build a complete testcase for the DSE issue above.

Source code:

----------------------------------------------------------------------
#include <string.h>
#include <stdio.h>

struct s {
    char c;
    int i;
};

__attribute__((noinline,noclone))
void f(struct s *p, struct s *q)
{
    struct s w;

    memset(&w, 0, sizeof(struct s));
    w = *q;

    memset(p, 0, sizeof(struct s));
    *p = w;
}

int main()
{
    struct s x;
    memset(&x, 1, sizeof(struct s));

    struct s y;
    memset(&y, 2, sizeof(struct s));

    f(&y, &x);

    for (unsigned char *p = (unsigned char *)&y; p < (unsigned char *)&y +
sizeof(struct s); p++)
        printf("%d", *p);
    printf("\n");
}
----------------------------------------------------------------------

Results:

----------------------------------------------------------------------
$ gcc -std=c11 -pedantic -Wall -Wextra test.c && ./a.out
11111111
$ gcc -std=c11 -pedantic -Wall -Wextra -O3 test.c && ./a.out
12221111
----------------------------------------------------------------------

gcc x86-64 version: gcc (GCC) 10.0.0 20191114 (experimental)


But from the C standard POV this case is much more clear: there is no problem
as stores into a struct make its padding unspecified (C11, 6.2.6.1p6). OTOH
this sample demonstrates the problem with trunc, so it could be more convenient
for testing.

Reply via email to