В GCC проверки строки формата контролируются параметром -Wformat
, который по умолчанию отключен.
Создание кода с помощью-Wformat
(или -Wall
, который включает его ), выдает предупреждение:
$ gcc -Wformat 630368.c -o 630368
630368.c: In function ‘main’:
630368.c:6:16: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘char *’ [-Wformat=]
scanf("%d", name);
~^ ~~~~
%hhd
(с GCC 8 )или
$ gcc -Wformat 630368.c -o 630368
630368.c: In function ‘main’:
630368.c:6:16: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘char *’ [-Wformat=]
6 | scanf("%d", name);
| ~^ ~~~~
| | |
| | char *
| int *
| %hhd
(с GCC 10 ).
Ubuntu поставляет GCC с пользовательскими спецификациями, которые включают -Wformat
по умолчанию; см.gcc -dumpspecs
:
*distro_defaults:
%{!fno-asynchronous-unwind-tables:-fasynchronous-unwind-tables} %{!fno-stack-protector:%{!fstack-protector-all:%{!ffreestanding:%{!nostdlib:%{!fstack-protector:-fstack-protector-strong}}}}} %{!Wformat:%{!Wformat=2:%{!Wformat=0:%{!Wall:-Wformat} %{!Wno-format-security:-Wformat-security}}}} %{!fno-stack-clash-protection:-fstack-clash-protection} %{!fcf-protection*:%{!fno-cf-protection:-fcf-protection}}
(в частности%{!Wformat:%{!Wformat=2:%{!Wformat=0:%{!Wall:-Wformat} %{!Wno-format-security:-Wformat-security}}}}
).