9front - general discussion about 9front
 help / color / mirror / Atom feed
From: "Devon H. O'Dell" <devon.odell@gmail.com>
To: 9front@9front.org
Subject: Re: [9front] strndup should use strnlen and memcpy
Date: Fri, 18 Dec 2020 06:26:18 -0800	[thread overview]
Message-ID: <CAFgOgC8uz6xjfm_o5JmQgO4W4hC9FsvQkx=iPGR4-KXs0-mN+Q@mail.gmail.com> (raw)
In-Reply-To: <BD534CA012C6A8B5DE0439481660D0AB@gmail.com>

Hi Sigrid,

The C2x working draft[1] incorporates strdup and strndup. Of strndup,
it says, "[t]he strndup function creates a string initialized with no
more than size initial characters of the *array pointed to by s*..."
(emphasis mine). Strings in the standard library are always called
strings. While any object occupying fewer than `size+1` bytes must be
a string, this is a side-effect of how the API is specified to behave.

POSIX is ambiguous, but implies that it expects a string either way:
"[t]he strndup() function shall be equivalent to the strdup()
function...", without subsequently placing exceptions on the input
constraints. Many (most?) implementations don't bother to check (or
explicitly allow non-strings), and WG14 likely recognized this when
standardizing the functionality in libc.

Anyway, if taking the "find length before allocation" approach to
implementation, the WG14 wording requires use of strnlen semantics.
One can implement without a strnlen by always allocating size+1 bytes,
which is why the standard can put strnlen_s in an annex.

--dho

[1]: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2454.pdf

On Fri, Dec 18, 2020 at 2:01 AM Sigrid Solveig Haflínudóttir
<ftrvxmtrx@gmail.com> wrote:
>
> > Because the terminating '\0' may not be here.
>
> Why wouldn't it be there?  Isn't strndup supposed to work on strings?
> Is there any (trustworthy standard-like) source that mentions
> non-terminated strings as something that should work with strndup?
>

  parent reply	other threads:[~2020-12-18 14:34 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-18  8:22 Xiao-Yong Jin
2020-12-18  8:38 ` Sigrid Solveig Haflínudóttir
2020-12-18  9:01   ` tlaronde
2020-12-18  9:59     ` Sigrid Solveig Haflínudóttir
2020-12-18 10:21       ` Kemal
2020-12-18 10:31         ` Sigrid Solveig Haflínudóttir
2020-12-18 10:48           ` Kemal
2020-12-18 11:02             ` Eli Cohen
2020-12-18 11:05               ` Eli Cohen
2020-12-18 11:29           ` tlaronde
2020-12-18 11:37             ` Sigrid Solveig Haflínudóttir
2020-12-18 11:43               ` Eli Cohen
2020-12-18 11:50               ` Kemal
2020-12-18 11:55                 ` Eli Cohen
2020-12-18 12:06                   ` Kemal
2020-12-18 13:22                   ` José Miguel Sánchez García
2020-12-18 14:34           ` Devon H. O'Dell
2020-12-18 14:26       ` Devon H. O'Dell [this message]
2020-12-18 15:27 ` ori

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='CAFgOgC8uz6xjfm_o5JmQgO4W4hC9FsvQkx=iPGR4-KXs0-mN+Q@mail.gmail.com' \
    --to=devon.odell@gmail.com \
    --cc=9front@9front.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 NNTP newsgroup(s).