结构填充是否可以由用户代码安全使用?

时间:2022-10-09 06:09:20

Assuming I have a struct like the following:

假设我有一个如下所示的结构:

struct Struct {
    char Char;
    int Int;
};

and sizeof( int ) is greater than one and the compiler adds padding for the Char member variable - is the compiler-generated code allowed to change the values of the padding bytes?

和sizeof(int)大于1并且编译器为Char成员变量添加填充 - 编译器生成的代码是否允许更改填充字节的值?

I mean if I use pointer arithmetic and write some data into the padding bytes surrounding the Char member variable and later do variable.Char = assignment is it possible that the code generated by the compiler will also overwrite some of the padding bytes?

我的意思是,如果我使用指针运算并将一些数据写入Char成员变量周围的填充字节,然后执行variable.Char =赋值,编译器生成的代码是否也可能会覆盖某些填充字节?

4 个解决方案

#1


10  

The following sentence is wrong: No, it would not overwrite the padding bytes. But it probably is not a good practice to use that. If you need it, add member variables there.

以下句子是错误的:不,它不会覆盖填充字节。但使用它可能不是一个好习惯。如果需要,可在其中添加成员变量。

I researched based on comments indicating (correctly) that I am stupid:

我根据评论指出(正确)我是愚蠢的:

The C Standard has an "Annex J" with section J.1 Unspecified behavior. It says, "The value of padding bytes when storing values in structures or unions". The implication is that the compiler can generate whatever instructions it wants to write the data into the structure, which may allow it to overwrite padding after a member.

C标准有一个“附件J”,其中J.1部分未指明行为。它说,“在结构或联合中存储值时填充字节的值”。这意味着编译器可以生成它想要将数据写入结构的任何指令,这可以允许它在成员之后覆盖填充。

#2


13  

What if the compiler were smart enough to use a word write to save the char? Your carefully saved data would be lost. ;-)

如果编译器足够聪明以使用单词写入来保存字符怎么办?您精心保存的数据将丢失。 ;-)

#3


4  

You sure can write something there, and memset-ing an instance of such structure does that. However, it's not safe and never a good idea to do so. Some other day another developer puts a #pragma somewhere or adds a member to struct declaration and your code will explode in many weird and fancy ways, which could take quite a while to debug.

你肯定可以在那里写一些东西,并且记住这种结构的实例就是这样。但是,这样做并不安全,绝不是一个好主意。有一天,另一个开发人员将#pragma放在某处或者将一个成员添加到struct声明中,并且你的代码会以许多奇怪和奇特的方式爆炸,这可能需要很长时间才能进行调试。

#4


1  

The only reason to do this would be something like a plugin evilly tricking a host application into storing extra data.

这样做的唯一原因就是插件可以欺骗主机应用程序来存储额外的数据。

Don't do it though, because at some point in the future it will break, and it will be a severe headache for all concerned.

不要这样做,因为在未来的某个时刻它会破裂,这对所有相关人员来说都是一个严重的问题。

#1


10  

The following sentence is wrong: No, it would not overwrite the padding bytes. But it probably is not a good practice to use that. If you need it, add member variables there.

以下句子是错误的:不,它不会覆盖填充字节。但使用它可能不是一个好习惯。如果需要,可在其中添加成员变量。

I researched based on comments indicating (correctly) that I am stupid:

我根据评论指出(正确)我是愚蠢的:

The C Standard has an "Annex J" with section J.1 Unspecified behavior. It says, "The value of padding bytes when storing values in structures or unions". The implication is that the compiler can generate whatever instructions it wants to write the data into the structure, which may allow it to overwrite padding after a member.

C标准有一个“附件J”,其中J.1部分未指明行为。它说,“在结构或联合中存储值时填充字节的值”。这意味着编译器可以生成它想要将数据写入结构的任何指令,这可以允许它在成员之后覆盖填充。

#2


13  

What if the compiler were smart enough to use a word write to save the char? Your carefully saved data would be lost. ;-)

如果编译器足够聪明以使用单词写入来保存字符怎么办?您精心保存的数据将丢失。 ;-)

#3


4  

You sure can write something there, and memset-ing an instance of such structure does that. However, it's not safe and never a good idea to do so. Some other day another developer puts a #pragma somewhere or adds a member to struct declaration and your code will explode in many weird and fancy ways, which could take quite a while to debug.

你肯定可以在那里写一些东西,并且记住这种结构的实例就是这样。但是,这样做并不安全,绝不是一个好主意。有一天,另一个开发人员将#pragma放在某处或者将一个成员添加到struct声明中,并且你的代码会以许多奇怪和奇特的方式爆炸,这可能需要很长时间才能进行调试。

#4


1  

The only reason to do this would be something like a plugin evilly tricking a host application into storing extra data.

这样做的唯一原因就是插件可以欺骗主机应用程序来存储额外的数据。

Don't do it though, because at some point in the future it will break, and it will be a severe headache for all concerned.

不要这样做,因为在未来的某个时刻它会破裂,这对所有相关人员来说都是一个严重的问题。