]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
time-util: use usec_add() and usec_sub_unsigned()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 13 Feb 2023 19:27:27 +0000 (04:27 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 23 Feb 2023 23:55:27 +0000 (08:55 +0900)
And move the check with USEC_TIMESTAMP_FORMATTABLE_MAX at the end,
as usec_add() can handle overflow correctly.

src/basic/time-util.c

index 73def365a4881957918c2a9171bae11b5ef9871f..5ffd6bc57497ee624f7853ffda7aae1ff2ea7162 100644 (file)
@@ -882,20 +882,17 @@ from_tm:
         if (x < 0)
                 return -EINVAL;
 
-        usec = (usec_t) x * USEC_PER_SEC + x_usec;
-        if (usec > USEC_TIMESTAMP_FORMATTABLE_MAX)
-                return -EINVAL;
+        usec = usec_add(x * USEC_PER_SEC, x_usec);
 
 finish:
-        if (usec + plus < usec) /* overflow? */
-                return -EINVAL;
-        usec += plus;
-        if (usec > USEC_TIMESTAMP_FORMATTABLE_MAX)
+        usec = usec_add(usec, plus);
+
+        if (usec < minus)
                 return -EINVAL;
 
-        if (usec >= minus)
-                usec -= minus;
-        else
+        usec = usec_sub_unsigned(usec, minus);
+
+        if (usec > USEC_TIMESTAMP_FORMATTABLE_MAX)
                 return -EINVAL;
 
         if (ret)