Watch
0
0
Fork
You've already forked hyperhive
0

swarm-controller: refuse a percentage with a leading zero in its whole part

systemd ignores `08%` and `085480%` for CPUQuota= and `08%` for MemoryMax=
("Invalid ... ignoring"), which leaves the agent uncapped. `hundredths` now
refuses a whole part that starts with `0` unless it is exactly `0`, for both
fields. `0.5%` and `0.01%` stay accepted.
This commit is contained in:
atlas 2026-10-12 15:25:37 +02:00 • committed by mara
commit 05c9075825
2 changed files with 15 additions and 4 deletions

View file

@ -493,7 +493,8 @@ caps. swarm-controller alone writes it:
`PUT` refuses an agent no hive's wanted state declares, unknown fields, and
any value systemd would ignore, which would leave the agent uncapped. A
percentage has at most two decimals and is above zero, up to `100%` for
percentage has at most two decimals, no leading zero in its whole part (it
refuses `08%`), and is above zero, up to `100%` for
`memory_max` and `21474836.47%` for `cpu_quota`. A size is digits with an
optional fraction and an optional `B`, `K`, `M`, `G` or `T` (upper case), at
least one byte and below 2^64 - 1 bytes. The controller stores each value as

View file

@ -105,7 +105,9 @@ fn memory_max(value: &str) -> Result<String, String> {
}
/// `value` as systemd reads a percentage, in hundredths of a percent: digits,
/// optionally `.` and one or two digits, then `%`. `None` for anything else.
/// optionally `.` and one or two digits, then `%`. `None` for anything else,
/// and for a whole part with a leading zero other than `0` itself: systemd
/// ignores `08%` and may read `010%` as octal.
fn hundredths(value: &str) -> Option<u64> {
let number = value.strip_suffix('%')?;
let (int, frac) = match number.split_once('.') {
@ -113,7 +115,7 @@ fn hundredths(value: &str) -> Option<u64> {
Some(_) => return None,
None => (number, ""),
};
if !digits(int) {
if !digits(int) || (int.len() > 1 && int.starts_with('0')) {
return None;
}
let frac = match frac.len() {
@ -324,7 +326,6 @@ mod tests {
"0.01%",
"1500%",
"400.12%",
"05%",
"21474836.47%",
] {
assert_eq!(cpu_quota(ok).as_deref(), Ok(ok));
@ -345,6 +346,11 @@ mod tests {
"0.001%",
"99999999999999999999%",
"21474836.48%",
"05%",
"08%",
"085480%",
"010%",
"00.5%",
"1.%",
".5%",
"400%\nExecStartPre=/bin/sh -c id #%",
@ -391,6 +397,10 @@ mod tests {
"150%",
"100.01%",
"12.345%",
"08%",
"085480%",
"010%",
"00.5%",
"8X",
"G",
"8 G",