News for hackers. Stuff that matters.
Re:The Lost Art of Structure Packing (2018) by kazinator on Monday April 27, 2020 @03:32PM (#22999122)
memset_s is a pointless misinvention. I can't think of any situation in which I would use memset, and not want it to be what memset_s is claimed to be.

Which is to say that memset_s should just be called memset; there is no need for memset to be doing stupid things so that people must use memset_s.

I have no plans to use memset_s (ever), or to upstream any fix that involves using it unless the author provides a repro test case, and a proof that the problem can't be fixed with compiler options that make the problem go away with memset.

In the worst imaginable scenario, I will #define memset memset_s everywhere (after the inclusion of <string.h> of course, not before, and an #undef memset).

(The #undef memset may be enough, in fact, if the only problem is that memset is #define'd to some compiler built-in that doesn't properly implement classic C90 memset in all cases.)

Re:The Lost Art of Structure Packing (2018) by rurban on Monday April 27, 2020 @05:04PM (#23000012)
Agree. memset should be secure by default. That means it should not be optimized away by the compiler. And second the secure variant should flush the content, so that cache attacks can not read secrets which were memset'ed, but still in the cache on broken CPU's (Intel).
Re:The Lost Art of Structure Packing (2018) by saagarjha on Monday April 27, 2020 @09:45PM (#23002432)
sloooooooooooooooow. No, really, do you want a full flush every time you do a memset? That would be horrendous for performance.
Re:The Lost Art of Structure Packing (2018) by kazinator on Tuesday April 28, 2020 @06:03PM (#23012681)
"And second the secure variant should flush ..."

Probably means the extra-secure variant, more secure than the regular non-optimized-away memset that really clears the memory, but without the flush.

Re:The Lost Art of Structure Packing (2018) by saagarjha on Tuesday April 28, 2020 @08:19PM (#23013785)
Yeah, that's a more reasonable interpretation.
Re:The Lost Art of Structure Packing (2018) by saagarjha on Monday April 27, 2020 @04:35PM (#22999750)
Most of the Annex K * _s functions are pretty stupid, to be honest, but this is one that is actually useful. It's not that memset is doing stupid things; rather it's being smart and skipping work that it's not required to do.
Re:The Lost Art of Structure Packing (2018) by kazinator on Monday April 27, 2020 @05:17PM (#23000146)
memset has always been required to set N bytes starting at a given address to zero. That's the requirement. Been that way since it appeared in AT&T Unix and beat BSD's bzero to the ANSI C punch.

There is no need for a broken memset that fails to set some of the bytes, so that a fixed one under a different name has to be used in its place.

If you're using memset such that it's okay for memset not to set some of the bytes, and you'd like them not to be set if that makes things faster, then you shouldn't be using memset. You're using a hammer to drive a screw: wrong tool.

C has perfectly good initialization and assignment for structures.

End of story; I'm going to walk away pretend I never read this subthread, re-joining the hordes of C programmers using memset in the normal way, adding to the countless lines of code that do it that way and are never going to be changed.

This bullshit will be backpedaled out of the standard eventually, you just wait.

Re:The Lost Art of Structure Packing (2018) by saagarjha on Tuesday April 28, 2020 @01:19AM (#23003660)
…and assigning to a variable is supposed to put some bytes at a certain address, yes. Except compilers will skip doing so if you never read from the variable again, and the same thing happens here: the compiler can "know" that it has no need to actually do the assignment, as writing to padding bytes is not required to actually do anything and trying to observe the value is currently the subject of debate but somewhere between "undefined" and "whatever you get out of it has nothing to do with what you think went in".