Skip to content

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

2 participants