Re: [PATCH v2] overflow: Add __must_check attribute to check_*() helpers

From: Kees Cook
Date: Mon Aug 17 2020 - 15:37:03 EST


On Mon, Aug 17, 2020 at 11:08:54AM +0200, David Sterba wrote:
> On Sat, Aug 15, 2020 at 10:09:24AM -0700, Kees Cook wrote:
> > +static inline bool __must_check __must_check_overflow(bool overflow)
> > +{
> > + return unlikely(overflow);
>
> How does the 'unlikely' hint propagate through return? It is in a static
> inline so compiler has complete information in order to use it, but I'm
> curious if it actually does.

It may not -- it depends on how the compiler decides to deal with it. :)

> In case the hint gets dropped, the fix would probably be
>
> #define check_add_overflow(a, b, d) unlikely(__must_check_overflow(({ \
> typeof(a) __a = (a); \
> typeof(b) __b = (b); \
> typeof(d) __d = (d); \
> (void) (&__a == &__b); \
> (void) (&__a == __d); \
> __builtin_add_overflow(__a, __b, __d); \
> })))

Unfortunately not, as the unlikely() ends up eating the __must_check
attribute. :(

--
Kees Cook