by Aanok · 5y ago · 0 comments
A fun exercise you can do is put a "%s" in the format string, omit the string argument and see what happens to the stack.
I'd say the usual trap is rather the size of the target buffer, because that requires bigger static analysis guns. (I'm ignoring things like "%n", because then you're playing with fire already.)
https://gcc.gnu.org/onlinedocs/gcc-3.2/gcc/Function-Attribut...
There's options other than printf too.
char buf[2];
sprintf(buf, "%d", n);
This will happily write to buf[2] and beyond if n is negative or greater than 9.