This doesn't work with GCC 7. (Maybe it would work with GCC 8; the
release notes say that it is better at avoiding false positives.) For
integer specifiers like "%ld", the only thing I've found that works
without enlarging the buffer is to cast the argument to a smaller
integer type.
You can't do that; %ld requires a long argument, and casting results in undefined behavior.
For float specifiers like "%3.1f", even using a small
type doesn't seem to work. For example:
$ cat test.c
#include <stdio.h>
int
main ()
{
char buffer[16];
short a;
snprintf (buffer, 16, "%3.1f", a);
}
This is undefined behavior, as %f requires a double argument.