Skip to content
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

[BUG] Missing tick overflow protection on u.xTCP.xLastActTime and u.xTCP.xLastAliveTime #1183

Closed
lzungri opened this issue Sep 7, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@lzungri
Copy link
Contributor

lzungri commented Sep 7, 2024

No tick overflow protection mechanism in place for u.xTCP.xLastActTime and u.xTCP.xLastAliveTime:

Consider replacing raw arithmetic on the tick count with the safer xTaskCheckForTimeOut.

@lzungri lzungri added the bug Something isn't working label Sep 7, 2024
@htibosch
Copy link
Contributor

htibosch commented Sep 7, 2024

Hello @lzungri , thank you for reporting this, but I don't think that there is a problem with mentioned code.

   TickType_t uxAge = xTaskGetTickCount() - pxSocket->u.xTCP.xLastActTime;

Both xTaskGetTickCount() and xLastActTime have the type TickType_t, which is an uint32_t or bigger.

Suppose that xLastActTime has the value 0xFFFFFFF0, and now the clocktick has overflown to 0x00000002. Is that the overflow you mean? uxAge will become 0x00000012, thanks to the magic of two's complement.

--- BEGIN EDIT ---

Using signed variables would give the same result:

int xLastActTime    = -16
xTaskGetTickCount() =   2
uxAge               =   2 - ( -16 ) = 18

--- END EDIT ---

If you feel that I miss the point, please give an example in which things might go wrong.

@lzungri
Copy link
Contributor Author

lzungri commented Sep 7, 2024

@htibosch Thanks for looking into that.

Suppose that xLastActTime has the value 0xFFFFFFF0, and now the clocktick has overflown to 0x00000002. Is that the overflow you mean? uxAge will become 0x00000012, thanks to the magic of two's complement.

When only one tick overflow has occurred, the result will be correct. I believe the problem will arise when more than 1 tick overflow occurred during that evaluation period. In that case, uxAge will not hold the right time delta as it doesn't account for the number of overflows.
This issue may not be that problematic on 32/64 bits CPUs where TickType_t is big enough to make sure an overflow only happens after a year or so. However, on ports where TickType_t is defined as uint16_t or uint8_t, overflows will happen much more frequently and could lead to timing/accuracy problems. Having a high configTICK_RATE_HZ will make things worse.

xTaskCheckForTimeOut/vTaskSetTimeOutState internally keep track of the number of overflows and thus will not have this problem.

@htibosch
Copy link
Contributor

htibosch commented Sep 8, 2024

I do not think that the +TCP is being used on a "16-bit" CPU. At least I don't know of any such project.

@lzungri
Copy link
Contributor Author

lzungri commented Sep 8, 2024

I do not think that the +TCP is being used on a "16-bit" CPU. At least I don't know of any such project.

Got it, please let me know if I should close this issue.

@htibosch
Copy link
Contributor

htibosch commented Sep 8, 2024

If nobody else has comments on this subject, you may close it.
Thanks.

@aggarg
Copy link
Member

aggarg commented Sep 9, 2024

I am closing this issue as per the above discussion. Feel free to reopen is you need anything else.

@aggarg aggarg closed this as completed Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants