On 09/08/2016 01:21 AM, Paul Eggert wrote:
Sure, attached. On Fedora 24 x86-64 (GCC 6.1.1 20160621, valgrind 3.11.0), when I compile with "gcc -O2 flexouch.c" and run with "valgrind ./a.out", valgrind complains "Invalid read of size 2". This is because GCC compiles "p->d[0] == 2 && p->d[1] == 3" into "cmpw $770, 8(%rax); sete %al", which loads the uninitialized byte p->d[1] simultaneously with the initialized byte p->d[0].
Interesting. That optimization doesn't really depend on d being a flexible array, so you can also reproduce a (different) valgrind warning with the following:
#include <stddef.h> #include <stdlib.h> struct s { int x; char d[2]; }; __attribute__((noinline,noclone)) void foo (struct s *p) { p->d[0] = 1; } int main (void) { struct s *p = malloc (sizeof *p); foo (p); return p->d[0] == 2 && p->d[1] == 3; } Bernd