From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (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 E247F352C4F for ; Tue, 9 Jun 2026 15:23:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781018583; cv=none; b=sGhp7Vp/qmn7b9hYEFwQ2qPKdycJUGmvnH8+KK2bravixjIs5T3ZQJpXmaVba8+/Tk3ezQGuUDhOrBVOSUvfX0SpqTiB1VcP/SitKuaoWORrBqA3JSZSXu8cILUtn8gFreV+LDniBBAe1X47MENez8ZCpkCNhiTONz9OsVmeCLc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781018583; c=relaxed/simple; bh=kuIHPRKQTH2/4GUOzUX9fYHsOqljjnSwteHizxDZwoQ=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=skeIfvsJNlkr4UJptuU0lpJJqD+sTbpzs70XMp4XxItTWhFq0ddeEVmvaQyKsR+u/AludKAQObWe/2hjpVMUJSbWb5l1Z84fReK2HfQz9W2o7P1XOvGoy72xTZriNT054/Nddl+h/ZzZou0rWYqAKSTfHNWEnaCwlykDF7bbYrs= 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=EmEgUkYs; arc=none smtp.client-ip=209.85.128.49 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="EmEgUkYs" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-490c1915793so38415885e9.2 for ; Tue, 09 Jun 2026 08:23:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1781018580; x=1781623380; 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=Y9e8wekR8dwGp203+UO+bawrLa7V5VSovtR0ieIMnb0=; b=EmEgUkYsqMXt3PcL2oDNtf4s31q2zIbcBJv/dN84jh8FsfWBK6bQgpP48p9P2tDD/1 dC2PzE8lCP94utuoolSC3mrZmT5deLI7gAsHVNVxnKBUNJldj6ztV6L5jc8BLzPe1ZFw laN0EYv2YwD4Id02DUnA/aEtlKE7lfFKGVbiNBtGBk26cqsiGXz0PU1060nPlJij50kb CedZQ5Z9kyJW6JNhqkEOq/SFUuZEKYRerPMbLyw7JEJOqGV7VJxtXJHA79f8qj/xusS4 8SOaprgxETdCFvrtBp4kJ7WPgIaIDew3eLOWiIEUWYESOQcYeCp+vREh/qFl95ARSnFZ KLdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781018580; x=1781623380; 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=Y9e8wekR8dwGp203+UO+bawrLa7V5VSovtR0ieIMnb0=; b=JUTwstxEPYLKrlmpRiyKBHGqWIRZyhyIlZoVColEi2yAAWAlDiY/62ALEchFBMErA+ f2/bBJc50DOjX/+rkxEt7iX5BxEpdwshdm5/aaPBobJsJS47SSEH2oHwuEfSs3zdzBFf oA1XgoYsl9oaeBQDGIhCFxDLIZkjllILj0/R1Sa7S3hqryWUUohKrEvCGPBj6CGU+ZTP AqDsLprjVn5AFhLRFTx0hO6MSCa5Gu3JZVtdVHyQ+YEYZvxSwv3f8SzkbqYOnR3b1LBA PO9mqIsRwEb5LPcxslbLHYSxv18s+GKYH9qUdtfG1Gr3yfTo0cIRU7yflwiHXR/BykHJ QY+w== X-Forwarded-Encrypted: i=1; AFNElJ/Vp2JYxOL0JdcsKtPOX4L7VpJlBGTxnDDRur3ckU5EKoztnyGYybo73iAE+XQvluvc4UMsNZkQaK98EqY=@vger.kernel.org X-Gm-Message-State: AOJu0YwZF3F5Tt4KK6upcxJbW0tMu4euJMgRw0cxpAe8tUEdP0gL+gfp 7p1mbS51KQGOJeK+mF99TqtuM92msRPjOP41bSmQsj2D76jhIFxV4Uc3 X-Gm-Gg: Acq92OG8kDE6+TBTeXmTZeHn2TBBgKFbpsbQ/z6z/HghQ70eLmuFI7fL+56TGYM97jv kb4QAfK7A3dvF1kVhvsxTdrHw1iyWfw6vKYJPljsijcHFOInXG7/j874LsA8om96YvY1lgq7A4X Ywl5VKVY9fjRcd4imZXnelPA53XOyUGUVGi9B+ELV/HXxN+OwjMLFaNGQeHDbEGBPtlWF8OJixx H2BuXEXDzZ575vdsMYYM3sEy89kOhpAoTDFfW0jsAHHX/jXdzTapoCYkmUJv3slfQG9+ZIu64Vy nskHUYZ6Vz9YURvhaYtTYcNL46QZ4HbHOleXK+Ks1bsLG7YoTR+jniKoJF469TLpPTvzC3PO4EO q7wptTd2+cikQIToWfBSw7Pyfb1Xawf2NN1A3VScyQSgygqo3uAi1XgonzM+P4tJxqFPYpf+yP3 5tDsjcBhw709oO7GE5qp/jLbJ5iQJ6Jb84LpHHfVo6oOYtk+JDkgU0UfqQBxHm2JHyxXT1Ov0= X-Received: by 2002:a05:600c:3e87:b0:490:bcf6:469f with SMTP id 5b1f17b1804b1-490c251d8e4mr357536025e9.0.1781018580077; Tue, 09 Jun 2026 08:23:00 -0700 (PDT) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-490bc3e5a00sm484287205e9.15.2026.06.09.08.22.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jun 2026 08:22:59 -0700 (PDT) Date: Tue, 9 Jun 2026 16:22:58 +0100 From: David Laight To: "Danilo Krummrich" Cc: "Kees Cook" , , "Arnd Bergmann" , , , "Greg Kroah-Hartman" , "Rafael J. Wysocki" Subject: Re: [PATCH next] driver core: Replace strcpy() with memcpy() Message-ID: <20260609162258.5228eda2@pumpkin> In-Reply-To: References: <20260606202633.5018-1-david.laight.linux@gmail.com> 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, 09 Jun 2026 16:08:55 +0200 "Danilo Krummrich" wrote: > On Sat Jun 6, 2026 at 10:25 PM CEST, david.laight.linux wrote: > > diff --git a/drivers/base/platform.c b/drivers/base/platform.c > > index 75b4698d0e58..63fdfffe1a8c 100644 > > --- a/drivers/base/platform.c > > +++ b/drivers/base/platform.c > > @@ -617,10 +617,11 @@ static void platform_device_release(struct device *dev) > > struct platform_device *platform_device_alloc(const char *name, int id) > > { > > struct platform_object *pa; > > + size_t len = strlen(name); > > This could be strlen(name) + 1 right away, which would also avoid the memcpy() > to implicitly rely on kzalloc() zeroing the memory, where strcpy() was > self-contained before. I tried not to do that 'optimisation'. Here the kzalloc() is almost certainly needed for other reasons. There is also the issue that, in principle (but probably not in practise), the input string might change between the strlen() and the copy. So it is probably best practise to not rely on the '\0' still being there when the copy is done. > > > > > - pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL); > > + pa = kzalloc(sizeof(*pa) + len + 1, GFP_KERNEL); > > if (pa) { > > - strcpy(pa->name, name); > > + memcpy(pa->name, name, len); > > But in general, what's the improvement? strlen() still operates on a potentially > unbounded string? > > It removes the redundant strlen() calculation that is implied by the current > code, though this may be eliminated by CSE optimization. CSE won't eliminate it. It probably wouldn't eliminate a second strlen(name) either. I'm not trying to remove strlen(), just strcpy(). But not just changing them all the strscpy(). David