From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183]) by sourceware.org (Postfix) with ESMTP id 4744E3858D1E for ; Fri, 23 Dec 2022 06:55:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4744E3858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gentoo.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gentoo.org Content-Type: multipart/signed; boundary="Apple-Mail=_4ADE03DA-4F8B-4C94-A244-6EA8578440E3"; protocol="application/pgp-signature"; micalg=pgp-sha512 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.300.101.1.3\)) Subject: Re: copying a string with truncation (was: [PATCH] resolv: add IPv6 support to inet_net_pton()) From: Sam James In-Reply-To: <5ec8375d-2074-0eba-51dc-988cb80f7b8d@gmail.com> Date: Fri, 23 Dec 2022 06:55:09 +0000 Cc: Zack Weinberg via Libc-alpha , Job Snijders , Florian Weimer Message-Id: <2361A19D-75E7-4B01-97E6-1B193DB600E4@gentoo.org> References: <5ec8375d-2074-0eba-51dc-988cb80f7b8d@gmail.com> To: Alejandro Colomar X-Mailer: Apple Mail (2.3731.300.101.1.3) X-Spam-Status: No, score=-4.0 required=5.0 tests=BAYES_00,JMQ_SPF_NEUTRAL,KAM_DMARC_STATUS,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS,TXREP autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --Apple-Mail=_4ADE03DA-4F8B-4C94-A244-6EA8578440E3 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On 22 Dec 2022, at 20:25, Alejandro Colomar via Libc-alpha = wrote: >=20 > On 12/22/22 19:28, Alejandro Colomar wrote:>> 1) Use strncpy() instead = of strlcpy() >> Would someone please add a function to glibc that truncates a string, = while still producing a string (as opposed to a null-padded fixed-width = character sequence)? >> Here goes an extract of the yet-unreleased strncpy(3) manual page = from the Linux man-pages master branch: >> DESCRIPTION >> These functions copy the string pointed to by src into a = null=E2=80=90padded >> character sequence at the fixed=E2=80=90width buffer pointed = to by dst. If the >> destination buffer, limited by its size, isn=E2=80=99t large = enough to hold the >> copy, the resulting character sequence is truncated. For = the differ=E2=80=90 >> ence between the two functions, see RETURN VALUE. >> An implementation of these functions might be: >> char * >> stpncpy(char *restrict dst, const char *restrict src, = size_t sz) >> { >> bzero(dst, sz); >> return mempcpy(dst, src, strnlen(src, sz)); >> } >> char * >> strncpy(char *restrict dst, const char *restrict src, = size_t sz) >> { >> stpncpy(dst, src, sz); >> return dst; >> } >> [...] >> CAVEATS >> The name of these functions is confusing. These functions = produce a >> null=E2=80=90padded character sequence, not a string (see = string_copying(7)). >> It=E2=80=99s impossible to distinguish truncation by the = result of the call, >> from a character sequence that just fits the destination = buffer; trun=E2=80=90 >> cation should be detected by comparing the length of the = input string >> with the size of the destination buffer. >> I'll be releasing the a new man-pages version very soon (a week at = most), so that this page and also the new string_copying(7) overview are = widely available. >=20 > I released a moment ago. So, I'd suggest either adding = strlcpy(3)&strlcat(3) to glibc, or stpecpy(3) (defined here: = ). Or even both, since each of them serves a purpose: = strlcpy(3)&strlcat(3) are for simpler code where performance and = truncation are not a concern, and stpecpy(3) does the same but faster = with just a few more bytes of code (and detecting truncation is even = simpler). >=20 strlcpy and strlcat is in POSIX next = (https://www.austingroupbugs.net/view.php?id=3D986) and will be in glibc in due course, I believe. See https://sourceware.org/bugzilla/show_bug.cgi?id=3D178. Florian sent = a patch a few months ago but I don't think anything happened with it yet = (https://patchwork.sourceware.org/project/glibc/patch/87fsjp7rqz.fsf@olden= burg.str.redhat.com/). I'll update a bug with this new information, as well. Best, sam --Apple-Mail=_4ADE03DA-4F8B-4C94-A244-6EA8578440E3 Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=signature.asc Content-Type: application/pgp-signature; name=signature.asc Content-Description: Message signed with OpenPGP -----BEGIN PGP SIGNATURE----- iNUEARYKAH0WIQQlpruI3Zt2TGtVQcJzhAn1IN+RkAUCY6VQzV8UgAAAAAAuAChp c3N1ZXItZnByQG5vdGF0aW9ucy5vcGVucGdwLmZpZnRoaG9yc2VtYW4ubmV0MjVB NkJCODhERDlCNzY0QzZCNTU0MUMyNzM4NDA5RjUyMERGOTE5MAAKCRBzhAn1IN+R kCzIAP9tn3pt/RsVE/2z6zC6j4TQpJi1YdzNRkcL/PNWylE8YwD/WFGvz3XeKEem cwi9JLQjXZm9eNiG4BvAYaNnARDwzg0= =SUYH -----END PGP SIGNATURE----- --Apple-Mail=_4ADE03DA-4F8B-4C94-A244-6EA8578440E3--