Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp1502201rdb; Sat, 10 Feb 2024 05:02:54 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVmrWOyQWEwIj2NKx2+8Fdphs3FjVyHxqF0irIEgtaxxSCBDdXDf+aaPSPD1kUtsw+6+ff7njfUV0mntakl4Z8ZuJHiseXnk7EfRp9hgw== X-Google-Smtp-Source: AGHT+IEAR6ZeiLV53exWzJRi/JXhz1S7QeoPi3BF37HH/rImj+/1Pv/P7f4rsaPs0On48U3pBcVt X-Received: by 2002:ac8:5c8b:0:b0:42c:43d1:1465 with SMTP id r11-20020ac85c8b000000b0042c43d11465mr2681826qta.9.1707570174650; Sat, 10 Feb 2024 05:02:54 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707570174; cv=pass; d=google.com; s=arc-20160816; b=ocC+Cm0xGODW7IOfC43VpGBiHxykelB5UBRCs+PP8v3yYKhcuE9ng8dGK7i88LWdcG nI0sckoBTTtms397d+MUjuv86TNX8UfpC9g/1NPprirr17lyFwIENZ/qr6tODhHAHS8+ not1Z6q8FOgafpisym000xFAbKDufk3qYyYxlOkCE3GP7f3HeDLJy/vmD/nEjBS85Z1d k+89/FL6Bg0b8szX6cNSHPqat0K2gUhY5POQfw320CAr7R3r3nVf/YDgcqaXitJo3hmp t8YWkzQ5/wHUmnoAbOsbWhKr0fyrugx4S6yEdgxMzA7OEsMMVrNQ7m2G2K5tjjCBWXy0 Fccg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:content-language:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:accept-language :in-reply-to:references:message-id:date:thread-index:thread-topic :subject:cc:to:from; bh=z++avKfPq9qDu4RZiReKmDmUzs0wTT/wjh7b/cwG4bU=; fh=yfvy4JdbrTeir9LgTNR/6yZv8GDUsJAeWPrYSZi/nsM=; b=NA4/BD8tllBlqPC2aJ7XWcfyFMVlfPyVECuD3E0wI9dMh9eUziiicllCfw0Qt9p7Nv l4VudaOe2kr68/A8lmH66qjxdzgIglsLj2KKJTqWem32bnYyuw9a4s+qIGYaKDh6/tEx h/uxfLu3/sqFUhKnPL9f0afA9KAVTDcCjaX0k070AG4Wd/nh2ZBCY28CS2O6SWpLxD3V WxHgoHwrVRGuKm9ZfMVUfrXcfREIpnTdjAXQgLNbtLS5V0rMz/ljgJMHCKu/SJnnsUe9 mYinI91e4b8SbV1c8b5yEYw22FfvjcGqFQSejnNgL127t9pO8tFKBf3Qu2J0RhBz/khw OAvg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=aculab.com dmarc=pass fromdomain=aculab.com); spf=pass (google.com: domain of linux-kernel+bounces-60408-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60408-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com X-Forwarded-Encrypted: i=2; AJvYcCWQysP1FTF//8R0gKE3RSEfDJQnpDgKi5qdSIqfyMTxeb5Q33rqEAr4wOQwbMIg2GKyWQ3uJjdIsSXYwFdkgEamr8HIWLJSTixOvfpcnw== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id y4-20020a05622a004400b0042c6840f48fsi871818qtw.66.2024.02.10.05.02.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 10 Feb 2024 05:02:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-60408-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=aculab.com dmarc=pass fromdomain=aculab.com); spf=pass (google.com: domain of linux-kernel+bounces-60408-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60408-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 488591C21092 for ; Sat, 10 Feb 2024 13:02:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C70F6481D0; Sat, 10 Feb 2024 13:02:47 +0000 (UTC) Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 772313D0DF for ; Sat, 10 Feb 2024 13:02:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.58.85.151 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707570167; cv=none; b=u1laY42ohMsfiWzAlkRV+/2+RI7m/0L3cPgDODfL6iOJIjZlg4hZv3nub8021AhsNQyW9wL/oapACMErn8oF377k2X2dqVi35t78JZiGwYujwlwyMe5iXdfM/PhI783nmV3KD/oOqcPm18nIn6KC8eKcM9hYS92d1IdEsbbtHFk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707570167; c=relaxed/simple; bh=z++avKfPq9qDu4RZiReKmDmUzs0wTT/wjh7b/cwG4bU=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=N51oPHjCR+mwqaXkABEAly0L1povNHzWXLAkvoIMSvXcNiJ47tQh+727OWNf8RLEP++V1cHQyc2JaNQOpku+trIrSGLha8UGix/rXvu9z3TJk52ivxhGMteXozh+bn3Wb7c/XtdTGPAiYjNzWm1/uOpDZF68H65Pa7cmxERIr2M= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM; spf=pass smtp.mailfrom=aculab.com; arc=none smtp.client-ip=185.58.85.151 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=aculab.com Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with both STARTTLS and AUTH (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id uk-mta-102-tuk_flGuPWqAy7Bypew2Tg-1; Sat, 10 Feb 2024 13:02:41 +0000 X-MC-Unique: tuk_flGuPWqAy7Bypew2Tg-1 Received: from AcuMS.Aculab.com (10.202.163.4) by AcuMS.aculab.com (10.202.163.4) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Sat, 10 Feb 2024 13:02:21 +0000 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Sat, 10 Feb 2024 13:02:20 +0000 From: David Laight To: 'Pavel Machek' , Justin Stitt CC: Kees Cook , Andy Shevchenko , Rasmus Villemoes , "linux-hardening@vger.kernel.org" , "linux-kernel@vger.kernel.org" Subject: RE: [RFC] string: Allow 2-argument strscpy() Thread-Topic: [RFC] string: Allow 2-argument strscpy() Thread-Index: AQHaWe5TIJbv1x38yUWctvLGNyIGYrEDjCtw Date: Sat, 10 Feb 2024 13:02:20 +0000 Message-ID: <7060edadcbac4452bd70d7894a50568d@AcuMS.aculab.com> References: <20240129202901.work.282-kees@kernel.org> <20240129215525.4uxchtrywzzsrauc@google.com> In-Reply-To: Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable From: Pavel Machek > Sent: 07 February 2024 17:52 >=20 > > > Using sizeof(dst) is the overwhelmingly common case for strscpy(). > > > Instead of requiring this everywhere, allow a 2-argument version to b= e > > > used that will use the sizeof() internally. > > > > Yeah, this is definitely the case. I have a ton of patches replacing > > strncpy with strscpy [1] and many of them match the pattern of: > > | strscpy(dest, src, sizeof(dest)) > > > > BTW, this hack for function overloading is insane. Never really looked = into > > it before. >=20 > This hack is insane, but this is also highly confusing, please don't > do this. An alternative would be to convert xxx(tgt, src, 0) to xxx(tgt, src, sizeof (tgt) - that is when the specified length is a compile-time constant zero. Either with: =09(__builtin_constat_p(len) && (len) =3D=3D 0 ? sizeof (dst) : (len)) Or, leveraging is_constexpr() and doing (I've probably got the syntax wrong= ): =09__Generic(0 ? (void *)(len) : (int *)0, =09=09void *: len, =09=09int *: sizeof (dst)) That probably needs a helper: =09is_constzero(value, if_zero, if_non_zero) to make it more generally useful. =09David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1= PT, UK Registration No: 1397386 (Wales)