gcc options: -Wformat
gcc options: -Wformat
Reqruies
- Compiler:
gcc 2.8or later- glibc:
2.2or later
printf or scanf, it's common to encounter errors caused by passing incorrect argument types.A key option that helps catch these mistakes early during the compilation stage is -Wformat.
Argument Type and Count Check
-Wformat option detects actual errors.format_warning.cc
Compile Outputbash
unsigned long type is being printed with %d.In addition to argument types, it also displays warnings if the number of arguments required by the format string is insufficient, as shown below.
format_warning2.cc
Compile Outputbash
GCC Documentation for -Wformat
-Wformat option as follows.gcc-7.5.0/Warning-OptionsDoc-Wformat -Wformat=n
Check calls to printf and scanf, etc.,
to make sure that the arguments supplied have types appropriate to the format string specified, and that the conversions specified in the format string make sense.
This includes standard functions, and others specified by format attributes (see Function Attributes),
in the printf, scanf, strftime and strfmon (an X/Open extension, not in the C standard) families (or other target-specific families).
Which functions are checked without format attributes having been specified depends on the standard version selected, and such checks of functions without the attribute specified are disabled by -ffreestanding or -fno-builtin.
The formats are checked against the format features supported by GNU libc version 2.2.
These include all ISO C90 and C99 features, as well as features from the Single Unix Specification and some BSD and GNU extensions.
Other library implementations may not support all these features; GCC does not support warning about features that go beyond a particular library's limitations.
However, if -Wpedantic is used with -Wformat, warnings are given about format features not in the selected standard version (but not for strfmon formats, since those are not in any version of the C standard).
Supported Functions in glibc
You can find the list of supported functions by examining the GCC source code.
gcc/c-format.cc
printf, scanf, and strftime families.va_arg() as targets for -Wformat detection, instead of just standard glibc functions?In fact, I am explaining-Wformatfirst as it serves as the foundation for introducing__attribute__((format)).
Let's briefly examine them based on GCC 7.5.0.
-Wformat-contains-nul
\0) character embedded within the format string.sample source code - format_contains_nul.cc
compile and check resultbash
-Wformat-extra-args
sample source code - format_extra_args.cc
compile and check resultbash
-Wformat-overflow
sprintf might write data larger than the buffer size, potentially causing an overflow.sample source code - format_overflow.cc
compile and check resultbash
sample source code - format_overflow.cc
compile and check resultbash
argv value, but the default -Wformat-overflow option doesn't detect it.compile with -Wformat-overflow=2bash
-Wformat-zero-length
sample source code - format_zero_length.cc
compile and check resultbash
However, code like the following occurs surprisingly often.
sometimes, these ridiculous codes appearc
-Wformat-nonliteral
sample source code - format_nonliteral.cc
compile and check resultbash
fmt as const char will make this warning disappear.fmt) dynamically.-Wformat-security
sample source code - format_security.cc
compile and check resultbash
security is in the option name.%n. By manipulating arguments through stack overflows or other means, the code becomes vulnerable to exploits like a Format String Bug.Format String Bug Exploitation techniques in a separate post.-Wformat-signedness
signed format specifier is used with an unsigned argument, or vice versa.sample source code - format_signedness.cc
compile and check resultbash
-Wformat-truncation
snprintf, it detects and warns when data is truncated to fit the specified buffer size.sample source code - format_truncation.cc
compile and check resultbash