ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
6f381c
From a521f942d5c304bca7c61bacb3c79e565853718e Mon Sep 17 00:00:00 2001
dcda24
From: Yu Watanabe <watanabe.yu+github@gmail.com>
dcda24
Date: Thu, 7 Jul 2022 18:27:02 +0900
dcda24
Subject: [PATCH] time-util: fix buffer-over-run
dcda24
dcda24
Fixes #23928.
dcda24
dcda24
(cherry picked from commit 9102c625a673a3246d7e73d8737f3494446bad4e)
dcda24
6f381c
Resolves: #2139391
dcda24
---
dcda24
 src/basic/time-util.c     | 2 +-
dcda24
 src/test/test-time-util.c | 5 +++++
dcda24
 2 files changed, 6 insertions(+), 1 deletion(-)
dcda24
dcda24
diff --git a/src/basic/time-util.c b/src/basic/time-util.c
dcda24
index c36e462193..d46d884be5 100644
dcda24
--- a/src/basic/time-util.c
dcda24
+++ b/src/basic/time-util.c
dcda24
@@ -515,7 +515,7 @@ char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
dcda24
                         t = b;
dcda24
                 }
dcda24
 
dcda24
-                n = MIN((size_t) k, l);
dcda24
+                n = MIN((size_t) k, l-1);
dcda24
 
dcda24
                 l -= n;
dcda24
                 p += n;
dcda24
diff --git a/src/test/test-time-util.c b/src/test/test-time-util.c
dcda24
index 354a01dd1a..6ebde4153c 100644
dcda24
--- a/src/test/test-time-util.c
dcda24
+++ b/src/test/test-time-util.c
dcda24
@@ -187,6 +187,11 @@ static void test_format_timespan(usec_t accuracy) {
dcda24
         test_format_timespan_one(500 * USEC_PER_MSEC, accuracy);
dcda24
         test_format_timespan_one(9*USEC_PER_YEAR/5 - 23, accuracy);
dcda24
         test_format_timespan_one(USEC_INFINITY, accuracy);
dcda24
+
dcda24
+        /* See issue #23928. */
dcda24
+        _cleanup_free_ char *buf;
dcda24
+        assert_se(buf = new(char, 5));
dcda24
+        assert_se(buf == format_timespan(buf, 5, 100005, 1000));
dcda24
 }
dcda24
 
dcda24
 static void test_timezone_is_valid(void) {