Skip to content

Commit

Permalink
time-util: fix buffer-over-run
Browse files Browse the repository at this point in the history
Fixes #23928.

(cherry picked from commit 9102c625a673a3246d7e73d8737f3494446bad4e)

Resolves: #2139391
  • Loading branch information
yuwata authored and systemd-rhel-bot committed Nov 7, 2022
1 parent 4c241b8 commit a521f94
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/basic/time-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,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 @@ -187,6 +187,11 @@ static void test_format_timespan(usec_t accuracy) {
test_format_timespan_one(500 * USEC_PER_MSEC, accuracy);
test_format_timespan_one(9*USEC_PER_YEAR/5 - 23, accuracy);
test_format_timespan_one(USEC_INFINITY, accuracy);

/* See issue #23928. */
_cleanup_free_ char *buf;
assert_se(buf = new(char, 5));
assert_se(buf == format_timespan(buf, 5, 100005, 1000));
}

static void test_timezone_is_valid(void) {
Expand Down

0 comments on commit a521f94

Please sign in to comment.