public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* UB status of snprintf on invalid ptr+size combination?
@ 2023-03-14 19:47 Simon Chopin
  2023-03-14 21:39 ` Paul Eggert
  0 siblings, 1 reply; 32+ messages in thread
From: Simon Chopin @ 2023-03-14 19:47 UTC (permalink / raw)
  To: libc-alpha

Hi,

We've come across a couple of regressions[0] in 2.37 on armhf due to a
peculiar use of snprintf that boils down to this:

char [32]buf;
...
snprintf(buf, INT_MAX, "%s", "Hello world");

This used to work fine, but now fails in a non-obvious way: the return
value is the one you would expect from a successful call (here, 11), but
the last character is overwritten by a NUL terminator.

It's likely the new behavior has been introduced by

commit e88b9f0e5cc50cab57a299dc7efe1a4eb385161d
Author: Florian Weimer <fweimer@redhat.com>
Date:   Mon Dec 19 18:56:54 2022 +0100

    stdio-common: Convert vfprintf and related functions to buffers

We're hitting the issue because buf+INT_MAX overflows on armhf (being
32-bit and on the stack).

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.

Do you think it worth it to try and fix this regression? Florian
suggested an approach similar to

commit 0d50f477f47ba637b54fb03ac48d769ec4543e8d
Author: Florian Weimer <fweimer@redhat.com>
Date: Wed Jan 25 08:01:00 2023 +0100

    stdio-common: Handle -1 buffer size in __sprintf_chk & co (bug 30039)


Thanks in advance,
Simon

[0]: https://bugs.launchpad.net/ubuntu/+source/glibc/+bug/2011326
[1]: https://bugs.launchpad.net/ubuntu/+source/notcurses/+bug/2008108

--
Simon Chopin
Foundations Team                               	    Ubuntu MOTU/Core Dev
simon.chopin@canonical.com                            schopin@ubuntu.com

^ permalink raw reply	[flat|nested] 32+ messages in thread

end of thread, other threads:[~2023-03-20 17:45 UTC | newest]

Thread overview: 32+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-14 19:47 UB status of snprintf on invalid ptr+size combination? 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
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

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).