OSDN Git Service

checkpatch: Detect '%#' or '%0#' in printf-style format strings
authorDov Murik <dovmurik@linux.vnet.ibm.com>
Mon, 14 Sep 2020 17:26:23 +0000 (17:26 +0000)
committerPaolo Bonzini <pbonzini@redhat.com>
Wed, 30 Sep 2020 17:11:36 +0000 (19:11 +0200)
According to the coding style document, we should use literal '0x' prefix
instead of printf's '#' flag (which appears as '%#' or '%0#' in the format
string).  Add a checkpatch rule to enforce that.

Note that checkpatch already had a similar rule for trace-events files.

Example usage:

  $ scripts/checkpatch.pl --file chardev/baum.c
  ...
  ERROR: Don't use '#' flag of printf format ('%#') in format strings, use '0x' prefix instead
  #366: FILE: chardev/baum.c:366:
  +            DPRINTF("Broken packet %#2x, tossing\n", req); \
  ...
  ERROR: Don't use '#' flag of printf format ('%#') in format strings, use '0x' prefix instead
  #472: FILE: chardev/baum.c:472:
  +        DPRINTF("unrecognized request %0#2x\n", req);
  ...

Signed-off-by: Dov Murik <dovmurik@linux.vnet.ibm.com>
Message-Id: <20200914172623.72955-1-dovmurik@linux.vnet.ibm.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
scripts/checkpatch.pl

index ca4445f..6ed3497 100755 (executable)
@@ -2880,14 +2880,20 @@ sub process {
                                $herecurr);
                }
 
-# check for %L{u,d,i} in strings
+# format strings checks
                my $string;
                while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
                        $string = substr($rawline, $-[1], $+[1] - $-[1]);
                        $string =~ s/%%/__/g;
+                       # check for %L{u,d,i} in strings
                        if ($string =~ /(?<!%)%L[udi]/) {
                                ERROR("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
-                               last;
+                       }
+                       # check for %# or %0# in printf-style format strings
+                       if ($string =~ /(?<!%)%0?#/) {
+                               ERROR("Don't use '#' flag of printf format " .
+                                     "('%#') in format strings, use '0x' " .
+                                     "prefix instead\n" . $herecurr);
                        }
                }