]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
hostnamed: when parsing day/month of firmware date, force decimal parsing 28080/head
authorLennart Poettering <lennart@poettering.net>
Tue, 20 Jun 2023 13:00:07 +0000 (15:00 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 20 Jun 2023 13:18:58 +0000 (15:18 +0200)
safe_atou() by default determines the base from the prefix 0x, 0b, 0o
and for compat with just 0 for octal. This is not what we want here,
since the date components are padded with zeroes yet still decimal.
Hence force decimal parsing (and while we are at it, prohibit a couple
of unexpected decorations).

WIthout this we'd fail to parse any the 8th and 9th day of each months, as
well aus aug and september of every year, because these look like octal
numbers but cannot actually parsed as such.

Let's change the testcase to check for a date that exposes this
bheaviour.

src/hostname/hostnamed.c
test/units/testsuite-71.sh

index 2f63ac4dcf987aed403297cce75528f4cf598c7d..9ef45f8e75a6c3cf1fc884312df421df59a661bb 100644 (file)
@@ -307,20 +307,20 @@ static int get_firmware_date(usec_t *ret) {
                 return -EINVAL;
 
          unsigned m, d, y;
-         r = safe_atou(month, &m);
+         r = safe_atou_full(month, 10 | SAFE_ATO_REFUSE_PLUS_MINUS | SAFE_ATO_REFUSE_LEADING_WHITESPACE, &m);
          if (r < 0)
                 return r;
          if (m < 1 || m > 12)
                 return -EINVAL;
          m -= 1;
 
-         r = safe_atou(day, &d);
+         r = safe_atou_full(day, 10 | SAFE_ATO_REFUSE_PLUS_MINUS | SAFE_ATO_REFUSE_LEADING_WHITESPACE, &d);
          if (r < 0)
                 return r;
          if (d < 1 || d > 31)
                 return -EINVAL;
 
-         r = safe_atou(year, &y);
+         r = safe_atou_full(year, 10 | SAFE_ATO_REFUSE_PLUS_MINUS | SAFE_ATO_REFUSE_LEADING_WHITESPACE, &y);
          if (r < 0)
                 return r;
          if (y < 1970 || y > (unsigned) INT_MAX)
index 6f99ecadd81424b51bd062ca19346d6dd3a350f5..9d84dd89ed3190941ce7a9c4af6cda693f3c2571 100755 (executable)
@@ -119,9 +119,9 @@ EOF
     mount -t tmpfs none /sys/class/dmi/id
     echo '1' >/sys/class/dmi/id/uevent
 
-    echo '01/01/2000' >/sys/class/dmi/id/bios_date
+    echo '09/08/2000' >/sys/class/dmi/id/bios_date
     systemctl stop systemd-hostnamed
-    assert_in '2000-01-01' "$(hostnamectl)"
+    assert_in '2000-09-08' "$(hostnamectl)"
 
     echo '2022' >/sys/class/dmi/id/bios_date
     systemctl stop systemd-hostnamed