Skip to contentSkip to conversation

thermal: sysfs: switch to use scnprintf() to suppress truncation warning

PATCH In review Andy Shevchenko 4 participants 6 messages Aug 17 – Sep 7 Thermal

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 thermal: sysfs: switch to use scnprintf() to suppress truncation warning

Switch the sysfs code to use scnprintf() to avoid warnings about potential truncation of the names of the sysfs attributes. We can't increase the buffer size because the size is the part of an ABI for some reason. Note, with the current size of buffer the affected attributes have a room for up to 1000 names, which ought to be enough for all cases. There is no functional change, as the same limitation was implied before.

Fixes c56f5c0342df ("Thermal: Make Thermal trip points writeable")
---
 drivers/thermal/thermal_sysfs.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/thermal/thermal_sysfs.c +6 -6
index b44abfc997ed..44bd7c50e4ac 100644
@@ -400,8 +400,8 @@ static int create_trip_attrs(struct thermal_zone_device *tz)
 		struct thermal_trip_attrs *trip_attrs = &td->trip_attrs;
 
 		/* create trip type attribute */
-		snprintf(trip_attrs->type.name, THERMAL_NAME_LENGTH,
-			 "trip_point_%d_type", i);
+		scnprintf(trip_attrs->type.name, sizeof(trip_attrs->type.name),
+			  "trip_point_%d_type", i);
 
 		sysfs_attr_init(&trip_attrs->type.attr.attr);
 		trip_attrs->type.attr.attr.name = trip_attrs->type.name;
@@ -410,8 +410,8 @@ static int create_trip_attrs(struct thermal_zone_device *tz)
 		attrs[i] = &trip_attrs->type.attr.attr;
 
 		/* create trip temp attribute */
-		snprintf(trip_attrs->temp.name, THERMAL_NAME_LENGTH,
-			 "trip_point_%d_temp", i);
+		scnprintf(trip_attrs->temp.name, sizeof(trip_attrs->temp.name),
+			  "trip_point_%d_temp", i);
 
 		sysfs_attr_init(&trip_attrs->temp.attr.attr);
 		trip_attrs->temp.attr.attr.name = trip_attrs->temp.name;
@@ -423,8 +423,8 @@ static int create_trip_attrs(struct thermal_zone_device *tz)
 		}
 		attrs[i + tz->num_trips] = &trip_attrs->temp.attr.attr;
 
-		snprintf(trip_attrs->hyst.name, THERMAL_NAME_LENGTH,
-			 "trip_point_%d_hyst", i);
+		scnprintf(trip_attrs->hyst.name, sizeof(trip_attrs->hyst.name),
+			  "trip_point_%d_hyst", i);
 
 		sysfs_attr_init(&trip_attrs->hyst.attr.attr);
 		trip_attrs->hyst.attr.attr.name = trip_attrs->hyst.name;
Signature · 1 line
2.50.1
#1 lore raw
Quoted 6 lines · Andy Shevchenko

Switch the sysfs code to use scnprintf() to avoid warnings about potential truncation of the names of the sysfs attributes. We can't increase the buffer size because the size is the part of an ABI for some reason. Note, with the current size of buffer the affected attributes have a room for up to 1000 names, which ought to be enough for all cases. There is no functional change, as the same limitation was implied before.

I still have the build issues, can we get this applied to v7.3-rcX, please?

Quoted 1 line
Fixes c56f5c0342df ("Thermal: Make Thermal trip points writeable")
Signature · 2 lines
With Best Regards,
Andy Shevchenko
reviewer Reviewed-by on v1 1/1
#2 lore raw
In reply to Andy Shevchenko
Quoted 6 lines · Andy Shevchenko

Switch the sysfs code to use scnprintf() to avoid warnings about potential truncation of the names of the sysfs attributes. We can't increase the buffer size because the size is the part of an ABI for some reason. Note, with the current size of buffer the affected attributes have a room for up to 1000 names, which ought to be enough for all cases. There is no functional change, as the same limitation was implied before.

Right, it doesn't touch the real truncation check problem only avoids the complain from the tool. We can think about this later in some redesign.

Quoted 6 lines
Fixes c56f5c0342df ("Thermal: Make Thermal trip points writeable")
---
  drivers/thermal/thermal_sysfs.c | 12 ++++++------
  1 file changed, 6 insertions(+), 6 deletions(-)
#3 lore raw
Quoted 16 lines · Lukasz Luba
Quoted 6 lines · Andy Shevchenko

Switch the sysfs code to use scnprintf() to avoid warnings about potential truncation of the names of the sysfs attributes. We can't increase the buffer size because the size is the part of an ABI for some reason. Note, with the current size of buffer the affected attributes have a room for up to 1000 names, which ought to be enough for all cases. There is no functional change, as the same limitation was implied before.

