[PATCH] Avoid double argument evaluation in MIN and MAX macros
Rich Felker
dalias at aerifal.cx
Thu Apr 3 01:20:50 UTC 2014
On Wed, Apr 02, 2014 at 12:07:19PM +0200, Denys Vlasenko wrote:
> On Mon, Mar 31, 2014 at 12:08 AM, Bartosz Golaszewski
> <bartekgola at gmail.com> wrote:
> > Commit c3a27b0b fixes a double argument evaluation by modifying the
> > macro invocation. What about preventing it on macro definition level?
> >
> > Signed-off-by: Bartosz Golaszewski <bartekgola at gmail.com>
> > ---
> > include/libbb.h | 22 +++++++++++++++-------
> > util-linux/swaponoff.c | 5 ++---
> > 2 files changed, 17 insertions(+), 10 deletions(-)
> >
> > diff --git a/include/libbb.h b/include/libbb.h
> > index 1cbe2c8..db75641 100644
> > --- a/include/libbb.h
> > +++ b/include/libbb.h
> > @@ -265,13 +265,21 @@ struct BUG_off_t_size_is_misdetected {
> > #define SKIP ((int) 2)
> >
> > /* Macros for min/max. */
> > -#ifndef MIN
> > -#define MIN(a,b) (((a)<(b))?(a):(b))
> > -#endif
> > -
> > -#ifndef MAX
> > -#define MAX(a,b) (((a)>(b))?(a):(b))
> > -#endif
> > +#undef MIN
> > +#define MIN(a,b) \
> > + ({ \
> > + __typeof__(a) _a = (a); \
> > + __typeof__(b) _b = (b); \
> > + _a < _b ? _a : _b; \
> > + })
> > +
> > +#undef MAX
> > +#define MAX(a,b) \
> > + ({ \
> > + __typeof__(a) _a = (a); \
> > + __typeof__(b) _b = (b); \
> > + _a > _b ? _a : _b; \
> > + })
>
> typeof() is GCCism. I am wary of using it unless it's necessary.
In addition I think everybody's used to MIN/MAX being macros subject
to multiple evaluation...
Rich
More information about the busybox
mailing list