public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Paul Eggert <eggert@cs.ucla.edu>
To: Simon Chopin <simon.chopin@canonical.com>
Cc: libc-alpha@sourceware.org
Subject: Re: UB status of snprintf on invalid ptr+size combination?
Date: Tue, 14 Mar 2023 14:39:42 -0700	[thread overview]
Message-ID: <a5bda742-c534-f5a6-abba-39d29c26f01e@cs.ucla.edu> (raw)
In-Reply-To: <CAOOWow1L2ZMXE6S5pd3uKvAeHNQXMPtjew42LbAiQE-Pnd2ULg@mail.gmail.com>

On 3/14/23 12:47, Simon Chopin via Libc-alpha wrote:
> When the issue was first discovered[1], I didn't raise the issue because I
> dismissed it as UB, but it reappeared in an unrelated context, and
> colleagues pointed out that the wording in the standard doesn't actually
> say that the `n` argument is the size of the array.

That sounds like a misreading of the C standard. Even though the 
standard often does not explicitly say that a size argument is the size 
of an array, it's obvious from context that this is intended. So Florian 
is correct here that the call with INT_MAX is not portable C code.

For example, it's valid for snprintf to be implemented this way:

   int
   snprintf (char *buf, size_t size, char const *fmt, ...)
   {
      char *buf_limit = buf + size;
      ...
   }

even though this would have undefined behavior if BUF points to a 
character array smaller than SIZE.

glibc snprintf does the equivalent of the above internally, so it's a 
good thing that notcurses has been fixed to not use the INT_MAX trick as 
that trick already did not work in general with glibc and I assume with 
other C libraries (with rare and hard-to-diagnose failures), even aside 
from any fortification business that made the bug more visible.

  reply	other threads:[~2023-03-14 21:39 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 [this message]
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
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=a5bda742-c534-f5a6-abba-39d29c26f01e@cs.ucla.edu \
    --to=eggert@cs.ucla.edu \
    --cc=libc-alpha@sourceware.org \
    --cc=simon.chopin@canonical.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).