3 ms·
If anyone reading this and feels uncertain on how this is done, DON'T do this: snprintf(dest, sizeof dest, source); // BAD code do not repeat that looks
by unwind 4y ago
If anyone reading this and feels uncertain on how this is done, DON'T do this:
snprintf(dest, sizeof dest, source); // BAD code do not repeat
that looks great at first sight, just another size-checked way of copying strings, but remember that the third argument to `snprintf()` [1] is of course a `printf()`-style formatting string. So if that `source` argument contains any percent symbols, there's gonna be a party in your computer and both Undefined and Behavior are going to show up. You don't want that.
Instead, if you want to use `snprintf()` for this, remember to do:
snprintf(dest, sizeof dest, "%s", source);
[1]: https://linux.die.net/man/3/snprintf https://linux.die.net/man/3/snprintf
- Karellen 4y agoThanks for mentioning this. I didn't include it in my comment because I originally thought it was too obvious, but considering it now, it probably was worth making explicit.
- unwind 4y agoI blame Stack Overflow for conditioning me into seeing more of the possible ways things could break, when it comes to C code. :)
- jbboehr 4y ago`-Wformat-security` [0] to the rescue! [0]: https://fedoraproject.org/wiki/Format-Security-FAQ https://fedoraproject.org/wiki/Format-Security-FAQ