From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ej1-f45.google.com (mail-ej1-f45.google.com [209.85.218.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 646D531B819 for ; Tue, 16 Dec 2025 21:43:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765921399; cv=none; b=jaL/rJYVDr1xyn5GgHW5sD1JiPeSjyS3gDkvWasKTMqONn4QnkJZvjYhsl+gRYMqzRx9hpIGi4DfqpC0NGkztTWWHCYeckH61CjT967ZyvvM3WWTK1fEOQ3++HjNUAKpE/RsOLP1HXiWqnXAHKo3sf6eYSH1jbB+5YgvJ25QZCI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765921399; c=relaxed/simple; bh=m4/PosvYVWcXaVtxjw2E2cyJczRpEzbA94UHzJ+vxVg=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=s/poe6WPMvi5e7xwkTZGD1YnK4xX8pRgjMQ7Mfb2Yx8SMFYOZHvMbVyNTiWnIWxJqMN7JeBbdT/4kFWtR1TPkbK4+Z0pD0KuBkzTPcfg1llQnwE+v2yXU1++SRyDhNIo1O14S1KFzTMA41BZUHfIRKJEZs/DjG56rekyMo3YdzI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=mcXii3yG; arc=none smtp.client-ip=209.85.218.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mcXii3yG" Received: by mail-ej1-f45.google.com with SMTP id a640c23a62f3a-b79f8f7ea43so1108215866b.2 for ; Tue, 16 Dec 2025 13:43:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765921394; x=1766526194; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=xCYUk76BqorMFZGGzHYlq7Ql2Ms+ChYX93WODaTTv/U=; b=mcXii3yGKlx4PAh+PENYVNjA4miiRnWYoENtwnehGYpywKKg3cC0L7Eb06I+wdr6e5 wOis7BYU++1q3gcXq6Ku+7IrjKum2hnO292GJZLTfrziYdThN9kNP27SKdOFZYjAIcAM ZDHubgVrJPzg5Zyy04cRxHg6KlI6vH5Z7Fywwl9W5OgfXzmMJbVjiwzXpNf0obtNC/EG ILO4uciJE6L71eZFtaPb4acUJWELoBUCFbhMIW5yo/YN/OP9WWzNK4M1GMaAKZk8Vd4q 7FmLgxJTFXyaZ1TGYEf3FyKwCcL0040uKiZNy8Has4RH2UKMuvYPnGZqDfy0FriTcVbt LLBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765921394; x=1766526194; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=xCYUk76BqorMFZGGzHYlq7Ql2Ms+ChYX93WODaTTv/U=; b=GudQu5mgII/z++Rcdd2bzxcyA0sg3JjtTOOXZAx4JPwkAhldAM+OyIttZjuPIsB2uz 0Jpmx++SUERqD6eLA14+oOjK31D9Jo9TdZ4Jm64/Cd1MYpVWiQlbEjmffZn+fZtfZAee bkpkqhHsXEpz0AVKV/sPO/pBhEhOduccJT5sH/oiqNEKQAEGV6ltJkZn2alviMPecZQo MWBNdBuhzJ31vIC467zG428anlEHqmo5afGqS7KrqnvIGkHAvaym6z37w8FgrTJfBPBV AC6TGp5tDCFl6+H/kTcr4Ds2/S5nIubHbE1tXvo7fNFgKjCP1RebbKsvLfX6T/d8/3Sq vGxQ== X-Forwarded-Encrypted: i=1; AJvYcCUlE7KMI4Kj/sXlyn6kZIIWtmWstirifSsoevG8+Qbr93XR8CddZxesdavfUuwWzlRn+hKDgprjqt8nOq0=@vger.kernel.org X-Gm-Message-State: AOJu0YyCXzhujoZNENu6hNORf5HdCNIFHMy8luO+NA+FEEOMlDLM1ipM X62IBhcePtcmEtTVTJO0XGwL7shZA3teQkglxhaAsn+VouB1vnaIset7 X-Gm-Gg: AY/fxX4mUCAG2FXr9UDUZROUb3JSrhT3zH0DfQbRHVHDw3dAW3zdoobEg7FdD9hA0Cl PKVaGd3vMCEtEOVIH+Ux7IOSGm+IYr5lmhgOOGECozyrYxVPuIUI6/+donBoy5Bh0ORRPFiq7fe Kz7Cq/l7deIwsI7YHpnQg5FYR5m8WFex9lRHxW2KAPg3vphD9tUo5wXxs94ewPUiX6nR7svolkv unuA2YmsBSB+UTuFCD6ItCkRzEMwgOP1t18tNtwVGA6nyTJuQX7rZPjDl1v6tuwNlHjOvKCmgZr bWVxThnYqE1r4Q3Q0N2Fua4jKkoYetURlj1H+xy90wTIaKxwAa4HUNkTWkF6BVCI/1YsmIHoED4 lL28/OjzpSb9gffhGarweI0Usq5lk8JgHzrbRJp3yXxomHd7qYlKsF5RXXZS4RzWkDNFIw/j2sh 3Xl0mDWBfSv66NWqUAgIdBXHLP6yiDTDsBfSiPS2XnbPJHVENBuvAx X-Google-Smtp-Source: AGHT+IHa3y/03SZsTeHOlCjDJ8fIVGFQgr/bOY3qYup05WAAN+vlqgxoh1PfI7G59bazy8Aw3bdFPg== X-Received: by 2002:a05:6000:2384:b0:431:7a0:dbc2 with SMTP id ffacd0b85a97d-43107a0df48mr3058615f8f.31.1765914255221; Tue, 16 Dec 2025 11:44:15 -0800 (PST) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4310adef010sm670171f8f.35.2025.12.16.11.44.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Dec 2025 11:44:15 -0800 (PST) Date: Tue, 16 Dec 2025 19:44:13 +0000 From: David Laight To: Thorsten Blum Cc: "Rafael J. Wysocki" , Daniel Lezcano , Zhang Rui , Lukasz Luba , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] thermal: core: Use strnlen in thermal_zone_device_register_with_trips Message-ID: <20251216194413.1f12a3df@pumpkin> In-Reply-To: <20251216130943.40180-2-thorsten.blum@linux.dev> References: <20251216130943.40180-2-thorsten.blum@linux.dev> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Tue, 16 Dec 2025 14:09:44 +0100 Thorsten Blum wrote: > Replace strlen() with the safer strnlen() and calculate the length of > the thermal zone name 'type' only once. No functional changes. > > Signed-off-by: Thorsten Blum > --- > Changes in v2: > - Format the code differently (Rafael) > - Link to v1: https://lore.kernel.org/lkml/20251215121633.375193-1-thorsten.blum@linux.dev/ > --- > drivers/thermal/thermal_core.c | 8 ++++++-- > 1 file changed, 6 insertions(+), 2 deletions(-) > > diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c > index 17ca5c082643..90e7edf16a52 100644 > --- a/drivers/thermal/thermal_core.c > +++ b/drivers/thermal/thermal_core.c > @@ -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. > + 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 > + 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);