Chapter 1. Systems Programmers Can Have Nice Things
In certain contexts—for example the context Rust is targeting—being 10x or even 2x faster than the competition is a make-or-break thing. It decides the fate of a system in the market, as much as it would in the hardware market.
All computers are now parallel... Parallel programming is programming.
Michael McCool et al., Structured Parallel Programming
TrueType parser flaw used by nation-state attacker for surveillance; all software is security-sensitive.
We chose to open our book with the three quotes above for a reason. But let’s start with a mystery. What does the following C program do?
intmain(intargc,char**argv){unsignedlonga[1];a[3]=0x7ffff7b36cebUL;return0;}
On Jim’s laptop this morning, this program printed:
undef: Error: .netrc file is readable by others.undef: Remove password or make file unreadable by others.
Then it crashed. If you try it on your machine, it may do something else. What’s going on here?
The program is flawed. The array a is only one element long, so using a[3] is, according to the C programming language standard, undefined behavior:
Behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements
Undefined behavior doesn’t just have an unpredictable result: the standard explicitly permits the program to do anything at all. In our case, storing this particular value ...