Skip to content

Fix integer overflow in FreeRTOS_round_up() - #1378

Closed
pamirmundt wants to merge 1 commit into
FreeRTOS:mainfrom
pamirmundt:fix/round-up-overflow
Closed

pamirmundt wants to merge 1 commit into
FreeRTOS:mainfrom
pamirmundt:fix/round-up-overflow

Conversation

@pamirmundt

Copy link
Copy Markdown

Description

FreeRTOS_round_up() can return incorrect results for large values because the intermediate addition may overflow, even when the rounded result fits within uint32_t.

For example, FreeRTOS_round_up( UINT32_MAX - 1U, 3U ) previously returned 0 instead of UINT32_MAX.

This change uses remainder-based arithmetic to avoid intermediate overflow and correctly round values within the uint32_t range. If the rounded result cannot be represented, the function returns zero.

Testing

Added unit tests covering:

  • Large values where the rounded result is still representable
  • Values that are already multiples of the divisor
  • Unrepresentable results and the zero fallback
  • Boundary cases near UINT32_MAX

The 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.

Use remainder-based rounding to avoid intermediate unsigned overflow when the rounded result is representable.

Add regression tests covering boundary values and overflow.

@htibosch htibosch left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@pamirmundt pamirmundt closed this Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants