- 
                Notifications
    You must be signed in to change notification settings 
- Fork 8.2k
drivers: timer: stm32 lptim fix Potentially overflow #31142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| Is there a reason why these calculations are done in two different ways (multiplication then division and division then multiplication)? | 
61d0c7d    to
    95fc7d5      
    Compare
  
    | with this calculation on 64bit of returned time value, the timer tests can pass on nucleo_wb55rg | 
        
          
                drivers/timer/stm32_lptim_timer.c
              
                Outdated
          
        
      There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't look correct to me, but it was already the case before and not due to to you change. If the computation has to be done with precision as in the comment, it should be:
uint64_t ret = ((uint64_t)lp_time * sys_clock_hw_cycles_per_sec()) / LPTIM_CLOCK;
Basically just like the other change. Also note that sys_clock_hw_cycles_per_sec is equivalent to CONFIG_SYS_CLOCK_TICKS_PER_SEC when CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME is not defined, which is the case for this driver.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok,
I did not find any error when testing (10x) tests/kernel/timer/timer_api and tests/kernel/timer/timer_monotonic on nucleo stm32wb55 (when lptim enabled)
95fc7d5    to
    022b73c      
    Compare
  
    | As me and @aurel32 already wrote, dividing before multiplying causes lost of precision. uint64_t ret = ((uint64_t)lp_time * sys_clock_hw_cycles_per_sec()) / LPTIM_CLOCK; | 
Fix Unintentional integer overflow in the calculation Integer handling issues (OVERFLOW_BEFORE_WIDEN) Potentially overflowing expression Signed-off-by: Francois Ramu <[email protected]>
022b73c    to
    b80fc4c      
    Compare
  
    | Thanks @FRASTM | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fix, it looks good to me now.
Fix Unintentional integer overflow in the calculation
Integer handling issues (OVERFLOW_BEFORE_WIDEN)
Potentially overflowing expression with a cast
Fixes #29700
Fixes #29697
Signed-off-by: Francois Ramu [email protected]