qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Philippe Mathieu-Daudé" <philmd@redhat.com>
To: Dov Murik <dovmurik@linux.vnet.ibm.com>,
	qemu-devel@nongnu.org, Eric Blake <eblake@redhat.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Markus Armbruster <armbru@redhat.com>
Subject: Re: [PATCH] checkpatch: Detect '%#' or '%0#' in printf-style format strings
Date: Mon, 14 Sep 2020 08:55:33 +0200	[thread overview]
Message-ID: <cc5a95d7-4efd-a3de-c1a2-0c77cb60c63a@redhat.com> (raw)
In-Reply-To: <20200914060109.69583-1-dovmurik@linux.vnet.ibm.com>

+qemu-perl team

On 9/14/20 8:01 AM, 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>
> ---
>  scripts/checkpatch.pl | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
> 
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index bd3faa154c..6ec2a9f6a1 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -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);
> 

Thank you for this patch!

What about folding it in the same block?

-- >8 --
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -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
---



  reply	other threads:[~2020-09-14  6:56 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-14  6:01 [PATCH] checkpatch: Detect '%#' or '%0#' in printf-style format strings Dov Murik
2020-09-14  6:55 ` Philippe Mathieu-Daudé [this message]
2020-09-14 11:27   ` Dov Murik
2020-09-14 13:00     ` Philippe Mathieu-Daudé
2020-09-22  9:22       ` Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cc5a95d7-4efd-a3de-c1a2-0c77cb60c63a@redhat.com \
    --to=philmd@redhat.com \
    --cc=armbru@redhat.com \
    --cc=dovmurik@linux.vnet.ibm.com \
    --cc=eblake@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).