C implementation of Tic-Tac-Toe in a single call to printf (2020)
github.com
github.com
This also means that if you lazily stuff a user controller string into a printf() style function (instead of doing e.g. `printf("%s", str)`), this can be used as a gadget for arbitrary memory writes (in addition to arbitrary stack reads). For this reason, there are compiler warnings if you use a format string that isn't a string literal (IIRC now on by default in GCC 11) and some C libraries like dietlibc refuse to implement %n.
Programmers should be able to assume that printf isn't writing to memory under ordinary operation.
But looking at it now from a more practical point of view, I guess I can't see how that would happen, even with aggressive compiler optimisations (which are the usual thing to trigger really surprising consequences of undefined behaviours).
> The %n conversion specifier has serious security implications, so it was changed to no longer store the number of bytes written so far into the variable indicated by the pointer argument. Instead a syslog(3) message will be generated, after which the program is aborted with SIGABRT.
The authors did not only used printf() as a Turing-complete language, they also formatted their source code in a way pleasing to the eye. Cheers!
Don't forget coding is an art of fun and mystery, created to learn and play!
My personal favorite: https://www.ioccc.org/1998/banks.c
Hint: it's a flight sim :)
I've written a flight sim, too, but the code is not so beautiful like banks.c: https://github.com/reallew/voxelcopter