|
|
@@ -17,10 +17,13 @@ rt_weak rt_err_t rt_clock_boottime_get_us(struct timeval *tv)
|
|
|
{
|
|
|
RT_ASSERT(tv != RT_NULL);
|
|
|
|
|
|
- rt_uint64_t ns = (rt_clock_cputimer_getcnt() * rt_clock_cputimer_getres()) / RT_CLOCK_TIME_RESMUL;
|
|
|
+ /* Use 64-bit intermediate values to prevent overflow */
|
|
|
+ rt_uint64_t cnt = rt_clock_cputimer_getcnt();
|
|
|
+ rt_uint64_t res = rt_clock_cputimer_getres();
|
|
|
+ rt_uint64_t ns = (cnt * res) / RT_CLOCK_TIME_RESMUL;
|
|
|
|
|
|
- tv->tv_sec = ns / (1000ULL * 1000 * 1000);
|
|
|
- tv->tv_usec = (ns % (1000ULL * 1000 * 1000)) / 1000;
|
|
|
+ tv->tv_sec = (long)(ns / (1000ULL * 1000 * 1000));
|
|
|
+ tv->tv_usec = (long)((ns % (1000ULL * 1000 * 1000)) / 1000);
|
|
|
|
|
|
return RT_EOK;
|
|
|
}
|
|
|
@@ -29,9 +32,12 @@ rt_weak rt_err_t rt_clock_boottime_get_s(time_t *t)
|
|
|
{
|
|
|
RT_ASSERT(t != RT_NULL);
|
|
|
|
|
|
- rt_uint64_t ns = (rt_clock_cputimer_getcnt() * rt_clock_cputimer_getres()) / RT_CLOCK_TIME_RESMUL;
|
|
|
+ /* Use 64-bit intermediate values to prevent overflow */
|
|
|
+ rt_uint64_t cnt = rt_clock_cputimer_getcnt();
|
|
|
+ rt_uint64_t res = rt_clock_cputimer_getres();
|
|
|
+ rt_uint64_t ns = (cnt * res) / RT_CLOCK_TIME_RESMUL;
|
|
|
|
|
|
- *t = ns / (1000ULL * 1000 * 1000);
|
|
|
+ *t = (time_t)(ns / (1000ULL * 1000 * 1000));
|
|
|
|
|
|
return RT_EOK;
|
|
|
}
|
|
|
@@ -40,10 +46,14 @@ rt_weak rt_err_t rt_clock_boottime_get_ns(struct timespec *ts)
|
|
|
{
|
|
|
RT_ASSERT(ts != RT_NULL);
|
|
|
|
|
|
- rt_uint64_t ns = (rt_clock_cputimer_getcnt() * rt_clock_cputimer_getres()) / RT_CLOCK_TIME_RESMUL;
|
|
|
+ /* Use 64-bit intermediate values to prevent overflow */
|
|
|
+ rt_uint64_t cnt = rt_clock_cputimer_getcnt();
|
|
|
+ rt_uint64_t res = rt_clock_cputimer_getres();
|
|
|
+ rt_uint64_t ns = (cnt * res) / RT_CLOCK_TIME_RESMUL;
|
|
|
|
|
|
- ts->tv_sec = ns / (1000ULL * 1000 * 1000);
|
|
|
- ts->tv_nsec = ns % (1000ULL * 1000 * 1000);
|
|
|
+ ts->tv_sec = (time_t)(ns / (1000ULL * 1000 * 1000));
|
|
|
+ ts->tv_nsec = (long)(ns % (1000ULL * 1000 * 1000));
|
|
|
|
|
|
return RT_EOK;
|
|
|
}
|
|
|
+
|