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

Format time_t values portably and fix other values too #1085

Merged
merged 1 commit into from
Mar 5, 2021

Conversation

koutcher
Copy link
Collaborator

Fixes #1084

src/util.c Outdated
Comment on lines 163 to 168
if (!string_nformat(buf, buflen, NULL, "%s%"PRIdMAX"%c",
now.tv_sec >= timestamp ? "" : "-",
seconds, reldate[i].compact_symbol))
return "";

} else if (!string_nformat(buf, buflen, NULL, "%ld %s%s %s",
} else if (!string_nformat(buf, buflen, NULL, "%"PRIdMAX" %s%s %s",
Copy link

Choose a reason for hiding this comment

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

If you use this format specifier, what you should do is cast the value as well (so (intmax_t)value iirc). Otherwise this will read 64 bits from a 32 bit field on 32bit glibc, for example.

Generally speaking, I'd recommend doing %lld for format and (long long)value instead.

This goes for all instances.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Agreed, let's go the BSD way.

I actually took example on Git but I overlooked that they define their own timestamp_t type (though there was a clue as they use the unsigned version PRIuMAX).

@@ -170,7 +170,7 @@ file_finder_draw(struct file_finder *finder)

wmove(finder->win, finder->height - 1, 0);
wbkgdset(finder->win, get_line_attr(NULL, LINE_TITLE_FOCUS));
wprintw(finder->win, "[finder] file %d of %d", pos->lineno + 1, finder->lines);
wprintw(finder->win, "[finder] file %lu of %zu", pos->lineno + 1, finder->lines);
Copy link

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

@koutcher koutcher Feb 20, 2021

Choose a reason for hiding this comment

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

Yes, I noticed that too, lineno is unsigned int in line struct while it is unsigned long in position struct. For now, I only wanted to restore the concordance between formats and types, but I agree it could deserve attention as well.

src/draw.c Outdated
@@ -322,7 +322,7 @@ draw_lineno_custom(struct view *view, struct view_column *column, unsigned int l
return false;

if (lineno == 1 || (lineno % interval) == 0) {
static char fmt[] = "%ld";
static char fmt[] = "%#u";
Copy link

Choose a reason for hiding this comment

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

I'm reasonably sure u can't use #. GCC even warns about it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually # is just a placeholder, it is replaced with a digit one line below. I'll use %3u for clarity.

@koutcher
Copy link
Collaborator Author

Thanks for the review @ericonr, I was sure we could use more than one pair of eyes on this kind of subject :-).

@koutcher koutcher force-pushed the gh-1084-format-time_t-portably branch from 28ba357 to 31f1d32 Compare February 26, 2021 18:22
@koutcher koutcher merged commit 71d8944 into jonas:master Mar 5, 2021
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.

Can't use %ld to portably print time_t values
2 participants