What is the expected behaviour for this code snippet?
char * aNullPointer = 0;
snprintf (res, 128, "Testing %s null pointer",aNullPointer);
Note that I am deliberately trying to get it to de-reference my null pointer aNullPointer.
Behaviour 1) res points to a string “Testing (null) null pointer”
Behaviour 2) Seg Fault
It seems I get differing behaviours depending on my platform. Some snprintf implementations perform a sanity check, whereas others do not.
What is the most common behaviour?
It’s undefined behavior – there’s nothing to expect. The fact that some implementations check for
NULLand replace it with “nil” or “null” is just a nicety, you can’t rely on it at all.