Skip to contentSkip to conversation

lib/vsprintf: Preserve the sign of seconds in %ptSp

PATCH In review Hungyu Lin 2 participants 3 messages Nov 9 – Nov 10

Subsystems (3)
lore
Share & tools
lore

Hungyu Lin

author
#0 lore raw

PATCH v3 1/1 lib/vsprintf: Preserve the sign of seconds in %ptSp

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")

---
Changes in v3:
- Add Cc: [email protected], as suggested by Andy Shevchenko.
- Collect Reviewed-by and Tested-by tags.
- No code changes.

Previous version:
https://lore.kernel.org/lkml/[email protected]/

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(-)
lib/tests/printf_kunit.c +15 -0
index eccf041ebd56..3bd5115c2eab 100644
@@ -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
lib/vsprintf.c +8 -1
index 1d95ab9314bf..50b99ec091a1 100644
@@ -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)
Signature · 1 line
2.43.0
maintainer Reviewed-by on v3 1/1
#1 lore raw
Quoted 9 lines · 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.

Quoted 8 lines

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")

The patch looks good to me:

I am going to queue it for 7.5.

I am sorry that it me so long to get to it and missing 7.4 merge window. My TODO list somehow exploded around the Plumbers conference and I am still catching up with it.

Best Regards,
Petr

maintainer
#2 lore raw
In reply to Hungyu Lin
Quoted 9 lines · 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.

Quoted 8 lines

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")

JFYI, the patch has been committed into printk/linux.git, branch for-7.5-trivial.

Best Regards,
Petr