[LTP] [PATCH 2/3] lib: adding unnecessary pointer comparison

Li Wang liwang@redhat.com
Fri Mar 11 06:46:02 CET 2022


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(+)

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 */
-- 
2.31.1



More information about the ltp mailing list