Skip to contentSkip to conversation

checkpatch: Detect '%#' or '%0#' in printf-style format strings

PATCH Superseded Dov Murik 3 participants 5 messages Sep 14, 2020 – Sep 22, 2020 Checkpatch

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Dov Murik

author
#0 lore raw

PATCH 1/1 checkpatch: Detect '%#' or '%0#' in printf-style format strings

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);
  ...
---
 scripts/checkpatch.pl | 12 ++++++++++++
 1 file changed, 12 insertions(+)
scripts/checkpatch.pl +12 -0
index bd3faa154c..6ec2a9f6a1 100755
@@ -2891,6 +2891,18 @@ sub process {
 			}
 		}
 
+# check for %# or %0# in printf-style format strings
+		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
+			my $string = substr($rawline, $-[1], $+[1] - $-[1]);
+			$string =~ s/%%/__/g;
+			if ($string =~ /(?<!%)%0?#/) {
+				ERROR("Don't use '#' flag of printf format " .
+				      "('%#') in format strings, use '0x' " .
+				      "prefix instead\n" . $herecurr);
+				last;
+			}
+		}
+
 # QEMU specific tests
 		if ($rawline =~ /\b(?:Qemu|QEmu)\b/) {
 			ERROR("use QEMU instead of Qemu or QEmu\n" . $herecurr);
Signature · 1 line
2.20.1
#1 lore raw

+qemu-perl team

Quoted 38 lines · Dov Murik

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);
  ...
---
 scripts/checkpatch.pl | 12 ++++++++++++
 1 file changed, 12 insertions(+)
scripts/checkpatch.pl +5 -0
index bd3faa154c..6ec2a9f6a1 100755
@@ -2891,6 +2891,18 @@ sub process {
 			}
 		}
 
+# check for %# or %0# in printf-style format strings
+		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
+			my $string = substr($rawline, $-[1], $+[1] - $-[1]);
+			$string =~ s/%%/__/g;
+			if ($string =~ /(?<!%)%0?#/) {

scripts/checkpatch.pl sub process { · line 2908

Quoted 10 lines
scripts/checkpatch.pl +7 -0
+				ERROR("Don't use '#' flag of printf format " .
+				      "('%#') in format strings, use '0x' " .
+				      "prefix instead\n" . $herecurr);
+				last;
+			}
+		}
+
 # QEMU specific tests
 		if ($rawline =~ /\b(?:Qemu|QEmu)\b/) {
 			ERROR("use QEMU instead of Qemu or QEmu\n" . $herecurr);

Thank you for this patch!

What about folding it in the same block?

-- >8 --

scripts/checkpatch.pl +2 -1
@@ -2880,15 +2880,22 @@ 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;
                        }

+ if ($string =~ /(?<!%)%0?#/) {
+ ERROR("Don't use '#' flag of printf
format " .
+ "('%#') in format strings, use
'0x' " .
+ "prefix instead\n" . $herecurr);
+ last;
+ }

                }

# QEMU specific tests
---

author
#2 lore raw
Quoted 54 lines · Philippe Mathieu-Daudé

+qemu-perl team

Quoted 38 lines · Dov Murik

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);
   ...
---
  scripts/checkpatch.pl | 12 ++++++++++++
  1 file changed, 12 insertions(+)
scripts/checkpatch.pl +5 -0
index bd3faa154c..6ec2a9f6a1 100755
@@ -2891,6 +2891,18 @@ sub process {
  			}
  		}
 
+# check for %# or %0# in printf-style format strings
+		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
+			my $string = substr($rawline, $-[1], $+[1] - $-[1]);
+			$string =~ s/%%/__/g;
+			if ($string =~ /(?<!%)%0?#/) {

scripts/checkpatch.pl sub process { · line 2908

Quoted 10 lines
scripts/checkpatch.pl +7 -0
+				ERROR("Don't use '#' flag of printf format " .
+				      "('%#') in format strings, use '0x' " .
+				      "prefix instead\n" . $herecurr);
+				last;
+			}
+		}
+
  # QEMU specific tests
  		if ($rawline =~ /\b(?:Qemu|QEmu)\b/) {
  			ERROR("use QEMU instead of Qemu or QEmu\n" . $herecurr);

Thank you for this patch!

Quoted 2 lines

What about folding it in the same block?

That makes sense, except that 'last' statement which will escape the loop if one of the bad patterns is found.

Maybe we can just drop 'last' from both if-then blocks? We'll get multiple alerts if bad patterns are used more than once in the same line, which sounds OK to me.

Quoted 32 lines

-- >8 --

scripts/checkpatch.pl +4 -1
@@ -2880,15 +2880,22 @@ 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;
                         }
+                       if ($string =~ /(?<!%)%0?#/) {
+                               ERROR("Don't use '#' flag of printf

format " .
+ "('%#') in format strings, use
'0x' " .

scripts/checkpatch.pl +3 -1
+                                     "prefix instead\n" . $herecurr);
+                               last;
+                       }
                 }
 
  # QEMU specific tests
---
#3 lore raw
Quoted 61 lines · Dov Murik
Quoted 57 lines · Philippe Mathieu-Daudé

+qemu-perl team

Quoted 42 lines · Dov Murik

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);
   ...

---
  scripts/checkpatch.pl | 12 ++++++++++++

  1 file changed, 12 insertions(+)
scripts/checkpatch.pl +0 -0
index bd3faa154c..6ec2a9f6a1 100755
@@ -2891,6 +2891,18 @@ sub process {
               }
           }

  +# check for %# or %0# in printf-style format strings +        while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
+            my $string = substr($rawline, $-[1], $+[1] - $-[1]); +            $string =~ s/%%/__/g;
+            if ($string =~ /(?<!%)%0?#/) {
+                ERROR("Don't use '#' flag of printf format " . +                      "('%#') in format strings, use '0x' " .

Quoted 9 lines

+                      "prefix instead\n" . $herecurr);
+                last;
+            }
+        }
+
  # QEMU specific tests
          if ($rawline =~ /\b(?:Qemu|QEmu)\b/) {
              ERROR("use QEMU instead of Qemu or QEmu\n" . $herecurr);

Thank you for this patch!

Quoted 2 lines

What about folding it in the same block?

Quoted 6 lines

That makes sense, except that 'last' statement which will escape the loop if one of the bad patterns is found.

Maybe we can just drop 'last' from both if-then blocks?  We'll get multiple alerts if bad patterns are used more than once in the same line, which sounds OK to me.

No objection.

Quoted 36 lines
Quoted 35 lines

-- >8 --

scripts/checkpatch.pl +1 -1
@@ -2880,15 +2880,22 @@ 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;
                         }
+                       if ($string =~ /(?<!%)%0?#/) {
+                               ERROR("Don't use '#' flag of printf format " .
+                                     "('%#') in format strings, use '0x' " .
+                                     "prefix instead\n" . $herecurr); +                               last;
+                       }
                 }

  # QEMU specific tests
---

#4 lore raw
Quoted 8 lines · Philippe Mathieu-Daudé
Quoted 6 lines

That makes sense, except that 'last' statement which will escape the loop if one of the bad patterns is found.

Maybe we can just drop 'last' from both if-then blocks?  We'll get multiple alerts if bad patterns are used more than once in the same line, which sounds OK to me.

No objection.

Sounds good.

Paolo