Right, it doesn't touch the real truncation check problem only avoids the complain from the tool. We can think about this later in some redesign.

Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Fixes c56f5c0342df ("Thermal: Make Thermal trip points writeable")
---
  drivers/thermal/thermal_sysfs.c | 12 ++++++------
  1 file changed, 6 insertions(+), 6 deletions(-)

Applied and added to linux-pm.git/fixes, thanks!

#4 lore raw
In reply to Andy Shevchenko
Quoted 2 lines · On Mon, 17 Aug 2026 12:33:24 +0200 Andy Shevchenko…

Switch the sysfs code to use scnprintf() to avoid warnings about potential truncation of the names of the sysfs attributes.

It has to be a 'bug' that 'format overflow' warnings are generated for snprintf() but not for scnprintf().
They really shouldn't behave differently.

I did some tests a while back and the warning goes away if you do 'some things' with the result (like comparing against the buffer size), but even explicitly ignoring it with a (void) cast isn't enough.

Using OPTIMIZER_HIDE_VAR() (or an _VAL variant) on the buffer size does stop the error, but I expect gcc to start checking sizeof(buf) as well. So both arguments might need hiding to get the warning ignored.

David

Quoted 49 lines

We can't increase the buffer
size because the size is the part of an ABI for some reason. Note, with the current size of buffer the affected attributes have a room for up to 1000 names, which ought to be enough for all cases. There is no functional change, as the same limitation was implied before.

Fixes c56f5c0342df ("Thermal: Make Thermal trip points writeable")
---
 drivers/thermal/thermal_sysfs.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/thermal/thermal_sysfs.c +6 -6
index b44abfc997ed..44bd7c50e4ac 100644
@@ -400,8 +400,8 @@ static int create_trip_attrs(struct thermal_zone_device *tz)
 		struct thermal_trip_attrs *trip_attrs = &td->trip_attrs;
 
 		/* create trip type attribute */
-		snprintf(trip_attrs->type.name, THERMAL_NAME_LENGTH,
-			 "trip_point_%d_type", i);
+		scnprintf(trip_attrs->type.name, sizeof(trip_attrs->type.name),
+			  "trip_point_%d_type", i);
 
 		sysfs_attr_init(&trip_attrs->type.attr.attr);
 		trip_attrs->type.attr.attr.name = trip_attrs->type.name;
@@ -410,8 +410,8 @@ static int create_trip_attrs(struct thermal_zone_device *tz)
 		attrs[i] = &trip_attrs->type.attr.attr;
 
 		/* create trip temp attribute */
-		snprintf(trip_attrs->temp.name, THERMAL_NAME_LENGTH,
-			 "trip_point_%d_temp", i);
+		scnprintf(trip_attrs->temp.name, sizeof(trip_attrs->temp.name),
+			  "trip_point_%d_temp", i);
 
 		sysfs_attr_init(&trip_attrs->temp.attr.attr);
 		trip_attrs->temp.attr.attr.name = trip_attrs->temp.name;
@@ -423,8 +423,8 @@ static int create_trip_attrs(struct thermal_zone_device *tz)
 		}
 		attrs[i + tz->num_trips] = &trip_attrs->temp.attr.attr;
 
-		snprintf(trip_attrs->hyst.name, THERMAL_NAME_LENGTH,
-			 "trip_point_%d_hyst", i);
+		scnprintf(trip_attrs->hyst.name, sizeof(trip_attrs->hyst.name),
+			  "trip_point_%d_hyst", i);
 
 		sysfs_attr_init(&trip_attrs->hyst.attr.attr);
 		trip_attrs->hyst.attr.attr.name = trip_attrs->hyst.name;
#5 lore raw
Quoted 10 lines · David Laight
Quoted 2 lines · On Mon, 17 Aug 2026 12:33:24 +0200 Andy Shevchenko…

Switch the sysfs code to use scnprintf() to avoid warnings about potential truncation of the names of the sysfs attributes.

It has to be a 'bug' that 'format overflow' warnings are generated for snprintf() but not for scnprintf().
They really shouldn't behave differently.

Quoted 7 lines

I did some tests a while back and the warning goes away if you do 'some things' with the result (like comparing against the buffer size), but even explicitly ignoring it with a (void) cast isn't enough.

Using OPTIMIZER_HIDE_VAR() (or an _VAL variant) on the buffer size does stop the error, but I expect gcc to start checking sizeof(buf) as well. So both arguments might need hiding to get the warning ignored.

Yeah, but in a few subsystems (mostly visible in 'input') the technique of using scnprintf() is used when either it's and ABI or we don't care about possible truncation.

Signature · 2 lines
With Best Regards,
Andy Shevchenko