Live data from Hacker News

Stop Memsetting Structures

anmolsarma.in

1–10 of 277 posts

Re: Stop Memsetting Structures

#2
Does 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 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

#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. :)

Same. But also, I feel like most of The Force's efforts are directed at C++. I could be wrong.

Re: Stop Memsetting Structures

#6
C99 designated init doesn't work for C code that also needs to compile in C++ mode (...yet, a limited subset of designated init is coming in C++20, clang also seems to be more relaxed about this and allows the full C99 designated init also in C++ right now).

For 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
Another reason to not memset structures: In the code

    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

#8
The article gives very dangerous advice, especially for networking code. Padding will not be overwritten and when transmitting the struct (provided there is no attribute packed or the compiler ignores it) information will leak.

Yes, 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
post #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. :)

Same. But also, I feel like most of The Force's efforts are directed at C++. I could be wrong.

C++ programmers need to be converted. C programmers just need sympathy.

Re: Stop Memsetting Structures

#10
post #2

Does 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…

No, but the follow up article is called Stop Memcpying Structures. And the follow up to that Stop Memcmping Structures. :)
Post reply on HN