From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by sourceware.org (Postfix) with ESMTPS id 3C4CE3858C62 for ; Thu, 9 Nov 2023 12:43:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3C4CE3858C62 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=kernel.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3C4CE3858C62 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2604:1380:4601:e00::1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699533809; cv=none; b=xddCJMk6HsgM+mkijFPTDBwzTpEzvfcHD5PuxclvPd91lPI739vU+z+X8iYgQqccZ2VJ39Mm+TdDYAqaHgSRT3v/QI0VKchfJDqmFN4z09Au8JaK4yOxgnEqSeAKPhVoQMQy8qZc57JUiS3+NzL1eeSud7nvAS3R4SGXz516vwY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699533809; c=relaxed/simple; bh=TF8l9TLsKrQ7ULUxaoMZ0ksw9UFt9nNYY1dwUY+M+Jo=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=iFfYIvNjOF2ci/9naPWJQ0dhbS2estyq7yCuKNWw5Ds/wuN25xPzo6dqi+VRy0RT8dPAdbFpi6miEoLzQejwDyZBOkjEbIZrG255O0YP2Is49p97Ln6GaEM3SvKXZtVPi0bUqnuf6D0WC85CR6Ank1YlKzYQfdTM92s8++L0Rxc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id 3F73FB81F03; Thu, 9 Nov 2023 12:43:26 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F378AC433C7; Thu, 9 Nov 2023 12:43:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1699533805; bh=TF8l9TLsKrQ7ULUxaoMZ0ksw9UFt9nNYY1dwUY+M+Jo=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=iMsbI9kRbXRZX+AklT/+dLT5B7u6tj7RnxvNcqfK17VyOCKOtFV1x85GR3wmLwsCS gqjFznZZjFaaxJakDA0uIkTt84pUdDw+FTrFLOTQLIgBZqgPFc8/4fHrsJgkXZRIsE AztmH+xlV1GG07RlS3wv8PrgmttI5cvCeGhN36+JlsZu8aSSo9zTYx/oHDsUjpfsIw iigR1xb/pVVXq4jOft1LBWYIC/bAYlA31a5muiaPI/Vyfd9aY5Ig8PpWT6LYlX6Y+7 RSgcW0auduZGtL7r6WgEVd1U6Mt8wlTOqIBKgS+ZrQRksBzwYymIJ7RxjPpYqb7on9 4s/UBtqDiGQtg== Date: Thu, 9 Nov 2023 13:43:21 +0100 From: Alejandro Colomar To: Jonny Grant Cc: Matthew House , linux-man , GNU C Library Subject: Re: strncpy clarify result may not be null terminated Message-ID: References: <20231108021240.176996-1-mattlloydhouse@gmail.com> <20231109031345.245703-1-mattlloydhouse@gmail.com> <250e0401-2eaa-461f-ae20-a7f44d0bc5ad@jguk.org> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="CEbyUQuTjdHtCXNj" Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --CEbyUQuTjdHtCXNj Content-Type: text/plain; protected-headers=v1; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Date: Thu, 9 Nov 2023 13:43:21 +0100 From: Alejandro Colomar To: Jonny Grant Cc: Matthew House , linux-man , GNU C Library Subject: Re: strncpy clarify result may not be null terminated On Thu, Nov 09, 2023 at 12:38:37PM +0100, Alejandro Colomar wrote: > If you would want to write something based on Michael Kerrisk's article, > you could do this: >=20 > ssize_t > strxcpy(char *restrict dst, char *restrict src, size_t dsize) > { > if (strlen(src) < dsize) Heh, here's my off-by-one bug of the day. Good thing is I can fix it in a single place; unlike calling strncpy(3) all the time. This should have been <=3D. Cheers, Alex > return -1; >=20 > strcpy(dst, src); > } >=20 > You may also want to calculate 'dsize' automagically, to avoid human > error, in case it's an array, so you could write a macro on top of it: >=20 > #define STRXCPY(dst, src) strxcpy(dst, src, ARRAY_SIZE(dst)) >=20 > These are just small wrappers over standard functions, so you shouldn't > have problems adding them to your project. >=20 > This is my long term plan for shadow-utils, indeed. I'm first > transforming strncpy(3) calls into strlcpy(3) to remove the superfluous > padding, and later will use this strxcpy() to remove the truncated > strings to avoid misinterpretation. --=20 --CEbyUQuTjdHtCXNj Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEE6jqH8KTroDDkXfJAnowa+77/2zIFAmVM0+kACgkQnowa+77/ 2zLtgA/+IB12zTb+SL3bJAu9dIdQ9y9wmtlNPKMwVhiYzshaM/W+EHQ10MKWdCxm Nb/F204JpL43k1evpPb1Oor/SF6zxUkl8Z0VF4q1hv5FIkzgZ//HK6lC4MukG8Jy OXkyrYyM2vEP+C4Kol0XtJeqnYWBCh3VGHWxQeVKMGmsJxVkRZS5NazEFubhnF5I 0kjZJG59cgTDOHD3EiswAmlM1MKe19WcTV/jTBUPXsOj2CflPIqpHNVyE4+HTisI qZUj2gqCW8437s6t73e4rq+gHWiVv0I+48kT/8pWSQNiRcTG89PWr8nw14F49Zqf qxRWefypFBpKqLa2YuTOmxcmuwVK+LZ7IajvWBroQMj5403zK9artHd71gma42YF u9PBRENSve6V5Cx/Fc0WbdygTmfGp0m98TVMSzkCFZrUd4A717QTbgaSvxn092yZ seNTpjdSGIQdr13u8A9KULvw0DUHVCXXOXb04KjBghJ8u0jAMdVrru5nULzfnjXm +gFjJwWipr8MjnHGMy9oJuLso0KkFVLb6WoVQwKxXvROrYR5VAewHJx4W7nYO12N n5OMrAqQ+hdPl7yHGcpSQoTIA/nYR8v/kG3Qdwid0W4M2W76ArDg3Wcl6nhvhBGk YaC4YgSBDCAPw+1Qz5xZ2q3gzgmfxuZDfsJgrOpLpm1QhvI1a9s= =gTs+ -----END PGP SIGNATURE----- --CEbyUQuTjdHtCXNj--