profile/systemd-osc-context: do not zero-pad pid value

The %.20d specifier was introduced in 2d738a0aee ("profile/systemd-osc-context:
Enforce length limits"). But, for numeric conversions, the precision is not an
upper bound, but rather a lower bound on the output width, padding as necessary
with 0 on the left. So as-is, this does not in fact limit the output to at most
20 characters.

Of course, in practice, pids on linux are never greater than 2^22, and
certainly never larger than what would fit in a 20-digit decimal. On the other
hand, that more or less guarantees that the pid= field is always emitted with
12+ leadings zeroes, which is a bit silly. Moreover, a leading 0 can cause a
parser to treat it as octal.

Since $$ does expand to the PID in decimal, just print that as a string, with the
enforced 20 character limit.
This commit is contained in:
Rasmus Villemoes
2026-07-21 11:06:30 +02:00
parent 9ca5488046
commit 10a356629a
+1 -1
View File
@@ -52,7 +52,7 @@ __systemd_osc_context_common() {
if [ -f /etc/machine-id ]; then
printf ";machineid=%.36s" "$(</etc/machine-id)"
fi
printf ";user=%.255s;hostname=%.255s;bootid=%.36s;pid=%.20d" "$USER" "$HOSTNAME" "$(</proc/sys/kernel/random/boot_id)" "$$"
printf ";user=%.255s;hostname=%.255s;bootid=%.36s;pid=%.20s" "$USER" "$HOSTNAME" "$(</proc/sys/kernel/random/boot_id)" "$$"
}
__systemd_osc_context_precmdline() {