public inbox for linux-man@vger.kernel.org
 help / color / mirror / Atom feed
From: Alejandro Colomar <une+c@alejandro-colomar.es>
To: David Svoboda <svoboda@cert.org>
Cc: Robert Seacord <rcseacord@gmail.com>,
	 "sc22wg14@open-std. org" <sc22wg14@open-std.org>,
	Florian Weimer <fweimer@redhat.com>,
	 Carlos O'Donell <carlos@redhat.com>,
	Aaron Ballman <aaron@aaronballman.com>,
	 "libc-alpha@sourceware.org" <libc-alpha@sourceware.org>,
	"musl@lists.openwall.com" <musl@lists.openwall.com>,
	 "linux-man@vger.kernel.org" <linux-man@vger.kernel.org>,
	Paul Eggert <eggert@cs.ucla.edu>
Subject: Re: [SC22WG14.34672] n3752, alx-0029r8 - Restore the traditional realloc(3) specification
Date: Wed, 7 Jan 2026 21:36:05 +0100	[thread overview]
Message-ID: <aV7CC9Sr_Wn3dbAB@devuan> (raw)
In-Reply-To: <PH1P110MB163601133BF0167C46C8CC9DCC84A@PH1P110MB1636.NAMP110.PROD.OUTLOOK.COM>

[-- Attachment #1: Type: text/plain, Size: 6742 bytes --]

Hi David,

On Wed, Jan 07, 2026 at 02:31:31PM +0000, David Svoboda wrote:
> Here are some more thoughts on n3752
> 
> I wish the paper said something in the intro along the lines of "the
> goal to change realloc(0) to return a non-null value upon success".

Agree; that could be said more explicitly.  If I need to write another
revision, I'll try to remember to include that.

>  That is implicit in the paper, but should be explicit.
> 
> Alex, I recommend that when presenting this paper, please explain
> carefully the edge cases of how realloc() currently works in C23.

In C23, it doesn't work at all; that's the point.  :-)

> This is because some of us know how it works in C17,
> some of us remember DR 400,
> some only know C99's behavior,
> and some only know C89's.

The wording in all those standards is so inconsistent, that I doubt
there's a unique interpretation, and thus I doubt most people know how
it worked in those standards.

I think the only appropriate description of realloc(p,0) is that it was
never correctly specified in the standard, and what matters is what
implementations did in the real world.

> So your audience is fragmented, and think they know how it works
> (without knowing the edge cases like realloc(0)).

They should really read the paper.

> WRT this text:
> 
>         Code written for platforms returning a null pointer can be
>       migrated to platforms returning non-null, without significant
>       issues.
> 
> I am very skeptical that this is indeed true.

gnulib has done the change in 2024.  No regressions have been reported.
It is a major implementation, used in Linux systems by essential
programs, and in POSIX systems also by many programs.

> But to be precise, this is Glibc's problem rather than WG14's.  If
> they are willing to change glibc to return non-null on realloc(0),
> then I am willing to agree to this change in ISO C.
> 
> Is there any evidence that changing this behavior breaks no code?  Any
> test failures?  Any surveys?

There is gnulib having changed its behavior after this paper.  No
problems have been reported.

> This paper ignores Windows other than to mention that it would need to
> change too.  I doubt MS will update MSVC to accommodate this paper.
> So accepting this paper makes MSVC noncompliant.

I have no ways of contacting MS.  If anyone knows MS people from WG21
that would be relevant, please bring them into the discussion.


Cheers,
Alex

