Re: [PATCH v5 6/9] bug.h: Prevent double evaulation of in BUILD_BUG_ON

From: Daniel Santos
Date: Thu Nov 15 2012 - 14:11:07 EST


On 11/15/2012 09:07 AM, Borislav Petkov wrote:
> On Tue, Nov 13, 2012 at 04:13:38PM -0600, danielfsantos@xxxxxxx wrote:
>> When calling BUILD_BUG_ON in an optimized build using gcc 4.3 and later,
>> the condition will be evaulated twice, possibily with side-effects.
>> This patch eliminates that error.
>>
>> Signed-off-by: Daniel Santos <daniel.santos@xxxxxxxxx>
>> ---
>> include/linux/bug.h | 5 +++--
>> 1 files changed, 3 insertions(+), 2 deletions(-)
>>
>> diff --git a/include/linux/bug.h b/include/linux/bug.h
>> index 1b2465d..ccd44ce 100644
>> --- a/include/linux/bug.h
>> +++ b/include/linux/bug.h
>> @@ -58,8 +58,9 @@ struct pt_regs;
>> extern int __build_bug_on_failed;
>> #define BUILD_BUG_ON(condition) \
>> do { \
>> - ((void)sizeof(char[1 - 2*!!(condition)])); \
>> - if (condition) __build_bug_on_failed = 1; \
>> + bool __cond = !!(condition); \
>> + ((void)sizeof(char[1 - 2*!!(__cond)])); \
> No need for the !!(__cond) here because you've done it in the line
> above. IOW,
>
> bool __cond = !!(condition);
> ((void)sizeof(char[1 - 2 * __cond]));
>
> which is even a bit more readable, as a matter of fact.
Ah yes. I did notice that at one point, but I think it slipped my mind.
Also, the kernel has introduced me to the usage of the !! construct, of
which I'm well versed in its affects in various situations and how gcc's
optimizer ends up treating its usage, so probably another reason I
didn't change it immediately. But it's basically shorthand for the
expression (condition ? 1 : 0), correct? Or are there other details in
the C standard that makes it different in some cases?

Thanks,
Daniel
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/