]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
macro: Use more correct type in IN_SET
authorJan Janssen <medhefgo@web.de>
Tue, 25 Oct 2022 17:33:40 +0000 (19:33 +0200)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 25 Oct 2022 18:27:09 +0000 (03:27 +0900)
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]

src/fundamental/macro-fundamental.h

index 2536c741c62f3d38cf6f1d487a9d1d580c93ea00..58ffaac33cbc9d0d97be0ba9a195eeff112f3ca4 100644 (file)
 #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__)                         \