From: Hungyu Lin <dennylin0707@gmail.com>
To: pmladek@suse.com, rostedt@goodmis.org
Cc: andriy.shevchenko@linux.intel.com, linux@rasmusvillemoes.dk,
senozhatsky@chromium.org, akpm@linux-foundation.org,
linux-kernel@vger.kernel.org, Hungyu Lin <dennylin0707@gmail.com>
Subject: [PATCH] lib/vsprintf: Preserve the sign of seconds in %ptSp
Date: Tue, 20 Oct 2026 22:02:43 +0000 [thread overview]
Message-ID: <20261020220243.67919-1-dennylin0707@gmail.com> (raw)
The documentation defines %ptSp as equivalent to %lld.%09ld, but
timespec64_str() formats tv_sec with default_dec_spec, which does not
have SIGN set. Negative seconds are therefore printed as unsigned
values: {-1, 0} produces "18446744073709551615.000000000" instead of
"-1.000000000".
Negative values can arise from time differences, for example in
fnic_get_stats_data() after the realtime clock steps backwards.
Use signed decimal formatting for the seconds and add printf KUnit
coverage for zero, negative seconds and the signed limits.
Fixes: bccd5937447f ("lib/vsprintf: Add specifier for printing struct timespec64")
Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
---
Tested with GCC 13.3.0 and W=1. The ARM64 printf KUnit suite, booted
in QEMU 10.2.1 with HVF, reports 27 pass / 1 fail before the fix and
28 pass / 0 fail afterwards. A 32-bit x86 build of lib/vsprintf.o and
lib/tests/printf_kunit.o also passes without warnings.
lib/tests/printf_kunit.c | 15 +++++++++++++++
lib/vsprintf.c | 9 ++++++---
2 files changed, 21 insertions(+), 3 deletions(-)
diff --git a/lib/tests/printf_kunit.c b/lib/tests/printf_kunit.c
index eccf041ebd56..3bd5115c2eab 100644
--- a/lib/tests/printf_kunit.c
+++ b/lib/tests/printf_kunit.c
@@ -572,6 +572,21 @@ time_and_date(struct kunit *kunittest)
test("2019-01-04T15:32:23.011235813", "%ptS", &ts);
test("1546615943.011235813", "%ptSp", &ts);
+
+ ts.tv_sec = 0;
+ ts.tv_nsec = 0;
+ test("0.000000000", "%ptSp", &ts);
+
+ ts.tv_sec = -1;
+ test("-1.000000000", "%ptSp", &ts);
+ test("1969-12-31T23:59:59.000000000", "%ptS", &ts);
+ ts.tv_nsec = 999999999;
+ test("-1.999999999", "%ptSp", &ts);
+
+ ts.tv_sec = S64_MIN;
+ test("-9223372036854775808.999999999", "%ptSp", &ts);
+ ts.tv_sec = S64_MAX;
+ test("9223372036854775807.999999999", "%ptSp", &ts);
}
static void
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 1d95ab9314bf..7c1964e7822e 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1997,10 +1997,13 @@ char *timespec64_str(char *buf, char *end, const struct timespec64 *ts,
.flags = ZEROPAD,
};
- if (fmt[2] == 'p')
- buf = number(buf, end, ts->tv_sec, default_dec_spec);
- else
+ if (fmt[2] == 'p') {
+ spec = default_dec_spec;
+ spec.flags |= SIGN;
+ buf = number(buf, end, ts->tv_sec, spec);
+ } else {
buf = time64_str(buf, end, ts->tv_sec, spec, fmt);
+ }
if (buf < end)
*buf = '.';
buf++;
--
2.43.0
reply other threads:[~2026-10-20 22:04 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261020220243.67919-1-dennylin0707@gmail.com \
--to=dennylin0707@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=pmladek@suse.com \
--cc=rostedt@goodmis.org \
--cc=senozhatsky@chromium.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®