public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Alejandro Colomar <alx.manpages@gmail.com>
To: Helge Kreutzmann <debian@helgefjell.de>,
	GNU C Library <libc-alpha@sourceware.org>
Cc: mario.blaettermann@gmail.com, linux-man@vger.kernel.org
Subject: Re: Issue in man page wcsncpy.3
Date: Sun, 4 Dec 2022 21:42:58 +0100	[thread overview]
Message-ID: <dbe4f048-4522-fcd2-3edf-ff192804c076@gmail.com> (raw)
In-Reply-To: <20221204090724.GA1249@Debian-50-lenny-64-minimal>


[-- Attachment #1.1: Type: text/plain, Size: 2547 bytes --]

Hi Helge, glibc developers,

On 12/4/22 10:07, Helge Kreutzmann wrote:
> Without further ado, the following was found:
> 
> Issue:    Is the "L" in the bracket (for the NULL character) correct?
> 
> "The B<wcsncpy>()  function is the wide-character equivalent of the"
> "B<strncpy>(3)  function.  It copies at most I<n> wide characters from the"
> "wide-character string pointed to by I<src>, including the terminating null"
> "wide character (L\\(aq\\e0\\(aq), to the array pointed to by I<dest>."
> "Exactly I<n> wide characters are written at I<dest>.  If the length"
> "I<wcslen(src)> is smaller than I<n>, the remaining wide characters in the"
> "array pointed to by I<dest> are filled with null wide characters.  If the"
> "length I<wcslen(src)> is greater than or equal to I<n>, the string pointed"
> "to by I<dest> will not be terminated by a null wide character."

As an unrelated note.  I've had this running in my mind for some time... your 
various bug reports for strncpy(3) and similar wide character functions have 
triggered those thougts.

I'm going to mark strncpy(3) and similar functions as deprecated, even if no 
libc or standard has done so.  There's wide agreement (at least in some 
communities) that strncpy(3) _is evil_.  There's simply no use for it.

I propose that glibc also marks it as deprecated.

I've worked for a few months on improving string handling in various projects: 
shadow-utils <https://github.com/shadow-maint/shadow/pull/569>, and in nginx 
Unit.  I've come to the following guidelines for using strings:

-  strlcpy(3):  Copy from string to string, detecting truncation
-  strscpy(9):  Copy from untrusted string to string, detecting broken ones
-  ustr2str():  Copy from unterminated string to string
    -  Definition: 
<https://github.com/shadow-maint/shadow/pull/569/files#diff-a2b8f8568ad025c2aeb4c64d3510f42d874e9ef0a64a89c2dd750aa93aabcd97>

-  stpecpy():  Copy from string to string, with easy & safe concatenation, and 
reporting truncation at the end of the chain call.
    -  Definition: <https://software.codidact.com/posts/285946/287522#answer-287522>

-  ustr2stpe():  Combination of ustr2str() and stpecpy().
    -  Definition:  Not yet public


And also, memcpy(), mempcpy(), or memccpy() can be used for copying unterminated 
strings.

But I don't see any scenario where strncpy() is the right function to call.  And 
the name is certainly not telling that either.


Cheers,

Alex


-- 
<http://www.alejandro-colomar.es/>

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

       reply	other threads:[~2022-12-04 20:43 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20221204090724.GA1249@Debian-50-lenny-64-minimal>
2022-12-04 20:42 ` Alejandro Colomar [this message]
2022-12-04 22:44   ` Alejandro Colomar
2022-12-04 23:06   ` Sam James
2022-12-04 23:09     ` Sam James
2022-12-04 23:28       ` Alejandro Colomar
2022-12-04 23:14     ` Alejandro Colomar

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=dbe4f048-4522-fcd2-3edf-ff192804c076@gmail.com \
    --to=alx.manpages@gmail.com \
    --cc=debian@helgefjell.de \
    --cc=libc-alpha@sourceware.org \
    --cc=linux-man@vger.kernel.org \
    --cc=mario.blaettermann@gmail.com \
    /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).