qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] checkpatch: Detect '%#' or '%0#' in printf-style format strings
@ 2020-09-14 17:26 Dov Murik
  2020-09-14 17:49 ` Philippe Mathieu-Daudé
  2020-09-22 10:40 ` Paolo Bonzini
  0 siblings, 2 replies; 3+ messages in thread
From: Dov Murik @ 2020-09-14 17:26 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Paolo Bonzini, Dov Murik, Markus Armbruster

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>
---

Since v1:
- consolidate format string checks to avoid code duplication

---
 scripts/checkpatch.pl | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index bd3faa154c..f8dac953b2 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -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);
 			}
 		}
 
-- 
2.20.1



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] checkpatch: Detect '%#' or '%0#' in printf-style format strings
  2020-09-14 17:26 [PATCH v2] checkpatch: Detect '%#' or '%0#' in printf-style format strings Dov Murik
@ 2020-09-14 17:49 ` Philippe Mathieu-Daudé
  2020-09-22 10:40 ` Paolo Bonzini
  1 sibling, 0 replies; 3+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-09-14 17:49 UTC (permalink / raw)
  To: Dov Murik, qemu-devel; +Cc: Paolo Bonzini, Markus Armbruster

On 9/14/20 7:26 PM, Dov Murik wrote:
> 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>
> ---
> 
> Since v1:
> - consolidate format string checks to avoid code duplication
> 
> ---
>  scripts/checkpatch.pl | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index bd3faa154c..f8dac953b2 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -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);
>  			}
>  		}
>  
> 

Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] checkpatch: Detect '%#' or '%0#' in printf-style format strings
  2020-09-14 17:26 [PATCH v2] checkpatch: Detect '%#' or '%0#' in printf-style format strings Dov Murik
  2020-09-14 17:49 ` Philippe Mathieu-Daudé
@ 2020-09-22 10:40 ` Paolo Bonzini
  1 sibling, 0 replies; 3+ messages in thread
From: Paolo Bonzini @ 2020-09-22 10:40 UTC (permalink / raw)
  To: Dov Murik, Philippe Mathieu-Daudé, qemu-devel; +Cc: Markus Armbruster

On 14/09/20 19:26, Dov Murik wrote:
> 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>
> ---
> 
> Since v1:
> - consolidate format string checks to avoid code duplication
> 
> ---
>  scripts/checkpatch.pl | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index bd3faa154c..f8dac953b2 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -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);
>  			}
>  		}
>  
> 

Queued, thanks.

Paolo



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-09-22 10:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-09-14 17:26 [PATCH v2] checkpatch: Detect '%#' or '%0#' in printf-style format strings Dov Murik
2020-09-14 17:49 ` Philippe Mathieu-Daudé
2020-09-22 10:40 ` Paolo Bonzini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).