Re: [PATCH] lib/vsprintf: Preserve the sign of seconds in %ptSp
From: Andy Shevchenko
Date: Wed Oct 21 2026 - 05:22:49 EST
On Tue, Oct 20, 2026 at 10:02:43PM +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 signed decimal formatting for the seconds and add printf KUnit
> coverage for zero, negative seconds and the signed limits.
Thanks for the report and the patch. My comment below.
...
> - 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);
Instead, add a new default_signed_dec_spec above and use it here.
While at it, make field_width to be explicitly -1, so
static const struct printf_spec default_signed_dec_spec = {
.base = 10,
.field_width = -1,
.precision = -1,
.flags = SIGN,
};
> + } else {
> buf = time64_str(buf, end, ts->tv_sec, spec, fmt);
> + }
--
With Best Regards,
Andy Shevchenko