> 
> 
> --
> 
> David Svoboda svoboda@sei.cmu.edu<mailto:svoboda@sei.cmu.edu>
> 
> Senior Software Security Engineer
> 
> CERT Applied Systems Group
> 
> (412) 596-0401
> 
> From: David Svoboda <svoboda@cert.org>
> Date: Wednesday, January 7, 2026 at 8:38 AM
> To: Alejandro Colomar <une+c@alejandro-colomar.es>
> Cc: Robert Seacord <rcseacord@gmail.com>, sc22wg14@open-std. org <sc22wg14@open-std.org>, Florian Weimer <fweimer@redhat.com>, Carlos O'Donell <carlos@redhat.com>, Aaron Ballman <aaron@aaronballman.com>, libc-alpha@sourceware.org <libc-alpha@sourceware.org>, musl@lists.openwall.com <musl@lists.openwall.com>, linux-man@vger.kernel.org <linux-man@vger.kernel.org>, David Svoboda <svoboda@cert.org>
> Subject: Re: [SC22WG14.34664] n3752, alx-0029r8 - Restore the traditional realloc(3) specification
> 
> Hi, Alex!
> 
> I'm just going to respond to your first point (about what precisely the UB is):
> 
> The n3605 Annex J text says:
> 
>     (157) A non-null pointer returned by a call to the calloc, malloc, realloc, or aligned_alloc
>     function with a zero requested size is used to access an object (7.25.4).
> 
> This suggests that working with a non-null pointer that has been returned from realloc() is the UB, not invoking realloc(0).
> 
> But 7.25.4.8 (realloc) text says:
> 
>     Otherwise, if ptr does not match a pointer earlier returned by a memory management function, or
>     if the space has been deallocated by a call to the free or realloc function, or if the size is zero, the
>     behavior is undefined.
> 
> So you're right...UB comes from actually invoking realloc(), not working with whatever it returned.  The Annex J wording needs some cleanup.  Unless your paper gets accepted, in which case this Annex J UB goes away (and it wouldn't hurt to mention this in your paper, as we have found lots of obsolete UBs in Annex J).
> 
> 
> On 1/7/26, 3:43 AM, "Alejandro Colomar" <une+c@alejandro-colomar.es> wrote:
> 
> Hi David,
> 
> On Wed, Jan 07, 2026 at 12:47:18AM +0000, David Svoboda wrote:
> [...]
> 
> > I could argue that this UB is really redundant, as it is a variant of
> > the UB you get from reading past the end of an array.
> > (notwithstanding that the array has zero length).
> 
> Clearly not.  First of all, requesting an array of zero elements is not
> accessing the array, so you can't put both UB in the same category.
> 
> Second, NULL is not an array of zero elements.  NULL is an invalid
> pointer.  You can't do anything with NULL (or you couldn't until it was
> changed recently).  With an array of zero elements, you can do pointer
> arithmetic and hold the pointer just fine, as long as you don't read
> past the end:
> 
> int     a[0];
> int     *p, *end;
> 
> p = a;
> end = a + _Countof(a);
> 
> while (p < end)
> do_stuff(p);
> 
> The above is valid in compilers that support arrays of zero elements,
> but is (was) not valid with NULL.
> 
> And third, a pointer to the first element of an array of zero elements
> is *not* past the end; it is the same as a pointer one after the last
> element, and thus it's perfectly valid to hold it.
> 
> > We could also argue that this should be implementation-defined
> > behavior, or even unspecified behavior.
> 
> No, this is what we had in C17, and UB is much better than that.
> C17 destroyed the standard definition of realloc(p,0), even though it
> was supposed to be a no-op change.  To some degree, I'm happy that that
> changed, as that brought us to now, where it is obvious that the only
> way forward is to go back to the traditional BSD specification.
> 
> >  However, the UBSG's current
> > arsenal for slaying earthly demons has traditionally not extended to
> > changing what platforms do, as n3752 does. So IMO the UBSG should
> > stand back and wait for n3752 to be resolved.
> 
> 
> Have a lovely day!
> Alex
> 
> --
> <https://www.alejandro-colomar.es>
> 
> 
> --
> 
> David Svoboda svoboda@sei.cmu.edu<mailto:svoboda@sei.cmu.edu>
> 
> Senior Software Security Engineer
> 
> CERT Applied Systems Group
> 
> (412) 596-0401

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

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

  parent reply	other threads:[~2026-01-07 20:36 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20251223161139.196AB356CF9@www.open-std.org>
     [not found] ` <20251223164349.F0BC5356D1A@www.open-std.org>
     [not found]   ` <CACqWKsOkbArXm0XBUHkLcFFwDUP8iDQv_xPeNbomR0bKf-GCFw@mail.gmail.com>
     [not found]     ` <20251223211529.6365A356CF9@www.open-std.org>
     [not found]       ` <CACqWKsNQCchFZnFKKAyi-3HDtJYQ6sc=UZeb+hX48WQ1e7yj_w@mail.gmail.com>
     [not found]         ` <20260106210527.AA3FA356D3A@www.open-std.org>
     [not found]           ` <20260106214930.A5C8E356D2B@www.open-std.org>
2026-01-06 23:08             ` n3752, alx-0029r8 - Restore the traditional realloc(3) specification Alejandro Colomar
     [not found]             ` <PH1P110MB1636A1DEC402A702C28EBA9ECC84A@PH1P110MB1636.NAMP110.PROD.OUTLOOK.COM>
2026-01-07  7:51               ` [SC22WG14.34664] " Alejandro Colomar
     [not found]                 ` <PH1P110MB1636D74EDD4F3074AC98F12FCC84A@PH1P110MB1636.NAMP110.PROD.OUTLOOK.COM>
2026-01-07 20:16                   ` Alejandro Colomar
2026-01-07 22:01                     ` Joseph Myers
     [not found]                     ` <20260107220138.AE8DF356CFB@www.open-std.org>
2026-01-08 14:59                       ` [SC22WG14.34679] " Martin Uecker
     [not found]                         ` <PH1P110MB16361EF635C579E30308D647CC85A@PH1P110MB1636.NAMP110.PROD.OUTLOOK.COM>
2026-01-08 16:26                           ` Joseph Myers
     [not found]                   ` <PH1P110MB163601133BF0167C46C8CC9DCC84A@PH1P110MB1636.NAMP110.PROD.OUTLOOK.COM>
2026-01-07 17:30                     ` [SC22WG14.34664] " Florian Weimer
2026-01-07 20:28                       ` Alejandro Colomar
2026-01-08 11:18                         ` Florian Weimer
2026-01-08 11:53                           ` [musl] " Adhemerval Zanella Netto
2026-01-07 20:36                     ` Alejandro Colomar [this message]
     [not found]                     ` <20260108023757.3C908356D01@www.open-std.org>
2026-01-08 11:08                       ` [SC22WG14.34681] " Alejandro Colomar
     [not found]           ` <20260106221652.F02B3356D1A@www.open-std.org>
2026-01-06 23:15             ` [SC22WG14.34665] " Alejandro Colomar
     [not found]       ` <20260106201250.2A0A5356CEC@www.open-std.org>
     [not found]         ` <4dda2463-3adf-4fdf-a2c9-d58a2cdce415@informatik.uni-frankfurt.de>
2026-01-07 20:00           ` [SC22WG14.34662] " 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=aV7CC9Sr_Wn3dbAB@devuan \
    --to=une+c@alejandro-colomar.es \
    --cc=aaron@aaronballman.com \
    --cc=carlos@redhat.com \
    --cc=eggert@cs.ucla.edu \
    --cc=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.org \
    --cc=linux-man@vger.kernel.org \
    --cc=musl@lists.openwall.com \
    --cc=rcseacord@gmail.com \
    --cc=sc22wg14@open-std.org \
    --cc=svoboda@cert.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