[英]Using `-ftrivial-auto-var-init` to guarantee the initialization of padding bytes to zero for non-static aggregate objects
I'm following Does C++ standard guarantee the initialization of padding bytes to zero for non-static aggregate objects?我正在关注Does C++ standard 保证将非静态聚合对象的填充字节初始化为零? and trying to force GCC and Clang to initialize padding with zero.
并试图强制GCC 和 Clang 用零初始化填充。 The
-ftrivial-auto-var-init=choice
flag seems to do the trick, but I don't see any difference between using it or not -ftrivial-auto-var-init=choice
标志似乎可以解决问题,但我看不出是否使用它有什么区别
The original code is here (without using -ftrivial-auto-var-init=zero
), and produces non-zeroed members:原始代码在这里(不使用
-ftrivial-auto-var-init=zero
),并生成非零成员:
Foo():
x:[----][0x42][0x43][0x44],v: 0
y:[----][----][----][----],v: 0
z:[----][0x4A][0x4B][0x4C],v: 0
Foo{}:
x:[----][----][----][----],v: 0
y:[----][----][----][----],v: 0
z:[----][----][----][----],v: 0
Foo():
x:[----][----][----][----],v: 0
y:[----][----][----][----],v: 0
z:[----][----][----][----],v: 0
Foo{}:
x:[----][0x42][0x43][0x44],v: 0
y:[----][----][----][----],v: 0
z:[----][0x4A][0x4B][0x4C],v: 0
I've added the -ftrivial-auto-var-init=zero
flag to both GCC 12 and Clang 15 .我已将
-ftrivial-auto-var-init=zero
标志添加到GCC 12 和 Clang 15 。 but the results are exactly the same, so I'm a bit confused as to what -ftrivial-auto-var-init=zero
should do.但结果完全一样,所以我对
-ftrivial-auto-var-init=zero
应该做什么感到有点困惑。
Foo():
x:[----][0x42][0x43][0x44],v: 0
y:[----][----][----][----],v: 0
z:[----][0x4A][0x4B][0x4C],v: 0
Foo{}:
x:[----][----][----][----],v: 0
y:[----][----][----][----],v: 0
z:[----][----][----][----],v: 0
Foo():
x:[----][----][----][----],v: 0
y:[----][----][----][----],v: 0
z:[----][----][----][----],v: 0
Foo{}:
x:[----][0x42][0x43][0x44],v: 0
y:[----][----][----][----],v: 0
z:[----][0x4A][0x4B][0x4C],v: 0
Note that -ftrivial-auto-var-init
only applies to automatic variables (like the name suggests):请注意,
-ftrivial-auto-var-init
仅适用于自动变量(顾名思义):
Initialize automatic variables with either a pattern or with zeroes to increase the security and predictability of a program by preventing uninitialized memory disclosure and use.
使用模式或零初始化自动变量,以通过防止未初始化的 memory 泄露和使用来提高程序的安全性和可预测性。
Automatic variables are variables that have automatic storage duration , ie local block-scope variables only.自动变量是具有自动存储持续时间的变量,即仅本地块范围变量。
Note that if you're using new
/ delete
then your objects will havedynamic storage duration and therefore -ftrivial-auto-var-init
will not apply to them.请注意,如果您使用的是
new
/ delete
,那么您的对象将具有动态存储持续时间,因此-ftrivial-auto-var-init
将不适用于它们。
This example demonstrates the effect of -ftrivial-auto-var-init=zero
:此示例演示了
-ftrivial-auto-var-init=zero
的效果:
godbolt神栓
struct Foo
{
char x;
int y;
char z;
};
int main()
{
{
char buf[sizeof(Foo)];
for(int i = 0; i < sizeof(buf); ++i)
{
buf[i] = 'A' + i;
}
buf[sizeof(Foo)-1] = '\0';
printf("%s\n", buf);
}
{
Foo f;
DumpF("Foo, automatic", &f);
}
}
When compiled with -ftrivial-auto-var-init=zero
you're guaranteed to get:当使用
-ftrivial-auto-var-init=zero
编译时,你一定会得到:
Foo, automatic:
x:[----][----][----][----],v: 0
y:[----][----][----][----],v: 0
z:[----][----][----][----],v: 0
Whereas if you compile without it you'll most likely get random bytes from the stack (in this case gcc chose to reuse the space of buf
):而如果你在没有它的情况下进行编译,你很可能会从堆栈中获取随机字节(在这种情况下 gcc 选择重用
buf
的空间):
Foo, automatic:
x:[0x41][0x42][0x43][0x44],v: 65
y:[0x45][0x46][0x47][0x48],v: 1212630597
z:[0x49][0x4A][0x4B][----],v: 73
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.