Skip to contentSkip to conversation

thermal: core: Use strnlen in thermal_zone_device_register_with_trips

PATCH Applied Thorsten Blum 3 participants 3 messages Dec 16, 2025 – Jan 7 Thermal

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 thermal: core: Use strnlen in thermal_zone_device_register_with_trips

Replace strlen() with the safer strnlen() and calculate the length of the thermal zone name 'type' only once. No functional changes.

---
Changes in v2:
- Format the code differently (Rafael)
- Link to v1: https://lore.kernel.org/lkml/[email protected]/

---
 drivers/thermal/thermal_core.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/thermal/thermal_core.c +6 -2
index 17ca5c082643..90e7edf16a52 100644
@@ -1505,15 +1505,19 @@ thermal_zone_device_register_with_trips(const char *type,
 	const struct thermal_trip *trip = trips;
 	struct thermal_zone_device *tz;
 	struct thermal_trip_desc *td;
+	size_t type_len = 0;
 	int id;
 	int result;
 
-	if (!type || strlen(type) == 0) {
+	if (type)
+		type_len = strnlen(type, THERMAL_NAME_LENGTH);
+
+	if (type_len == 0) {
 		pr_err("No thermal zone type defined\n");
 		return ERR_PTR(-EINVAL);
 	}
 
-	if (strlen(type) >= THERMAL_NAME_LENGTH) {
+	if (type_len == THERMAL_NAME_LENGTH) {
 		pr_err("Thermal zone name (%s) too long, should be under %d chars\n",
 		       type, THERMAL_NAME_LENGTH);
 		return ERR_PTR(-EINVAL);
Signature · 2 lines
Thorsten Blum <[email protected]>
GPG: 1D60 735E 8AEF 3BE4 73B6  9D84 7336 78FD 8DFE EAD4
#1 lore raw
Quoted 17 lines · On Tue, 16 Dec 2025 14:09:44 +0100 Thorsten Blum <[email protected]>…

Replace strlen() with the safer strnlen() and calculate the length of the thermal zone name 'type' only once. No functional changes.

---
Changes in v2:
- Format the code differently (Rafael)
- Link to v1: https://lore.kernel.org/lkml/[email protected]/

---
 drivers/thermal/thermal_core.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/thermal/thermal_core.c +0 -0
index 17ca5c082643..90e7edf16a52 100644

drivers/thermal/thermal_core.c thermal_zone_device_register_with_trips(const char *type, · line 1511

Quoted 8 lines
drivers/thermal/thermal_core.c +1 -1
@@ -1505,15 +1505,19 @@ thermal_zone_device_register_with_trips(const char *type,
 	const struct thermal_trip *trip = trips;
 	struct thermal_zone_device *tz;
 	struct thermal_trip_desc *td;
+	size_t type_len = 0;
 	int id;
 	int result;
 
-	if (!type || strlen(type) == 0) {

That one can just be:

	if (!type || !type[0])

Although one might ask 'why bother'.
Pretty much all kernel code has to assume that the callers pass reasonably sane data.
Sanity checks for things that are easy to get wrong are one thing, but some of the checks in this function look pretty pointless.

drivers/thermal/thermal_core.c thermal_zone_device_register_with_trips(const char *type, · line 1516

Quoted 9 lines
drivers/thermal/thermal_core.c +4 -1
+	if (type)
+		type_len = strnlen(type, THERMAL_NAME_LENGTH);
+
+	if (type_len == 0) {
 		pr_err("No thermal zone type defined\n");
 		return ERR_PTR(-EINVAL);
 	}
 
-	if (strlen(type) >= THERMAL_NAME_LENGTH) {

The code does want to check this - to stop the actual copy getting truncated much later on - especially since at least on caller had to do an snprintf() into a 'too long' buffer to stop gcc bleating.
But I'm not sure you need to worry about strlen() v strnlen(). (The kernel hardening people probably disagree...)

David

Quoted 4 lines
drivers/thermal/thermal_core.c +1 -0
+	if (type_len == THERMAL_NAME_LENGTH) {
 		pr_err("Thermal zone name (%s) too long, should be under %d chars\n",
 		       type, THERMAL_NAME_LENGTH);
 		return ERR_PTR(-EINVAL);
maintainer Applied
#2 lore raw
In reply to Thorsten Blum
Quoted 30 lines · Thorsten Blum

Replace strlen() with the safer strnlen() and calculate the length of the thermal zone name 'type' only once. No functional changes.

---
Changes in v2:
- Format the code differently (Rafael)
- Link to v1: https://lore.kernel.org/lkml/[email protected]/

---
 drivers/thermal/thermal_core.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/thermal/thermal_core.c +5 -1
index 17ca5c082643..90e7edf16a52 100644
@@ -1505,15 +1505,19 @@ thermal_zone_device_register_with_trips(const char *type,
        const struct thermal_trip *trip = trips;
        struct thermal_zone_device *tz;
        struct thermal_trip_desc *td;
+       size_t type_len = 0;
        int id;
        int result;
 
-       if (!type || strlen(type) == 0) {
+       if (type)
+               type_len = strnlen(type, THERMAL_NAME_LENGTH);
+
+       if (type_len == 0) {

drivers/thermal/thermal_core.c thermal_zone_device_register_with_trips(const char *type,

Quoted 10 lines
drivers/thermal/thermal_core.c +1 -2
                pr_err("No thermal zone type defined\n");
                return ERR_PTR(-EINVAL);
        }
 
-       if (strlen(type) >= THERMAL_NAME_LENGTH) {
+       if (type_len == THERMAL_NAME_LENGTH) {
                pr_err("Thermal zone name (%s) too long, should be under %d chars\n",
                       type, THERMAL_NAME_LENGTH);
                return ERR_PTR(-EINVAL);
--

Applied as 6.20 material, thanks!