4 ms·
The C and C++ standards people are finally trying to deal with their safety problems, decades too late. At least they're trying. Some of this may be an attempt
by Animats 10y ago
The C and C++ standards people are finally trying to deal with their safety problems, decades too late. At least they're trying. Some of this may be an attempt to catch up to Rust.
The "trap values" concept is interesting. They're thinking that some floating point values are signaling NaNs, which will cause an exception if processed.
One way to approach this is to divide types into "fully mapped", where all bit patterns map to a valid value, and "partially mapped", where they don't. All the integer types, "char", "short", "int", "long", etc. are fully mapped on modern machines. "bool" isn't. Floating point isn't. Pointers are not. Enums are not. Structs are fully mapped if all their fields are fully mapped.
Initializing a fully mapped type with junk won't break the C/++ machine model. That's not true of partially mapped types. So this could be enforced in casts. Casting to fully mapped types could be permitted as at present, but casting to a partially mapped type should require something visibly unsafe, like C++'s "reinterpret_cast". In C, turning the raw memory from "malloc" into a partially mapped type should be a visible act. In C++, all partially mapped fields should be required to be initialized in the constructor. It's OK at the language level if fully mapped fields start as junk.
Requiring that pointers be initialized would be good for safety. Failure to initialize a pointer is usually a bug. Sometimes it's an entry point for an exploit.
- panic 10y agoIIRC signaling NaNs aren't trap representations: they're fully-defined values which support operations like isnan().
- brandmeyer 10y agoCorrect. For more detail: the only mandated "signaling" for a signaling NaN is that it sets the sticky Invalid bit in the floating-point status register (aka Invalid exception). It is up to the host processor as to whether it is possible to get a signal on changes to the floating-point exception bits. Some CPU's don't have the ability at all.
- minipci1321 10y ago> Structs are fully mapped if all their fields are fully mapped. I recall some SPARC arch (uSparc?) circa 199x that would place chars and shorts in a struct as if it were an int (i.e, aligned on boundary of 4). Then, when loading this field from the structure, it would load a full word, then shift it 16 or 24 bits left then right, to propagate sign or 0 and squeeze out undefined bits. Interestingly enough, for programming it we used Sun Workbench which had some rudimentary equivalent of valgrind. So if in my code I would initialize the struct by initializing every attribute, then copy the entire structure using memcpy, the tool would complain about reads from uninitialised -- exactly because of the paddings of shorter types.
- _kst_ 10y ago> The "trap values" concept is interesting. That's what the article calls them, but the Standard uses the term "trap representations". The point is that a trap representation does not represent a value. (A representation is the bit sequence stored in an object. A value is more abstract; it's a member of the set of possible values of a type. A value can have more than one representation.)
- cesarb 10y ago> The "trap values" concept is interesting. They're thinking that some floating point values are signaling NaNs, which will cause an exception if processed. And then there's Itanium: https://blogs.msdn.microsoft.com/oldnewthing/20040119-00/?p=41003 https://blogs.msdn.microsoft.com/oldnewthing/20040119-00/?p=... Consider that any particular local variable might be allocated to a register. If the local variable has not been initialized, it might have junk from a previous use, and that junk might have the NaT bit set. Reading from that local variable, or even spilling it to the stack, can cause an exception.
- jabl 10y agoIn theory it might be possible to avoid trap representations for _Bool if one would specify that it would be like other unsigned integer types, i.e. 0 is false and non-zero is true, instead of the current situation where 0 is false, 1 is true and anything else is a trap representation. Now this would break at least one possible optimization that at least GCC does, namely that if you know that the value is either 1 or 0, then "!x" can be implemented as "x^1" (bitwise xor the value with 1) instead of a branch. I'm not sure that this optimization is particularly significant in real world code, though.