David Miller wrote:
> We always explicitly initialize the flows, and even for local stack
> assignment based initialization, gcc zeros out the padding bytes
> always.

I thought so too until I added the iptables compat functions recently
and noticed uninitialized padding of on-stack structures, which
confused iptables since it also uses memcmp.

This program demonstrates the effect, it doesn't output the expected
"1 2" but "1 4294967042" on my x86_64 (gcc-Version 4.1.2 20060901
(prerelease) (Debian 4.1.1-13)). The initialization doesn't touch
the padding bytes:

0x0000000000400494 <test+8>:    movl   $0x1,0xfffffffffffffff0(%rbp)
0x000000000040049b <test+15>:   movb   $0x2,0xfffffffffffffff4(%rbp)

#include <stdio.h>

struct x1 {
	unsigned int	x;
	char		y;
};

struct x2 {
	unsigned int	x;
	unsigned int	y;
};

void pollute(void)
{
	struct x2 x = {
		.x	= ~0,
		.y	= ~0,
	};
}

void test(void)
{
	struct x1 x1 = {
		.x	= 1,
		.y	= 2,
	};
	struct x2 *x2 = (struct x2 *)&x1;

	printf("%u %u\n", x2->x, x2->y);
}

int main(int argc, char **argv)
{
	pollute();
	test();
	return 0;
}

Reply via email to