Commit 522b837c authored by Alexey Dobriyan's avatar Alexey Dobriyan Committed by Linus Torvalds

checkpatch: warn when formats use %Z and suggest %z

vsnprintf extension %Z<foo> is non-standard C.  Suggest the use of %z
instead.

Miscellanea:

 - Correct the misuse of type string PRINTF_0xDECIMAL type strings are
   supposed to be uppercase only. Fix this and add tr/[a-z]/[A-Z] to the
   type check in case I forget this again sometime in the future.

 - Improve the mechanism to find these defects so all 3 current checks
   are done on the format string

[joe@perches.com: correct the misuse of type string PRINTF_0xDECIMAL, improve the mechanism to find these defects]
  Link: http://lkml.kernel.org/r/4e3ad74b0c9dc229b06018a2d79655308ddbbebd.1484014173.git.joe@perches.com
Link: http://lkml.kernel.org/r/20170109235955.GA6787@avx2Signed-off-by: default avatarAlexey Dobriyan <adobriyan@gmail.com>
Signed-off-by: default avatarJoe Perches <joe@perches.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 5b5e0928
...@@ -1848,6 +1848,8 @@ my $prefix = ''; ...@@ -1848,6 +1848,8 @@ my $prefix = '';
sub show_type { sub show_type {
my ($type) = @_; my ($type) = @_;
$type =~ tr/[a-z]/[A-Z]/;
return defined $use_type{$type} if (scalar keys %use_type > 0); return defined $use_type{$type} if (scalar keys %use_type > 0);
return !defined $ignore_type{$type}; return !defined $ignore_type{$type};
...@@ -5204,18 +5206,27 @@ sub process { ...@@ -5204,18 +5206,27 @@ sub process {
"Consecutive strings are generally better as a single string\n" . $herecurr); "Consecutive strings are generally better as a single string\n" . $herecurr);
} }
# check for %L{u,d,i} and 0x%[udi] in strings # check for non-standard and hex prefixed decimal printf formats
my $string; my $show_L = 1; #don't show the same defect twice
my $show_Z = 1;
while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
$string = substr($rawline, $-[1], $+[1] - $-[1]); my $string = substr($rawline, $-[1], $+[1] - $-[1]);
$string =~ s/%%/__/g; $string =~ s/%%/__/g;
if ($string =~ /(?<!%)%[\*\d\.\$]*L[udi]/) { # check for %L
if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
WARN("PRINTF_L", WARN("PRINTF_L",
"\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
last; $show_L = 0;
} }
if ($string =~ /0x%[\*\d\.\$\Llzth]*[udi]/) { # check for %Z
ERROR("PRINTF_0xDECIMAL", if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
WARN("PRINTF_Z",
"%Z$1 is non-standard C, use %z$1\n" . $herecurr);
$show_Z = 0;
}
# check for 0x<decimal>
if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
ERROR("PRINTF_0XDECIMAL",
"Prefixing 0x with decimal output is defective\n" . $herecurr); "Prefixing 0x with decimal output is defective\n" . $herecurr);
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment