qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: Peter Maydell <peter.maydell@linaro.org>, qemu-devel@nongnu.org
Cc: Richard Henderson <richard.henderson@linaro.org>, patches@linaro.org
Subject: Re: [Qemu-devel] [PATCH] coccinelle: new inplace-byteswaps.cocci to remove inplace-byteswapping calls
Date: Tue, 9 Oct 2018 13:23:07 -0500	[thread overview]
Message-ID: <a7f3a1b3-7c06-b82b-3201-4db1175ee8f0@redhat.com> (raw)
In-Reply-To: <20181009181612.10633-1-peter.maydell@linaro.org>

On 10/9/18 1:16 PM, Peter Maydell wrote:
> Add a new Coccinelle script which replaces uses of the inplace
> byteswapping functions *_to_cpus() and cpu_to_*s() with their
> not-in-place equivalents. This is useful for where the swapping
> is done on members of a packed struct -- taking the address
> of the member to pass it to an inplace function is undefined
> behaviour in C.
> 
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
> Richard asked for a coccinelle script in the scripts/coccinelle
> directory, so here's a patch to add it.
> 
>   scripts/coccinelle/inplace-byteswaps.cocci | 65 ++++++++++++++++++++++
>   1 file changed, 65 insertions(+)
>   create mode 100644 scripts/coccinelle/inplace-byteswaps.cocci
> 
> diff --git a/scripts/coccinelle/inplace-byteswaps.cocci b/scripts/coccinelle/inplace-byteswaps.cocci
> new file mode 100644
> index 00000000000..a869a90cbfd
> --- /dev/null
> +++ b/scripts/coccinelle/inplace-byteswaps.cocci
> @@ -0,0 +1,65 @@
> +// Replace uses of in-place byteswapping functions with calls to the
> +// equivalent not-in-place functions.  This is necessary to avoid
> +// undefined behaviour if the expression being swapped is a field in a
> +// packed struct.
> +
> +@@
> +expression E;
> +@@
> +-be16_to_cpus(&E);
> ++E = be16_to_cpu(E);
> +@@
> +expression E;
> +@@
> +-be32_to_cpus(&E);
> ++E = be32_to_cpu(E);

It's probably possible to shorten this as:

@@
expression E;
@@
(
-be16_to_cpus(&E);
+E = be16_to_cpu(E);
|
-be32_to_cpus(&E);
+E = be32_to_cpu(E);
...
)

But I'm also okay if the long form gets checked in.

Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org

  reply	other threads:[~2018-10-09 18:23 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-09 18:16 [Qemu-devel] [PATCH] coccinelle: new inplace-byteswaps.cocci to remove inplace-byteswapping calls Peter Maydell
2018-10-09 18:23 ` Eric Blake [this message]
2018-10-09 18:35   ` Peter Maydell
2018-10-10  9:01     ` Philippe Mathieu-Daudé
2018-10-10 14:26 ` Richard Henderson

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=a7f3a1b3-7c06-b82b-3201-4db1175ee8f0@redhat.com \
    --to=eblake@redhat.com \
    --cc=patches@linaro.org \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.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).