From 48936c02bb533f5f615425ba3e9b4b59235de928 Mon Sep 17 00:00:00 2001 From: Jakub Kicinski Date: Thu, 9 Dec 2021 10:57:19 -0800 Subject: [PATCH] minmax: don't nest min/max in clamp to avoid -Wshadow warnings Using clamp from minmax.h with -Wshadow (GCC 11 and 8, haven't tested others) generates a bunch of warnings because min and max both declare _a, _b vars. Store the result of min to a temporary variable instead of nesting the calls this way min's variables are not in scope of max. Signed-off-by: Jakub Kicinski --- ccan/minmax/minmax.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ccan/minmax/minmax.h b/ccan/minmax/minmax.h index d111d1bc3..55b44307e 100644 --- a/ccan/minmax/minmax.h +++ b/ccan/minmax/minmax.h @@ -38,7 +38,11 @@ _a > _b ? _a : _b; \ }) -#define clamp(v, f, c) (max(min((v), (c)), (f))) +#define clamp(v, f, c) \ + ({ \ + typeof(v) _ceiling_clamped = min((v), (c)); \ + (max(_ceiling_clamped, (f))); \ + }) #define min_t(t, a, b) \