From: Alejandro Colomar <alx.manpages@gmail.com>
To: libc-alpha@sourceware.org
Cc: Job Snijders <job@fastly.com>
Subject: Re: copying a string with truncation (was: [PATCH] resolv: add IPv6 support to inet_net_pton())
Date: Thu, 22 Dec 2022 21:25:53 +0100 [thread overview]
Message-ID: <5ec8375d-2074-0eba-51dc-988cb80f7b8d@gmail.com> (raw)
In-Reply-To: <caeb97c1-c905-e1d7-af25-148cd8fc42ad@gmail.com>
[-- Attachment #1.1: Type: text/plain, Size: 2864 bytes --]
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‐padded
> character sequence at the fixed‐width buffer pointed to by dst. If the
> destination buffer, limited by its size, isn’t large enough to hold the
> copy, the resulting character sequence is truncated. For the differ‐
> 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‐padded character sequence, not a string (see string_copying(7)).
>
> It’s impossible to distinguish truncation by the result of the call,
> from a character sequence that just fits the destination buffer; trun‐
> 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.
I released a moment ago. So, I'd suggest either adding strlcpy(3)&strlcat(3) to
glibc, or stpecpy(3) (defined here:
<http://www.alejandro-colomar.es/src/alx/alx/libstp.git/tree/src/stp/stpe/stpecpy.c>).
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).
I'll send an actual patch for adding stpecpy(3), to discuss with some actual code.
Cheers,
Alex
--
<http://www.alejandro-colomar.es/>
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2022-12-22 20:26 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-22 17:54 [PATCH] resolv: add IPv6 support to inet_net_pton() Job Snijders
2022-12-22 18:21 ` Florian Weimer
2022-12-22 18:28 ` copying a string with truncation (was: [PATCH] resolv: add IPv6 support to inet_net_pton()) Alejandro Colomar
2022-12-22 20:25 ` Alejandro Colomar [this message]
2022-12-23 6:55 ` Sam James
2022-12-23 7:00 ` Sam James
2022-12-23 11:42 ` Alejandro Colomar
2022-12-23 11:45 ` Alejandro Colomar
2022-12-31 15:11 ` Sam James
2023-01-17 10:56 ` [PATCH] resolv: add IPv6 support to inet_net_pton() Job Snijders
2023-04-19 11:31 ` Job Snijders
2024-03-17 1:23 ` Job Snijders
2024-03-17 3:19 ` Job Snijders
2024-03-17 11:18 ` Florian Weimer
2024-03-18 8:59 ` Job Snijders
2024-03-18 9:23 ` Andreas Schwab
2024-03-18 23:01 ` Job Snijders
2024-03-19 8:20 ` Andreas Schwab
2024-03-19 8:29 ` Job Snijders
2024-03-19 9:50 ` Andreas Schwab
2024-03-22 4:16 ` Job Snijders
2024-03-22 14:24 ` Zack Weinberg
2024-03-25 9:04 ` Job Snijders
2024-04-14 14:56 ` Job Snijders
2024-04-15 8:15 ` Xi Ruoyao
2024-03-25 8:45 ` Andreas Schwab
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5ec8375d-2074-0eba-51dc-988cb80f7b8d@gmail.com \
--to=alx.manpages@gmail.com \
--cc=job@fastly.com \
--cc=libc-alpha@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).