Skip to content

Commit e6d0738

Browse files
CopilotBernardXiong
andcommitted
Fix code review issues: overflow protection and parameter handling
Co-authored-by: BernardXiong <1241087+BernardXiong@users.noreply.github.com>
1 parent a73b4db commit e6d0738

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

components/drivers/clock_time/src/clock_time.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,20 @@ rt_err_t rt_clock_time_device_register(struct rt_clock_time_device *dev,
5252
rt_uint64_t freq = dev->ops->get_freq();
5353
if (freq > 0)
5454
{
55-
/* res_scale = (1e9 * RT_CLOCK_TIME_RESMUL) / freq */
56-
dev->res_scale = ((1000000000ULL * RT_CLOCK_TIME_RESMUL) / freq);
55+
/* res_scale = (1e9 * RT_CLOCK_TIME_RESMUL) / freq
56+
* To avoid overflow, we check if freq is very small.
57+
* For freq >= 1000, this calculation is safe on 64-bit.
58+
* For very small frequencies, limit the scale factor.
59+
*/
60+
if (freq >= 1000)
61+
{
62+
dev->res_scale = ((1000000000ULL * RT_CLOCK_TIME_RESMUL) / freq);
63+
}
64+
else
65+
{
66+
/* For very low frequencies, calculate more carefully */
67+
dev->res_scale = (1000000ULL * RT_CLOCK_TIME_RESMUL) / freq * 1000;
68+
}
5769
}
5870
else
5971
{

components/drivers/clock_time/src/hrtimer.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,17 @@ rt_weak rt_err_t rt_clock_hrtimer_settimeout(unsigned long cnt)
8080
static unsigned long _cnt_convert(unsigned long cnt)
8181
{
8282
unsigned long rtn = 0;
83-
unsigned long count = cnt - rt_clock_cputimer_getcnt();
83+
unsigned long current_cnt = rt_clock_cputimer_getcnt();
84+
85+
/* Check for overflow/underflow - if cnt is in the past or wrapped around */
86+
if (cnt <= current_cnt)
87+
{
88+
return 0;
89+
}
90+
91+
unsigned long count = cnt - current_cnt;
92+
93+
/* Sanity check: if count is too large, it might be a wrap-around */
8494
if (count > (_HRTIMER_MAX_CNT / 2))
8595
return 0;
8696

@@ -289,7 +299,7 @@ rt_err_t rt_clock_hrtimer_control(rt_clock_hrtimer_t timer, int cmd, void *arg)
289299
*(unsigned long *)arg = timer->timeout_cnt;
290300
break;
291301
case RT_TIMER_CTRL_GET_FUNC:
292-
arg = (void *)timer->timeout_func;
302+
*(void **)arg = (void *)timer->timeout_func;
293303
break;
294304

295305
case RT_TIMER_CTRL_SET_FUNC:

components/drivers/include/drivers/clock_time.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ rt_err_t rt_clock_hrtimer_detach(rt_clock_hrtimer_t timer);
256256
* @brief Keep errno in timer structure
257257
* @param timer Timer structure
258258
* @param err Error code to keep
259+
*
260+
* Note: This function negates err when setting errno to convert RT-Thread
261+
* error codes to POSIX-style errno values. This maintains compatibility
262+
* with the original ktime implementation.
259263
*/
260264
rt_inline void rt_clock_hrtimer_keep_errno(rt_clock_hrtimer_t timer, rt_err_t err)
261265
{

0 commit comments

Comments
 (0)