Repository navigation
Fix integer overflow in FreeRTOS_round_up() - #1378
pamirmundt wants to merge 1 commit into
Conversation
Use remainder-based rounding to avoid intermediate unsigned overflow when the rounded result is representable. Add regression tests covering boundary values and overflow.
htibosch
left a comment
There was a problem hiding this comment.
Thank you @pamirmundt , but your proposal doesn't get my approval.
At most we could update the parameter naming and the comments.
|
|
||
| if( d != 0U ) | ||
| { | ||
| ulResult = d * ( ( a + d - 1U ) / d ); |
There was a problem hiding this comment.
@pamirmundt , I think that the current implementation is favorable:
d * ( ( a + d - 1U ) / d )It is not a generic roundup function, it is made to calculate a number of blocks needed:
uint32_t FreeRTOS_round_up( uint32_t ulByteCount, uint32_t ulBlockSize )
ulBlockSize * ( ( ulByteCount + ulBlockSize - 1U ) / ulBlockSize )Suppose I have 3000 bytes of audio data, how many sectors of 1024 do I need:
FreeRTOS_round_up( 3000, 1024 ) = 3 sectors ( 3072 bytes )
I am sending TCP data, with en MSS of 1460:
FreeRTOS_round_up( 5289, 1460 ) = 4 segments ( 5840 bytes )
So if you don't mind, I don't approve this PR.
There was a problem hiding this comment.
Thanks for taking the time to review and explain. I understand the intended use of the function.
Just to clarify, my concern was the intermediate unsigned overflow in a + d - 1U. For example, FreeRTOS_round_up( UINT32_MAX - 1U, 3U ) currently returns 0, although the expected result, UINT32_MAX, is representable.
I understand if this is outside the expected input range or not something you want to address. Thanks again for the review!
There was a problem hiding this comment.
But never mind, the other PRs (#1377 and #1379) will get my approval.
As for FreeRTOS_round_up(), it would hurt to change the parameter names:
- uint32_t FreeRTOS_round_up( uint32_t a,
- uint32_t d )
+ uint32_t FreeRTOS_round_up( uint32_t ulByteCount,
+ uint32_t ulBlockSize )and explain what the function does.
Description
FreeRTOS_round_up()can return incorrect results for large values because the intermediate addition may overflow, even when the rounded result fits withinuint32_t.For example,
FreeRTOS_round_up( UINT32_MAX - 1U, 3U )previously returned0instead ofUINT32_MAX.This change uses remainder-based arithmetic to avoid intermediate overflow and correctly round values within the
uint32_trange. If the rounded result cannot be represented, the function returns zero.Testing
Added unit tests covering:
UINT32_MAXThe new regression test fails on the original implementation and passes with the fix.
Follow-up
I also found related overflow-handling issues in the TCP buffer-size and allocation paths in
FreeRTOS_Sockets.c.For example, even when the rounded buffer size is valid, adding allocation overhead can still overflow. Some callers also don't check whether rounding a nonzero request returns zero.
These issues are outside the scope of this PR and will be reported separately to the maintainers.
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.