Skip to content

Fix Year 2038 Problem in Sntp_ConvertToUnixTime #104

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

Merged
merged 20 commits into from
Apr 15, 2025

Conversation

kar-rahul-aws
Copy link
Member

@kar-rahul-aws kar-rahul-aws commented Apr 10, 2025

Description

This SNTP function assumes 32-bit signed UNIX timestamps, but devices going into production now should probably be using unsigned 32-bit UNIX timestamps or wider signed 64-bit timestamps.

This PR updates the SNTP to Unix time conversion logic to support 64-bit Unix time (Y2038 compliance) while maintaining backward compatibility with legacy 32-bit systems.

Test Steps

Checklist:

  • I have tested my changes. No regression in existing tests.
  • I have modified and/or added unit-tests to cover the code changes in this Pull Request.

Related Issue

https://t.corp.amazon.com/P204919465

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@tony-josi-aws
Copy link
Member

Shouldn't we update the sntpClient_SetTime function in the example_sntp_client_posix.c to use the updated version of Sntp_ConvertToUnixTime?

@kar-rahul-aws
Copy link
Member Author

The coreSNTP Windows Simulator demo will need to be changed here , will do that once these changes get approved.

@kar-rahul-aws kar-rahul-aws requested a review from cookpate April 11, 2025 17:16
cookpate
cookpate previously approved these changes Apr 11, 2025
@@ -839,12 +832,13 @@ SntpStatus_t Sntp_ConvertToUnixTime( const SntpTimestamp_t * pSntpTime,
* +
* Sntp Time since Era 1 Epoch
*/
*pUnixTimeSecs = UNIX_TIME_SECS_AT_SNTP_ERA_1_SMALLEST_TIME + pSntpTime->seconds;
*pUnixTimeSecs = ( UnixTime_t ) ( UNIX_TIME_SECS_AT_SNTP_ERA_1_SMALLEST_TIME + pSntpTime->seconds );
Copy link
Member

@tony-josi-aws tony-josi-aws Apr 14, 2025

Choose a reason for hiding this comment

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

Shouldn't we also update the seconds field of SntpTimestamp_t or cast it to UnixTime_t (if seconds with uint32_t itself doesn't overflow) before the addition takes place in order to make this change effective? Otherwise, I believe the addition should be happening as uint32_t and the overflow must have happened before the UnixTime_t cast of the result.

Copy link
Member Author

Choose a reason for hiding this comment

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

updated.

{
*pUnixTimeSecs = pSntpTime->seconds - SNTP_TIME_AT_UNIX_EPOCH_SECS;
*pUnixTimeSecs = ( UnixTime_t ) ( pSntpTime->seconds - SNTP_TIME_AT_UNIX_EPOCH_SECS );
Copy link
Member

Choose a reason for hiding this comment

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

Similar to above comment.

Copy link
Member Author

Choose a reason for hiding this comment

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

updated.

@kar-rahul-aws kar-rahul-aws merged commit 941ad2f into FreeRTOS:main Apr 15, 2025
13 checks passed
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.

3 participants