Menu

#135 pdMS_TO_TICKS macro may overflow

v1.0 (example)
closed-wont-fix
nobody
None
5
2016-09-25
2016-09-24
No

When configUSE_16_BIT_TICKS is set the pdMS_TO_TICKS macro may overflow. For example, on a system with a tick frequency of 1000 Hz a millisecond value greater than 65 will overflow the result of the first multiplication because it's only 16 bits wide.

Suggest type-promoting the multiplication of TICKS * MS to a 32-bit or greater result, then back to the width of TickType_t after division.

Example:
#define pdMS_TO_TICKS( xTimeInMs ) ( ( TickType_t ) ( ( ( unsigned long ) ( xTimeInMs ) * ( TickType_t ) configTICK_RATE_HZ ) / ( TickType_t ) 1000 ) )

Discussion

  • Richard Barry

    Richard Barry - 2016-09-25

    Hi Billiam - thank you for your suggestion. I am going to close this ticket, not because you are not making a valid point, but because there is no really portable solution to this macro that suites everybody, and therefore a mechanism is already in place that allows you to override the macro definition by having your own implementation in FreeRTOSConfig.h. To give some examples, your solution will cause an issue on 64-bit architectures (yes FreeRTOS runs in native 64-bit mode on some architectures), and people choosing a 16-bit tick may be using an 8-bit device where they 32-bit maths is too expensive.

    pdMS_TO_TICKS() will only get defined in projdefs.h if it is not already defined by the user. Now, admittedly, this is probably not documented anywhere (!)(?), but the definition appears as follows:

    #ifndef pdMS_TO_TICKS
        #define pdMS_TO_TICKS( xTimeInMs ) ( ( TickType_t ) ( ( ( TickType_t ) ( xTimeInMs ) * ( TickType_t ) configTICK_RATE_HZ ) / ( TickType_t ) 1000 ) )
    #endif
    
     
  • Richard Barry

    Richard Barry - 2016-09-25
    • status: open --> closed-wont-fix
     

Log in to post a comment.

MongoDB Logo MongoDB