Stop Memsetting Structures
anmolsarma.in
Stop Memsetting Structures
1–10 of 277 posts
Re: Stop Memsetting Structures
#2EDIT: Actually I have to apologize, because I’m not certain what the C standard says about whether the padding bytes can change when changing members of the structure (6.2.6.1):
6 When a value is stored in an object of structure or union type, including in a member object, the bytes of the object representation that correspond to any padding bytes take unspecified values. 51)
If that means that the padding can actually also change when just assigning individual members (which I think it does, but footnote 51 only calls out copying the whole struct by assignment, and I’m not 100% sure that “including a member object” means what I think in this context), then that means that copying an unpacked structure across information boundaries is technically always unsafe, because any change of a value in the struct might leak information by changing the padding to undefined values. It’s safe with explicitly packed structs, though (which should be the majority of the cases, as you’d have a representation independent of the architecture), but then so is what the author describes. I’d appreciate if someone could clear this up! memset’ing can still help you getting a structure quickly into a well-defined state, though.
Re: Stop Memsetting Structures
#3Even as a member of the Force (RIIR 1st brigade), I laughed pretty heartily at this. :)
Re: Stop Memsetting Structures
#4Re: Stop Memsetting Structures
#5> Cue the Rust Evangelism Strike Force chiming in to say that there is really no reason to be writing new C code in 2019. Even as a member of the Force (RIIR 1st brigade), I laughed pretty heartily at this. :)
Re: Stop Memsetting Structures
#6For code that only needs to compile as C, I agree. It's one of the best (if not the best addition) to the C language. A nice addition would be default values for struct members in the declaration with compile-time constants.
Re: Stop Memsetting Structures
#7 struct foo {
void * bar;
} baz;
...
memset(&baz, 0, sizeof(struct foo));
assert(baz.bar == NULL);
it's possible for the assertion to fail, since NULL it not guaranteed to be represented in memory by a zero bytes.Re: Stop Memsetting Structures
#8Yes, someone will point out that there are languages that do not have such problems. However, when still using C, one has to be extremely careful...
Re: Stop Memsetting Structures
#9> Cue the Rust Evangelism Strike Force chiming in to say that there is really no reason to be writing new C code in 2019. Even as a member of the Force (RIIR 1st brigade), I laughed pretty heartily at this. :)
Same. But also, I feel like most of The Force's efforts are directed at C++. I could be wrong.
Re: Stop Memsetting Structures
#10Does that also zero the padding that may exist between elements of the structure (or at its end)? Because if not, not memset'ing will open you up to all kinds of info leaks, especially if you plan to send that struct over the network (or to another process, or from kernel to userspace...). EDIT: Actually I have to apologize, because I’m not certain what the C standard says about whether the padding bytes can change w…