Skip to contentSkip to conversation

thermal: core: Use strnlen in thermal_zone_device_register_with_trips

PATCH Superseded Thorsten Blum 2 participants 2 messages Dec 15, 2025 – Dec 16, 2025 Thermal

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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.

---
 drivers/thermal/thermal_core.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/thermal/thermal_core.c +4 -2
index 17ca5c082643..63eb35b449c6 100644
@@ -1505,15 +1505,17 @@ 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;
 	int id;
 	int result;
 
-	if (!type || strlen(type) == 0) {
+	type_len = type ? strnlen(type, THERMAL_NAME_LENGTH) : 0;
+	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 14 lines · Thorsten Blum

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

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

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

Quoted 4 lines
drivers/thermal/thermal_core.c +1 -0
@@ -1505,15 +1505,17 @@ 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;

size_t type_len = 0;

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

Quoted 5 lines
drivers/thermal/thermal_core.c +1 -1
        int id;
        int result;
 
-       if (!type || strlen(type) == 0) {
+       type_len = type ? strnlen(type, THERMAL_NAME_LENGTH) : 0;

if (type)

        type_len = strnlen(type, THERMAL_NAME_LENGTH);

Pretty please.

Quoted 11 lines
drivers/thermal/thermal_core.c +2 -2
+       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);
--