From: Alejandro Colomar <alx.manpages@gmail.com>
To: Vincent Lefevre <vincent@vinc17.net>, libc-alpha@sourceware.org
Cc: Stephan Bergmann <sbergman@redhat.com>,
Paul Eggert <eggert@cs.ucla.edu>,
Simon Chopin <simon.chopin@canonical.com>,
Andreas Schwab <schwab@linux-m68k.org>
Subject: Re: UB status of snprintf on invalid ptr+size combination?
Date: Sat, 18 Mar 2023 03:30:27 +0100 [thread overview]
Message-ID: <9c8cae93-cb8c-8689-1f0e-2b87514d3702@gmail.com> (raw)
In-Reply-To: <20230318020725.GA15308@zira.vinc17.org>
[-- Attachment #1.1: Type: text/plain, Size: 2608 bytes --]
Hello Vincent,
On 3/18/23 03:07, Vincent Lefevre wrote:
> On 2023-03-16 11:29:31 +0100, Stephan Bergmann wrote:
>> On 15/03/2023 13:39, Vincent Lefevre wrote:
>>> No, it is not obvious. If the C standard does not say that this is
>>> the size of the array, then it does not have to be the size of the
>>> array. The C standard just says:
>>>
>>> Otherwise, output characters beyond the n-1st are discarded rather
>>> than being written to the array, and a null character is written at
>>> the end of the characters actually written into the array.
>>
>> But in 7.1.4 "Use of library functions" the standard also says
>>
>>> If a function argument is described as being an array, the pointer
>>> passed to the function shall have a value such that all address
>>> computations and accesses to objects (that would be valid if the
>>> pointer did point to the first element of such an array) are
>>> valid.
>>
>> which could be construed as meaning that the n-1st array element must always
>> be accessible, even if a given invocation is known to always generate less
>> then n output characters.
>
> But the standard does not say that n is the size of the array.
> The size of the array could be the maximum of n and the size
> corresponding to the untruncated output string.
I guess you mean the minimum? If it were the maximum, then it would
never truncate.
[assuming you meant minimum]:
As Andreas mentioned, that's valid for ISO C, but POSIX is more
restrictive. Here's a quote from fprintf(3posix):
The snprintf() function shall be equivalent to sprintf(), with
the addition of the n argument which states the size of the
buffer referred to by s.
It clearly specifies that 'n' is the size of the buffer, so
implementations are free to assume that `s+n` is a valid pointer.
>
> Similarly, for strncpy, I would not see n as the size of the arrays,
> i.e. it is not allowed for the implementation to read characters
> past a null character (possibly unless this does not have unwanted
> effects), even though such characters would be among the first n
> characters.
The size argument to strncpy(3) is the size of the destination buffer,
not the size of the input buffer. The input buffer must be either
a string, or a character sequence at least as large as the destination
buffer. Thus, in strncpy(3), reads are limited by
`strnlen(src, size)`, but writes are limited by `size`.
Cheers,
Alex
--
<http://www.alejandro-colomar.es/>
GPG key fingerprint: A9348594CE31283A826FBDD8D57633D441E25BB5
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2023-03-18 2:30 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-14 19:47 Simon Chopin
2023-03-14 21:39 ` Paul Eggert
2023-03-15 9:22 ` Andreas Schwab
2023-03-15 15:54 ` Siddhesh Poyarekar
2023-03-15 18:34 ` Michael Hudson-Doyle
2023-03-19 14:45 ` manfred
2023-03-19 23:07 ` Vincent Lefevre
2023-03-20 12:05 ` Siddhesh Poyarekar
2023-03-20 12:17 ` Alejandro Colomar
2023-03-20 12:29 ` Siddhesh Poyarekar
2023-03-20 13:36 ` Vincent Lefevre
2023-03-20 13:50 ` Vincent Lefevre
2023-03-20 16:56 ` Siddhesh Poyarekar
2023-03-20 17:36 ` Vincent Lefevre
2023-03-20 15:09 ` Vincent Lefevre
2023-03-20 16:15 ` Alejandro Colomar
2023-03-20 16:33 ` Vincent Lefevre
2023-03-20 17:00 ` Vincent Lefevre
2023-03-20 17:31 ` Siddhesh Poyarekar
2023-03-20 17:45 ` Vincent Lefevre
2023-03-15 12:39 ` Vincent Lefevre
2023-03-16 10:29 ` Stephan Bergmann
2023-03-18 2:07 ` Vincent Lefevre
2023-03-18 2:30 ` Alejandro Colomar [this message]
2023-03-18 10:58 ` Vincent Lefevre
2023-03-18 15:01 ` Andreas Schwab
2023-03-19 22:48 ` Vincent Lefevre
2023-03-19 23:24 ` Andreas Schwab
2023-03-20 4:10 ` Vincent Lefevre
2023-03-20 9:19 ` Andreas Schwab
2023-03-20 10:42 ` Vincent Lefevre
2023-03-20 10:44 ` 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=9c8cae93-cb8c-8689-1f0e-2b87514d3702@gmail.com \
--to=alx.manpages@gmail.com \
--cc=eggert@cs.ucla.edu \
--cc=libc-alpha@sourceware.org \
--cc=sbergman@redhat.com \
--cc=schwab@linux-m68k.org \
--cc=simon.chopin@canonical.com \
--cc=vincent@vinc17.net \
/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).