time-util: use usec_add() and usec_sub_unsigned()

And move the check with USEC_TIMESTAMP_FORMATTABLE_MAX at the end,
as usec_add() can handle overflow correctly.
This commit is contained in:
Yu Watanabe
2023-02-14 04:27:27 +09:00
parent f2ecfd8bc1
commit db43717e98

View 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)