[Toybox] Min/Max bug fix

Andre Renaud andre at bluewatersys.com
Thu Mar 14 13:46:02 PDT 2013


There is a minor bug in the min/max macros that can result in
unexpected results when doing things like:
5 + min(a,b)
This currently essentially becomes
(5 + a) < (b) ? (a) : (b)
instead of:
5 + ((a < b) ? a : b)

Attached patch resolves it with the minimal changes
In general however, might not the following common min/max definitions
be more robust?
#define min(x,y) ({ \
        typeof(x) _x = (x);       \
        typeof(y) _y = (y);       \
        _x < _y ? _x : _y; })
#define max(x,y) ({ \
        typeof(x) _x = (x);       \
        typeof(y) _y = (y);       \
        _x > _y ? _x : _y; })

Regards,
Andre
-------------- next part --------------
A non-text attachment was scrubbed...
Name: min_max.patch
Type: application/octet-stream
Size: 509 bytes
Desc: not available
URL: <http://lists.landley.net/pipermail/toybox-landley.net/attachments/20130315/d12bd916/attachment-0006.obj>


More information about the Toybox mailing list