I understand snprintf will return a negative value when "an encoding error occurs"
But what is a simple example of such an "encoding error" that will produce that result?
I'm working with gcc 10.2.0 C compiler, and I've tried malformed format specifiers, unreasonably large numbers for field length, and even null format strings.
- Malformed format specifiers just get printed literally
- Unreasonably large numbers as length specifiers produce fatal errors
- Null format strings also produce fatal errors
This relates to repeatedly doing something like:
length += snprintf(...
to build up a formatted string.
That might be safe if it is certain not to return a negative value.
Advancing the buffer pointer by a negative length could cause it to go out of bounds. But I'm looking for a case where that would actually happen. If there is such a case then the added complexity of this may be warranted:
length += result = snprintf(...
So far I couldn't find a scenario where it would be worth adding complexity for a check of a value that the compiler may never produce. Maybe you can give a simple example of one.