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

Log format specifier for 64-bit unsigned integer uses %lu causing issues when long unsigned is not 64-bits #76

Closed
warasilapm opened this issue Jul 17, 2023 · 4 comments

Comments

@warasilapm
Copy link

warasilapm commented Jul 17, 2023

In the code below, the log format specifier explicitly expects a type of %lu when printing the seconds delta:

https://github.com/FreeRTOS/coreSNTP/blob/3cb8ad937f70516cb9f08a7d622cfa9195736d3b/source/core_sntp_client.c#L795C1-L798C63

This can cause unexpected formatting function behavior depending on the platform. In our case, it printed out wildly large numbers; this will be different for every implementation of the formatter.

One potential solution would be to use inttypes.h or simply to use %llu. However, for many embedded builds of the standard library, proper support of %llu is spotty, so this may simply be a lost cause. Perhaps a conversion to uint32_t would be more suitable instead?

@Skptak
Copy link
Member

Skptak commented Jul 20, 2023

Hey, thanks for bringing this issue up! The issue you're raising here is definitely a valid one. I'll reach back out when I've had a chance to try a few different options and can figure out which may be the most portable.

@aggarg
Copy link
Member

aggarg commented Jul 20, 2023

None of the solution is perfect but may be adding a cast to unsigned long is a good compromise.

@Skptak Skptak mentioned this issue Dec 14, 2023
2 tasks
@Skptak
Copy link
Member

Skptak commented Dec 15, 2023

Apologies for the long delay in "fixing" this issue in PR #81 .
While swapping to %llu would be ideal, it is not a C90 Compliant modifier. Due to this a decision was made to convert the 64bit millisecond value to seconds, and cast it as a (unsigned long) to reduce the change of this value being too large.

This was in favor of adding additional code to check if the value would overflow the %lu specifier, as it would require a server response delay of over 24, or 48, days depending on if a signed or unsigned value.

I'm a bit curious to know if maybe the issue you had seen was actually due to a print statement that was incorrectly casting a int64_t to an ( unsigned long ) in the log, as seen here

If you feel that the fix in #81 is inadequate please feel free to open another PR, thanks!

@Skptak Skptak closed this as completed Dec 15, 2023
@warasilapm
Copy link
Author

LGTM.

I'm pretty sure there was a disconnect between the size of fields in the va_args vs. the formatter reading the format specifier that caused our issue. This should be a plenty sufficient fix. Thanks!

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

No branches or pull requests

3 participants