mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: Sascha Hauer <s.hauer@pengutronix.de>
Cc: barebox@lists.infradead.org
Subject: Re: [PATCH 2/2] of: fix side effect in macro expansion
Date: Wed, 29 Apr 2020 10:47:20 +0200	[thread overview]
Message-ID: <41e1de8d-dd7e-6569-3960-47ebfbd0461b@pengutronix.de> (raw)
In-Reply-To: <20200429084147.GG5877@pengutronix.de>



On 4/29/20 10:41 AM, Sascha Hauer wrote:
> On Wed, Apr 29, 2020 at 10:26:27AM +0200, Ahmad Fatoum wrote:
>> On 4/29/20 10:00 AM, Sascha Hauer wrote:
>>> On Wed, Apr 29, 2020 at 08:40:40AM +0200, Ahmad Fatoum wrote:
>>>> On little endian platforms that don't define __arch_swab32p, be32_to_cpu
>>>> ultimately expands to an expression that evaluates its argument multiple
>>>> times. Because of the increment, this is wrong (and UB because of
>>>> unsequenced modification).
>>>> Hoist the side effect out of the macro argument.
>>>
>>> I'm not sure if the reasoning is correct. That would mean that
>>> of_read_number() doesn't work on ARM, but I can assure it actually does.
>>
>> Typo, sorry. arch needs to define __arch_swab32 (no p at the end).
>>
>> of_read_number -> be32_to_cpu -> __be32_to_cpu -> __swab32 -> __fswab32
>>
>> then
>>
>> static inline __attribute_const__ __u32 __fswab32(__u32 val)
>> {
>> #ifdef __arch_swab32
>>         return __arch_swab32(val);
>> #else
>>         return ___constant_swab32(val);
>> #endif
>> }
>>
>> where ___constant_swab32 is the macro that evaluates the argument
>> multiple times.
> 
> __fswab32 is a function, for the caller it doesn't matter how often
> ___constant_swab32 evaluates its argument.

Oh. Looks like a false positive. cppcheck thinks we could reach
__constant_swab32, which is a macro, but it's only reachable for
constant expressions, which are always side-effect free.

Please dismiss. I'll send a v2 with corrected reasoning in the commit
message.

Cheers
Ahmad

> 
> Sascha
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  reply	other threads:[~2020-04-29  8:47 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-29  6:40 [PATCH 1/2] scripts: imd: fix uninitialized variable read Ahmad Fatoum
2020-04-29  6:40 ` [PATCH 2/2] of: fix side effect in macro expansion Ahmad Fatoum
2020-04-29  8:00   ` Sascha Hauer
2020-04-29  8:26     ` Ahmad Fatoum
2020-04-29  8:41       ` Sascha Hauer
2020-04-29  8:47         ` Ahmad Fatoum [this message]
2020-04-29  7:38 ` [PATCH 1/2] scripts: imd: fix uninitialized variable read Sascha Hauer
2020-05-04  7:38   ` Steffen Trumtrar

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=41e1de8d-dd7e-6569-3960-47ebfbd0461b@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=s.hauer@pengutronix.de \
    /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