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

Fix printing of time_t values. #1161

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Fix printing of time_t values. #1161

wants to merge 1 commit into from

Conversation

he32
Copy link
Contributor

@he32 he32 commented May 24, 2023

There is no guarantee that time_t is a long int. On NetBSD, time_t is a long long int. And ... apparently there is no portable way to indicate a printf format for a time_t via sys/inttypes.h / int_fmtio.h.

So do the next best thing to improve portability to NetBSD and avoid the warning about mismatched format and argument type (-Wformat) by casting time_t to long long before printing, and use %lld as the format specifier.

In a similar vein, there's also no portable way to specify a suitable format for a size_t, so cast to long unsigned int and print with %lu.

There is no guarantee that `time_t` is a `long int`.  On
NetBSD, `time_t` is a `long long int`.  And ... apparently
there is no portable way to indicate a printf format for a
`time_t` via sys/inttypes.h / int_fmtio.h.

So do the next best thing to improve portability to NetBSD
and avoid the warning about mismatched format and argument type
(-Wformat) by casting `time_t` to `long long` before printing, and
use %lld as the format specifier.

In a similar vein, there's also no portable way to specify a
suitable format for a `size_t`, so cast to `long unsigned int`
and print with %lu.
@@ -624,7 +624,7 @@ execute_file(exec_context& ec, const std::string& path_and_args, bool multiline)

vars["#"] = env_arg_name;
for (size_t lpc = 0; lpc < split_args.size(); lpc++) {
snprintf(env_arg_name, sizeof(env_arg_name), "%lu", lpc);
snprintf(env_arg_name, sizeof(env_arg_name), "%lu", (long unsigned int)lpc);
Copy link
Owner

Choose a reason for hiding this comment

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

I believe there's a z length modifier, so this should be %zu and then you don't need to cast it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are correct -- removing the suggested cast and using "z" format works as well for this case.

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.

None yet

2 participants