[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH] Avoid double argument evaluation in MIN and MAX macros
From:       Bartosz Golaszewski <bartekgola () gmail ! com>
Date:       2014-03-30 20:08:33
Message-ID: 1396217292-1238-1-git-send-email-bartekgola () gmail ! com
[Download RAW message or body]

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@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; \
+	})
 
 /* buffer allocation schemes */
 #if ENABLE_FEATURE_BUFFERS_GO_ON_STACK
diff --git a/util-linux/swaponoff.c b/util-linux/swaponoff.c
index a7ad6db..b3d265f 100644
--- a/util-linux/swaponoff.c
+++ b/util-linux/swaponoff.c
@@ -137,11 +137,10 @@ static int do_em_all(void)
 				p = hasmntopt(m, "pri");
 				if (p) {
 					/* Max allowed 32767 (== SWAP_FLAG_PRIO_MASK) */
-					unsigned prio = bb_strtou(p + 4, NULL, 10);
+					unsigned prio = MIN(bb_strtou(p + 4, NULL, 10), SWAP_FLAG_PRIO_MASK);
 					/* We want to allow "NNNN,foo", thus errno == EINVAL is allowed too */
 					if (errno != ERANGE) {
-						g_flags = (g_flags & ~SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER |
-							MIN(prio, SWAP_FLAG_PRIO_MASK);
+						g_flags = (g_flags & ~SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER | prio;
 					}
 				}
 #endif
-- 
1.8.4.5

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic