Skip to content

Commit b49c6ca

Browse files
committed
systemd-nspawn: make SettingsMask 64 bit wide
The use of UINT64_C() in the SettingsMask enum definition is misleading: it does not mean that individual fields have this width. E.g., with enum { FOO = UINT64_C(1) } sizeof(FOO) gives 4. It only means that the shift is done properly. So 1 << 35 is undefined, but UINT64_C(1) << 35 is the expected 64 bit constant. Thus, the use UINT64_C() is useful, because we know that the shifts are done properly, no matter what the value of _RLIMIT_MAX is, but when those fields are used in expressions, we don't know what size they will be (probably 4). Let's add a define which "hides" the enum definition behind a define which gives the same value but is actually 64 bit. I think this is a nicer solution than requiring all users to cast SETTING_RLIMIT_FIRST before use. Fixes systemd#9035.
1 parent 9b505bc commit b49c6ca

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/nspawn/nspawn-settings.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,19 @@ typedef enum SettingsMask {
5656
SETTING_CPU_AFFINITY = UINT64_C(1) << 20,
5757
SETTING_RLIMIT_FIRST = UINT64_C(1) << 21, /* we define one bit per resource limit here */
5858
SETTING_RLIMIT_LAST = UINT64_C(1) << (21 + _RLIMIT_MAX - 1),
59-
_SETTINGS_MASK_ALL = (UINT64_C(1) << (21 + _RLIMIT_MAX)) - 1
59+
_SETTINGS_MASK_ALL = (UINT64_C(1) << (21 + _RLIMIT_MAX)) - 1,
60+
_FORCE_ENUM_WIDTH = UINT64_MAX
6061
} SettingsMask;
6162

63+
/* We want to use SETTING_RLIMIT_FIRST in shifts, so make sure it is really 64 bits
64+
* when used in expressions. */
65+
#define SETTING_RLIMIT_FIRST ((uint64_t) SETTING_RLIMIT_FIRST)
66+
#define SETTING_RLIMIT_LAST ((uint64_t) SETTING_RLIMIT_LAST)
67+
68+
assert_cc(sizeof(SettingsMask) == 8);
69+
assert_cc(sizeof(SETTING_RLIMIT_FIRST) == 8);
70+
assert_cc(sizeof(SETTING_RLIMIT_LAST) == 8);
71+
6272
typedef struct Settings {
6373
/* [Run] */
6474
StartMode start_mode;

0 commit comments

Comments
 (0)