[En-Nut-Discussion] NutSleep Overflow Fix

Bob Wirka bobwirka at yahoo.com
Fri Aug 16 17:25:36 CEST 2013


Hello,

NutSleep(uint32_t ms) will not work properly if  'ms' is greater than 4,294,967 (0xffffffff / 10000).

This is because the calculation in NutTimerMillisToTicks() will overflow.

Below is my fix:

/*!
 * \brief Calculate system ticks for a given number of milliseconds.
 */
uint32_t NutTimerMillisToTicks(uint32_t ms)
{
    unsigned long long        product;

    product = (unsigned long long)ms * (unsigned long long)NutGetTickClock();
    return product / 1000;

    /*
     * _RCW_: 16-Aug-2013 : This would overflow for ms values > 4,294,967, as
     * the product of ms * NutGetTickClock() had to be <= 0xffff_ffff.
     */
//    return (ms * NutGetTickClock()) / 1000;
}

Best regards,

Bob Wirka
Realtime Control Works
Janesville, WI


More information about the En-Nut-Discussion mailing list