From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 434CA3B29C for ; Mon, 20 Nov 2023 23:39:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="X/8GlR8C" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E8ADCC433C7; Mon, 20 Nov 2023 23:39:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700523563; bh=/kj0NNW0xVOR5KrLzcgPE1xJ50lUA/vhKdrD5A5uK70=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=X/8GlR8C33O+E72Yi/vbDt01AvupWv/gN6YGWlrB/uAHgQYakWPBDFwEIhsrPcBdp ziOT7TS3p9DLBVsQnjzZp12Mt/8kktJ36Hz+agc69at4yYbP4jZKBhrS6sNe93/Zib zktnbnFbDrMP4N/LV5kZlg9T/N1jerLSSNMb5IqJV9bw7HJ8gAI36yE93CfgPbeHkX D0tX91v6BqUtqGoCsF9Znf40Jy0oCC4uzAGTzraUwBcQ1W2KNJVBeHENk4oJ0R98qA f9wQd+0xfY59dKwYReI8oxiGFYdHFbsiC+TFe++m+UcrAh5uTaNMdQbZnKSE6yBX72 yhtAjzdh1iG0g== Date: Tue, 21 Nov 2023 00:42:52 +0100 From: Alejandro Colomar To: Jonny Grant Cc: Paul Eggert , Matthew House , linux-man Subject: Re: strncpy clarify result may not be null terminated Message-ID: References: <66296bd3-9918-4dd1-8570-bccfbfd85239@jguk.org> <317576b1-a2cd-4f23-84d7-830c242d7e86@jguk.org> <24ade47a-a9b5-4be6-a322-babc0543f4bd@jguk.org> Precedence: bulk X-Mailing-List: linux-man@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="FWkUdlhtiod43To0" Content-Disposition: inline In-Reply-To: <24ade47a-a9b5-4be6-a322-babc0543f4bd@jguk.org> --FWkUdlhtiod43To0 Content-Type: text/plain; protected-headers=v1; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Date: Tue, 21 Nov 2023 00:42:52 +0100 From: Alejandro Colomar To: Jonny Grant Cc: Paul Eggert , Matthew House , linux-man Subject: Re: strncpy clarify result may not be null terminated Hi Jonny, On Mon, Nov 20, 2023 at 11:08:58PM +0000, Jonny Grant wrote: > > I discussed with Martin about this, IIRC, and he told me they had to > > decide which use of strncpy(3) to support, with the side effect that > > other uses would be warned about, and they chose the one that I think is > > bogus. >=20 > Fair enough. To be fair with Martin and GCC, the uses of strncpy(3) that I consider correct are so trivial that those warnings are unnecessary, since one should always use sizeof(dst) in the call, which can be done by a wrapper macro #define STRNCPY(dst, src) strncpy(dst, src, nitems(dst)) which is precisely what I did in shadow-utils. With this, the chances of getting the size wrong are 0, so I'd just turn off those warnings. Since strncpy(3) should always be used for writing to a fixed-size array, it's likely to be an actual array, of which you can take the size with nitems(). At least in shadow-utils, all calls have been replaced by that macro. I'm curious if all uses are similarly trivial in tar(1). So if this warning helps those who misuse strncpy(3) to at least misuse it safely, then it's a partially-good thing. Cheers, Alex --=20 --FWkUdlhtiod43To0 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEE6jqH8KTroDDkXfJAnowa+77/2zIFAmVb7vwACgkQnowa+77/ 2zKl6A//WbnTVgjlY+DyRgL1bU78v+2AjQRJjHZ/yllP5tfHR84cHo6TTFqvH5bS iqxq8PXx+K30rIDCoZALmHofL4+2EUdwvh0F1L9L18YQjenSkmZIzc39eq3P+InR drG9IIbZ1MYNeb32WW/5KUzbvx6cjJHbUZy3uAf5Pf8DSIzC0hrl8Kh3qBa2UwDf jV2tcFNMEPsukx21GhELLEpA5UNCJzd662OtEL41WpJxUDhmh1bAsXR5FFJisnOF mMfRaWIvS9MiHmhdlWEZIrFNPo/zfh0dsSOKL0OJ2p6NC+5GsKBwLua/Laeih59L Ccgmv3ERUEk5+K6j+JEocko3/HbkQ8pcP4GhMjCqWi0UV0aoF+s9CbVHK3BvCCxh 3n5fO00hIvBEIAKvytUArHtkEXgoFhWFRY4ibQ+b9PKZ44HOcNFrVcDgpfAwoT0/ yZcWOtEFdOQDVFMlOrBekqPaDTjbSToWYSfIGHvO3zj+J2fmZcogkyUvpozOKWbg zJHn/Qjn2TgkdT7WIb653/EFHnB2hMqpt4CXyDVMlAOh0tpC54ZSuXUSODl8+8C/ k3mgzY8ZW1e9wTM0suqGkizb5g3TDZyRory155hyfns4bpeSNiVoE2M0Rp/pU/CH PgNhdwkzKSXXEdkIJpDivFuy1Xq6dIFLF+nn2+EmyRJqHZ1zx3k= =0Wuf -----END PGP SIGNATURE----- --FWkUdlhtiod43To0--