* [PATCH v2] lib/vsprintf: Preserve the sign of seconds in %ptSp
@ 2026-10-21 11:06 Hungyu Lin
2026-10-21 14:12 ` Andy Shevchenko
0 siblings, 1 reply; 2+ messages in thread
From: Hungyu Lin @ 2026-10-21 11:06 UTC (permalink / raw)
To: pmladek, rostedt
Cc: andriy.shevchenko, linux, senozhatsky, akpm, linux-kernel, Hungyu Lin
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 a signed decimal printf_spec 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>
---
Changes in v2:
- Add default_signed_dec_spec with an explicit field_width of -1, as
suggested by Andy Shevchenko, and use it for the %ptSp seconds.
- Keep the test cases unchanged.
lib/tests/printf_kunit.c | 15 +++++++++++++++
lib/vsprintf.c | 9 ++++++++-
2 files changed, 23 insertions(+), 1 deletion(-)
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..50b99ec091a1 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1028,6 +1028,13 @@ static const struct printf_spec default_dec_spec = {
.precision = -1,
};
+static const struct printf_spec default_signed_dec_spec = {
+ .base = 10,
+ .field_width = -1,
+ .precision = -1,
+ .flags = SIGN,
+};
+
static const struct printf_spec default_dec02_spec = {
.base = 10,
.field_width = 2,
@@ -1998,7 +2005,7 @@ char *timespec64_str(char *buf, char *end, const struct timespec64 *ts,
};
if (fmt[2] == 'p')
- buf = number(buf, end, ts->tv_sec, default_dec_spec);
+ buf = number(buf, end, ts->tv_sec, default_signed_dec_spec);
else
buf = time64_str(buf, end, ts->tv_sec, spec, fmt);
if (buf < end)
--
2.43.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH v2] lib/vsprintf: Preserve the sign of seconds in %ptSp
2026-10-21 11:06 [PATCH v2] lib/vsprintf: Preserve the sign of seconds in %ptSp Hungyu Lin
@ 2026-10-21 14:12 ` Andy Shevchenko
0 siblings, 0 replies; 2+ messages in thread
From: Andy Shevchenko @ 2026-10-21 14:12 UTC (permalink / raw)
To: Hungyu Lin; +Cc: pmladek, rostedt, linux, senozhatsky, akpm, linux-kernel
On Wed, Oct 21, 2026 at 11:06:28AM +0000, Hungyu Lin wrote:
> 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 a signed decimal printf_spec 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")
Should we Cc to stable@?
> Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-10-21 14:12 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-21 11:06 [PATCH v2] lib/vsprintf: Preserve the sign of seconds in %ptSp Hungyu Lin
2026-10-21 14:12 ` Andy Shevchenko
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®