The problem that we should be solving is one found in the code, not a perceived one in the language spec. If it doesn't fix a problem found in code, it isn't really a problem worth fixing.
What code problem does your proposal fix? If nothing else, give me a snippet of (otherwise well-constructed, meaningful) code that suffers due to signed overflow UB but would no longer suffer if your proposed language change was adopted.
The problem that we should be solving is one found in the code, not a perceived one in the language spec
This is a question of numbers. There is one standard's body, a few compiler writers, and millions of regular programmers. The vision that the spec is always right and the programmer is always wrong is ludicrous. Specifically, when thousands of programmers fall prey to the same pitfall again and again, that's a strong hint that the spec provides a poor user interface.
If nothing else, give me a snippet of (otherwise well-constructed, meaningful) code that suffers due to signed overflow UB but would no longer suffer if your proposed language change was adopted.
Not signed overflow, but left shift on negative numbers (which may be viewed as a special case of signed overflow), affects curve25519, by Daniel J. Bernstein. Specifically, the carry propagation code in the TweetNaCl and SUPERCOP Ref10.
The relevant code looks like this (carry propagation after a multiplication, there are other instances):
When c0..c9 are negative (which always happens), the left shifts you see at the right-most instruction is undefined. (Yes, -42 << 3 is undefined.) One way to fix this is to convert to unsigned first:
This is not portable, because it relies on a 2's complement representation. But at least it's not undefined any more. An alternative would be to multiply by 1<<25 and 1<<26 respectively. It's portable, but even less readable:
And no, the fact the code can be fixed doesn't mean the standard is right. The standard makes me write uglier code, and that's undoubtedly bad. The question is whether the alternative has even worse consequences —I don't believe it has.
The general rule is use signed for arithmetic and unsigned for bit patterns. This code mixes them which typically when you run into these sorts of problems.
This code is already non-portable because of the assumption of 2s complement (meh) and the right-shift of negative values (again, don't mix arithmetic and bit-manipulating).
Long story short, this code makes several assumptions about what the platform will do. If the language made signed overflow inconveniently defined or even implementation defined, this code would still be broken.
Could the language do more to help you here? Absolutely. But the answer is not assigning arbitrary meaning to operations that don't intrinsically have a clear meaning.
If the language made signed overflow inconveniently defined or even implementation defined, this code would still be broken.
Note that the final version of the code doesn't overflow at all, because the limbs are small enough for the operand to never exceed 63 bits.
the answer is not assigning arbitrary meaning to operations that don't intrinsically have a clear meaning.
Again, there is a strong consensus on the meaning of signed right shift: nearly all platforms use the arithmetic shift, that propagates the sign bit. Such a consensus is almost as good as an "intrinsically clear meaning".
And if some crazy platform doesn't support arithmetic shift, the behaviour will still be defined, and the tests will instantly and cleanly fail.
don't mix arithmetic and bit-manipulating
Well meaning, widely applicable, but ultimately useless advice: this is crypto we're talking about. I need my code to run fast. I need my code to run in constant time (with respect to secrets). And most of all, I need to make the standard algorithms work without reinventing the wheel, and the experts have already settled on slightly non-portable designs. They know they were "mixing arithmetic and bit-manipulation", but did it anyway because that's what works best.
1
u/industry7 Jun 01 '17
The general problem is undefined behavior... Defining the behavior fixes the problem... by defining the behavior.
What's your question again?