From: Jan Janssen Date: Tue, 25 Oct 2022 17:33:40 +0000 (+0200) Subject: macro: Use more correct type in IN_SET X-Git-Tag: v253-rc1~637^2~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0bc4ac526d41483e6e9625d156d753bb3e287a19;p=thirdparty%2Fsystemd.git macro: Use more correct type in IN_SET This will now catch mistakes like this: struct s { int i:2; } s = { 1 }; assert_se(IN_SET(s.i, ULLONG_MAX)); > warning: implicit conversion from 'unsigned long long' to > 'typeof (+s.i)' (aka 'int') changes value from 18446744073709551615 > to -1 [-Wconstant-conversion] --- diff --git a/src/fundamental/macro-fundamental.h b/src/fundamental/macro-fundamental.h index 2536c741c62..58ffaac33cb 100644 --- a/src/fundamental/macro-fundamental.h +++ b/src/fundamental/macro-fundamental.h @@ -282,11 +282,9 @@ #define IN_SET(x, ...) \ ({ \ bool _found = false; \ - /* If the build breaks in the line below, you need to extend the case macros. (We use "long double" as \ - * type for the array, in the hope that checkers such as ubsan don't complain that the initializers for \ - * the array are not representable by the base type. Ideally we'd use typeof(x) as base type, but that \ - * doesn't work, as we want to use this on bitfields and gcc refuses typeof() on bitfields.) */ \ - static const long double __assert_in_set[] _unused_ = { __VA_ARGS__ }; \ + /* If the build breaks in the line below, you need to extend the case macros. We use typeof(+x) \ + * here to widen the type of x if it is a bit-field as this would otherwise be illegal. */ \ + static const typeof(+x) __assert_in_set[] _unused_ = { __VA_ARGS__ }; \ assert_cc(ELEMENTSOF(__assert_in_set) <= 20); \ switch (x) { \ FOR_EACH_MAKE_CASE(__VA_ARGS__) \