Skip to contentSkip to conversation

gcc extended format checking plugin

DISCUSSION David Lamparter 2 participants 2 messages Apr 19, 2020 Kbuild

lore
Share & tools
lore
#0 lore raw

Hello kernel build hackers,

in case it is of interest to anyone, we've recently added an extended format string checking plugin in the FRRouting project. We took inspiration from the Linux kernel and modified a printf (funnily enough, the one from FreeBSD) to support things like %pI4, and we wanted better warnings for this. Since the scheme is the same as in the Linux kernel, it may or may not be worth someone's time to take a look.

The plugin is found here:
https://github.com/FRRouting/frr/tree/master/tools/gcc-plugins

It's derived from gcc's "c-format.c", as such it may make sense to look at the diff instead of the straight source. The original GCC code is in the git history there. It's tested against gcc 9.3.0, YMMV on other versions.

Note that the plugin needs a single-line GCC source code modification:
https://github.com/FRRouting/frr/blob/master/tools/gcc-plugins/gcc-retain-typeinfo.patch
this is to stop GCC from prematurely stripping information, specifically from casts in function parameters. It does work without the change, but it may produce false-positive/negative warnings.

Aside from %pXX suffix support, the plugin also contains two other changes that are probably irrelevant to kernel land:
- it makes some typedefs "final" types since these are not guaranteed to be consistent in size across platforms and therefore can't be printf'd without a cast (e.g. pid_t, uid_t, time_t, etc.)
- it reserves %Lu for uint64_t

Please don't expect too much on its code quality, I'm not a GCC developer and essentially wrote this by trial and error ;)

Cheers,

-David

#1 lore raw

FW: GCC-plugin maintainers

Quoted 39 lines · David Lamparter

Hello kernel build hackers,

in case it is of interest to anyone, we've recently added an extended format string checking plugin in the FRRouting project. We took inspiration from the Linux kernel and modified a printf (funnily enough, the one from FreeBSD) to support things like %pI4, and we wanted better warnings for this. Since the scheme is the same as in the Linux kernel, it may or may not be worth someone's time to take a look.

The plugin is found here:
https://github.com/FRRouting/frr/tree/master/tools/gcc-plugins

It's derived from gcc's "c-format.c", as such it may make sense to look at the diff instead of the straight source. The original GCC code is in the git history there. It's tested against gcc 9.3.0, YMMV on other versions.

Note that the plugin needs a single-line GCC source code modification:
https://github.com/FRRouting/frr/blob/master/tools/gcc-plugins/gcc-retain-typeinfo.patch
this is to stop GCC from prematurely stripping information, specifically from casts in function parameters. It does work without the change, but it may produce false-positive/negative warnings.

Aside from %pXX suffix support, the plugin also contains two other changes that are probably irrelevant to kernel land:
- it makes some typedefs "final" types since these are not guaranteed to be consistent in size across platforms and therefore can't be printf'd without a cast (e.g. pid_t, uid_t, time_t, etc.)
- it reserves %Lu for uint64_t

Please don't expect too much on its code quality, I'm not a GCC developer and essentially wrote this by trial and error ;)

Cheers,

-David

Signature · 2 lines
Best Regards
Masahiro Yamada