Skip to content

Commit 522b837

Browse files
Alexey Dobriyantorvalds
authored andcommitted
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@avx2 Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com> Signed-off-by: Joe Perches <joe@perches.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 5b5e092 commit 522b837

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

scripts/checkpatch.pl

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,8 @@ sub possible {
18481848
sub show_type {
18491849
my ($type) = @_;
18501850

1851+
$type =~ tr/[a-z]/[A-Z]/;
1852+
18511853
return defined $use_type{$type} if (scalar keys %use_type > 0);
18521854

18531855
return !defined $ignore_type{$type};
@@ -5204,18 +5206,27 @@ sub process {
52045206
"Consecutive strings are generally better as a single string\n" . $herecurr);
52055207
}
52065208

5207-
# check for %L{u,d,i} and 0x%[udi] in strings
5208-
my $string;
5209+
# check for non-standard and hex prefixed decimal printf formats
5210+
my $show_L = 1; #don't show the same defect twice
5211+
my $show_Z = 1;
52095212
while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5210-
$string = substr($rawline, $-[1], $+[1] - $-[1]);
5213+
my $string = substr($rawline, $-[1], $+[1] - $-[1]);
52115214
$string =~ s/%%/__/g;
5212-
if ($string =~ /(?<!%)%[\*\d\.\$]*L[udi]/) {
5215+
# check for %L
5216+
if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
52135217
WARN("PRINTF_L",
5214-
"\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
5215-
last;
5216-
}
5217-
if ($string =~ /0x%[\*\d\.\$\Llzth]*[udi]/) {
5218-
ERROR("PRINTF_0xDECIMAL",
5218+
"\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5219+
$show_L = 0;
5220+
}
5221+
# check for %Z
5222+
if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5223+
WARN("PRINTF_Z",
5224+
"%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5225+
$show_Z = 0;
5226+
}
5227+
# check for 0x<decimal>
5228+
if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5229+
ERROR("PRINTF_0XDECIMAL",
52195230
"Prefixing 0x with decimal output is defective\n" . $herecurr);
52205231
}
52215232
}

0 commit comments

Comments
 (0)