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

time-util: fix buffer-over-run #23933

Merged
merged 1 commit into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/basic/time-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
t = b;
}

n = MIN((size_t) k, l);
n = MIN((size_t) k, l-1);

l -= n;
p += n;
Expand Down
5 changes: 5 additions & 0 deletions src/test/test-time-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ TEST(format_timespan) {
test_format_timespan_accuracy(1);
test_format_timespan_accuracy(USEC_PER_MSEC);
test_format_timespan_accuracy(USEC_PER_SEC);

/* See issue #23928. */
_cleanup_free_ char *buf;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just char buf[5]?

Copy link
Member Author

Choose a reason for hiding this comment

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

I do not know why, but valgrind does not detect the buffer-over-run if buf[5] is used.
ASAN may detect the issue even with buf[5]. But this is just a test code, hence I hope that's no problem.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I do not know why, but valgrind does not detect the buffer-over-run if buf[5] is used.

Because valgrind's memcheck doesn't check stack allocations.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, thanks.

assert_se(buf = new(char, 5));
assert_se(buf == format_timespan(buf, 5, 100005, 1000));
}

TEST(verify_timezone) {
Expand Down