From: Li Wang <liwang@redhat.com>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH 2/3] lib: adding unnecessary pointer comparison
Date: Fri, 11 Mar 2022 13:46:02 +0800 [thread overview]
Message-ID: <20220311054603.57328-3-liwang@redhat.com> (raw)
In-Reply-To: <20220311054603.57328-1-liwang@redhat.com>
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
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next prev parent reply other threads:[~2022-03-11 5:46 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-11 5:46 [LTP] [PATCH 0/3] refine the MIN/MAX macros Li Wang
2022-03-11 5:46 ` [LTP] [PATCH 1/3] include: replace min/max macro by the new definition Li Wang
2022-03-23 10:19 ` Petr Vorel
2022-03-24 2:52 ` Li Wang
2022-03-11 5:46 ` Li Wang [this message]
2022-03-23 10:22 ` [LTP] [PATCH 2/3] lib: adding unnecessary pointer comparison Petr Vorel
2022-03-11 5:46 ` [LTP] [PATCH 3/3] minmax: ensure the comparison numbers have compatible type Li Wang
2022-03-11 8:52 ` Li Wang
2022-03-23 13:33 ` Petr Vorel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220311054603.57328-3-liwang@redhat.com \
--to=liwang@redhat.com \
--cc=ltp@lists.linux.it \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox