ryantimwilson / rpms / systemd

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