a = b + c;What could possible go wrong? Let us list the ways:
- a, b, or c are the not the variables we want
- We specified addition, but we wanted something else
- The addition operation overflows
- a and b are unsigned, while c is signed and negative; the result becomes unsigned
- a has a smaller size than b or c; the assignment operation overflows
- a is signed while b or c are unsigned; a becomes negative while b+c are positive
- a is unsigned, while b and c are signed; again we have an overflow
- We used the wrong indentation level and people are unhappy about it
- to handle (3), we need trapping signed addition and trapping unsigned addition instructions
- to handle (4), we need a mixed signedness trapping add
- to handle (5), we need a trapping store unsigned and trapping store signed instructions, which check that the value in the register fits into the memory location specified
- ditto with (6) and (7)
So why are those instructions missing? In the 70's and 80's when the industry was ramping up performance was a much greater concern than security. Code was smaller and easier to audit; CPU cycles were longer and therefore more important to conserve; networks were small and private; truly malicious attacks were rare.
An unvirtuous cycle followed: C tried to make the most of exising processors, so its semantics mimic the instruction set of those days. It then became wildly successful, so processors were optimized for running C code; naturally they implemented or optimized instructions which directly translated to C concepts. This made C even more popular.
A pair of examples from the x86 world are the INTO and BOUND instructions. INTO (INTerrupt on Overflow) can follow an addition or subtraction instruction, effectively turning it into a trapping signed instruction. BOUND performs an array subscript bounds checking, trapping if the index is out of bounds. But the first implementations were rarely used, so they were not optimized in later iterations of the processor. Finally, the 64-bit extensions to the x86 instruction set removed those two instructions for good.