Message ID | 20220311054603.57328-3-liwang@redhat.com |
---|---|
State | Accepted |
Headers | show |
Series | refine the MIN/MAX macros | expand |
Hi Li,
looks reasonable.
Reviewed-by: Petr Vorel <pvorel@suse.cz>
Kind regards,
Petr
diff --git a/include/tst_minmax.h b/include/tst_minmax.h index 6417dd703..9d7d596fc 100644 --- a/include/tst_minmax.h +++ b/include/tst_minmax.h @@ -9,6 +9,7 @@ # define MIN(a, b) ({ \ typeof(a) _a = (a); \ typeof(b) _b = (b); \ + (void) (&_a == &_b); \ _a < _b ? _a : _b; \ }) #endif /* MIN */ @@ -17,6 +18,7 @@ # define MAX(a, b) ({ \ typeof(a) _a = (a); \ typeof(b) _b = (b); \ + (void) (&_a == &_b); \ _a > _b ? _a : _b; \ }) #endif /* MAX */
The intention is to ensure that _a and _b have compatible type by comparing their addresses; only if _a and _b have compatible types will pointers to them have compatible type. The result of the comparison is ignored, so the only effect is to provoke a diagnostic from the compiler if _a and _b have incompatible types. The goal is to avoid taking the minimum/maximum of values of different types, because the result can be surprising. Btw, I don't think we need to port the latest version of min/max from linux/include/linux/minmax.h because it has an additional constant expression checker. Actually, we (LTP) do have some tests pass variable to MIN/MAX and it should _not_ be working at that senario. Signed-off-by: Li Wang <liwang@redhat.com> Cc: Chunyu Hu <chuhu@redhat.com> --- include/tst_minmax.h | 2 ++ 1 file changed, 2 insertions(+)