Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

Sic transit discus mundi -- From the System Administrator's Guide, by Lars Wirzenius


devel / comp.lang.c / Re: *rubeyes*: realloc(ptr, 0) is UB?

SubjectAuthor
* *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
+* Re: *rubeyes*: realloc(ptr, 0) is UB?Lawrence D'Oliveiro
|`* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
| `* Re: *rubeyes*: realloc(ptr, 0) is UB?Lawrence D'Oliveiro
|  `* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
|   `- Re: *rubeyes*: realloc(ptr, 0) is UB?Lawrence D'Oliveiro
+* Re: *rubeyes*: realloc(ptr, 0) is UB?David Brown
|`* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
| `- Re: *rubeyes*: realloc(ptr, 0) is UB?BGB
+* Re: *rubeyes*: realloc(ptr, 0) is UB?Scott Lurndal
|+* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||`* Re: *rubeyes*: realloc(ptr, 0) is UB?Scott Lurndal
|| +- Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
|| `* Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||  `* Re: *rubeyes*: realloc(ptr, 0) is UB?Scott Lurndal
||   +* Re: *rubeyes*: realloc(ptr, 0) is UB?James Kuyper
||   |`* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   | `* Re: *rubeyes*: realloc(ptr, 0) is UB?James Kuyper
||   |  `* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |   +- Re: *rubeyes*: realloc(ptr, 0) is UB?Chris M. Thomasson
||   |   `* Re: *rubeyes*: realloc(ptr, 0) is UB?Keith Thompson
||   |    +* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    |`* Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    | `* Re: *rubeyes*: realloc(ptr, 0) is UB?Richard Kettlewell
||   |    |  +- Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    |  `* Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    |   `* Re: *rubeyes*: realloc(ptr, 0) is UB?Richard Kettlewell
||   |    |    `* Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    |     +* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    |     |`- Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    |     `* Re: *rubeyes*: realloc(ptr, 0) is UB?Richard Kettlewell
||   |    |      `* Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    |       `* Re: *rubeyes*: realloc(ptr, 0) is UB?Richard Kettlewell
||   |    |        `- Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    +* Re: *rubeyes*: realloc(ptr, 0) is UB?Lawrence D'Oliveiro
||   |    |+* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    ||`* Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    || `* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    ||  `* Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    ||   `* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    ||    +* Re: *rubeyes*: realloc(ptr, 0) is UB?Keith Thompson
||   |    ||    |`* Re: *rubeyes*: realloc(ptr, 0) is UB?Chris M. Thomasson
||   |    ||    | `* Re: *rubeyes*: realloc(ptr, 0) is UB?Keith Thompson
||   |    ||    |  `* Re: *rubeyes*: realloc(ptr, 0) is UB?David Brown
||   |    ||    |   `- Re: *rubeyes*: realloc(ptr, 0) is UB?Chris M. Thomasson
||   |    ||    +* Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    ||    |`* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    ||    | `- Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    ||    `- Re: *rubeyes*: realloc(ptr, 0) is UB?James Kuyper
||   |    |`* Re: *rubeyes*: realloc(ptr, 0) is UB?Keith Thompson
||   |    | +* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    | |+* Re: *rubeyes*: realloc(ptr, 0) is UB?Keith Thompson
||   |    | ||`- Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    | |+* Re: *rubeyes*: realloc(ptr, 0) is UB?bart
||   |    | ||+- Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    | ||+* Re: *rubeyes*: realloc(ptr, 0) is UB?Chris M. Thomasson
||   |    | |||+* Re: *rubeyes*: realloc(ptr, 0) is UB?bart
||   |    | ||||+* Re: *rubeyes*: realloc(ptr, 0) is UB?David Brown
||   |    | |||||+* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    | ||||||`- Re: *rubeyes*: realloc(ptr, 0) is UB?David Brown
||   |    | |||||+* Re: *rubeyes*: realloc(ptr, 0) is UB?Keith Thompson
||   |    | ||||||+* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    | |||||||`* Re: *rubeyes*: realloc(ptr, 0) is UB?bart
||   |    | ||||||| `- Re: *rubeyes*: realloc(ptr, 0) is UB?Malcolm McLean
||   |    | ||||||`* Re: *rubeyes*: realloc(ptr, 0) is UB?David Brown
||   |    | |||||| `* Re: *rubeyes*: realloc(ptr, 0) is UB?Lawrence D'Oliveiro
||   |    | ||||||  `- Re: *rubeyes*: realloc(ptr, 0) is UB?David Brown
||   |    | |||||+- Re: *rubeyes*: realloc(ptr, 0) is UB?bart
||   |    | |||||`* Re: *rubeyes*: realloc(ptr, 0) is UB?Chris M. Thomasson
||   |    | ||||| `- Re: *rubeyes*: realloc(ptr, 0) is UB?Chris M. Thomasson
||   |    | ||||+* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    | |||||`- Re: *rubeyes*: realloc(ptr, 0) is UB?Chris M. Thomasson
||   |    | ||||`* Re: *rubeyes*: realloc(ptr, 0) is UB?Chris M. Thomasson
||   |    | |||| +* Re: *rubeyes*: realloc(ptr, 0) is UB?Malcolm McLean
||   |    | |||| |`- Re: *rubeyes*: realloc(ptr, 0) is UB?Chris M. Thomasson
||   |    | |||| `* Re: *rubeyes*: realloc(ptr, 0) is UB?bart
||   |    | ||||  +- Re: *rubeyes*: realloc(ptr, 0) is UB?bart
||   |    | ||||  `- Re: *rubeyes*: realloc(ptr, 0) is UB?Lawrence D'Oliveiro
||   |    | |||`- Re: *rubeyes*: realloc(ptr, 0) is UB?Lew Pitcher
||   |    | ||`* Re: *rubeyes*: realloc(ptr, 0) is UB?Richard Kettlewell
||   |    | || `* Re: *rubeyes*: realloc(ptr, 0) is UB?bart
||   |    | ||  +* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    | ||  |+- Re: *rubeyes*: realloc(ptr, 0) is UB?Kalevi Kolttonen
||   |    | ||  |+* Re: *rubeyes*: realloc(ptr, 0) is UB?bart
||   |    | ||  ||`* Re: *rubeyes*: realloc(ptr, 0) is UB?Chris M. Thomasson
||   |    | ||  || `- Re: *rubeyes*: realloc(ptr, 0) is UB?Chris M. Thomasson
||   |    | ||  |`* Re: *rubeyes*: realloc(ptr, 0) is UB?Scott Lurndal
||   |    | ||  | `- Re: *rubeyes*: realloc(ptr, 0) is UB?Chris M. Thomasson
||   |    | ||  `- Re: *rubeyes*: realloc(ptr, 0) is UB?Richard Kettlewell
||   |    | |`* Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    | | `* Re: *rubeyes*: realloc(ptr, 0) is UB?Malcolm McLean
||   |    | |  +- Re: *rubeyes*: realloc(ptr, 0) is UB?Keith Thompson
||   |    | |  +- Re: *rubeyes*: realloc(ptr, 0) is UB?Keith Thompson
||   |    | |  +* Re: *rubeyes*: realloc(ptr, 0) is UB?Scott Lurndal
||   |    | |  |`- Re: *rubeyes*: realloc(ptr, 0) is UB?Lawrence D'Oliveiro
||   |    | |  +- Re: *rubeyes*: realloc(ptr, 0) is UB?Spiros Bousbouras
||   |    | |  `- Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    | `- Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    +* Re: *rubeyes*: realloc(ptr, 0) is UB?Keith Thompson
||   |    |+* Re: *rubeyes*: realloc(ptr, 0) is UB?Lawrence D'Oliveiro
||   |    ||`* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    |`* Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    `- Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   `- Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
|`* Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
+* Re: *rubeyes*: realloc(ptr, 0) is UB?Blue-Maned_Hawk
`- Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch

Pages:1234567
Re: *rubeyes*: realloc(ptr, 0) is UB?

<20240121134843.67@kylheku.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49043&group=comp.lang.c#49043

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-6894@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Sun, 21 Jan 2024 21:49:17 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 19
Message-ID: <20240121134843.67@kylheku.com>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me>
<uojudd$bs5q$1@dont-email.me> <uojv5f$c17v$1@dont-email.me>
<uok0ah$c5dj$1@dont-email.me> <87il3mbcfy.fsf@nosuchdomain.example.com>
Injection-Date: Sun, 21 Jan 2024 21:49:17 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="33c09ed4842a7f81f4877f94e0219235";
logging-data="416738"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/sLVk3XbaBaLQVvPdT1xMkc9dayAzXiCI="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:3bf/8zM2pF0wiagmu5N38CgySuw=
 by: Kaz Kylheku - Sun, 21 Jan 2024 21:49 UTC

On 2024-01-21, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
> David Brown <david.brown@hesbynett.no> writes:
> [...]
>> Personally, I think it would be often more efficient in modern C if
>> the allocation system didn't track sizes at all, and "free" passed the
>> original size as a parameter. But that ship sailed long ago for
>> standard C. (C++ supports a sized deallocation system, and of course
>> there's nothing to stop you making your own allocator system for C.)
>
> I suspect that calling malloc() with one size and free() with a
> different one would have been a rich source of subtle bugs.

Welcome to C23! free_sized(malloc(42), 73) -> UB.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: *rubeyes*: realloc(ptr, 0) is UB?

<uok4n9$ctgb$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49045&group=comp.lang.c#49045

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc@freeuk.com (bart)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Sun, 21 Jan 2024 22:09:45 +0000
Organization: A noiseless patient Spider
Lines: 41
Message-ID: <uok4n9$ctgb$1@dont-email.me>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me>
<uojudd$bs5q$1@dont-email.me> <uojv5f$c17v$1@dont-email.me>
<uok0ah$c5dj$1@dont-email.me> <87il3mbcfy.fsf@nosuchdomain.example.com>
<20240121134843.67@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 21 Jan 2024 22:09:45 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b26e4d8877358ca1ee8cdab6f46f03ff";
logging-data="423435"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX186nQbwD8jajeGQrXgOtjkjXCxMjq9T4Qo="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:nqIewmhhbSXORcgAroifA7GA18A=
In-Reply-To: <20240121134843.67@kylheku.com>
Content-Language: en-GB
 by: bart - Sun, 21 Jan 2024 22:09 UTC

On 21/01/2024 21:49, Kaz Kylheku wrote:
> On 2024-01-21, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>> David Brown <david.brown@hesbynett.no> writes:
>> [...]
>>> Personally, I think it would be often more efficient in modern C if
>>> the allocation system didn't track sizes at all, and "free" passed the
>>> original size as a parameter. But that ship sailed long ago for
>>> standard C. (C++ supports a sized deallocation system, and of course
>>> there's nothing to stop you making your own allocator system for C.)
>>
>> I suspect that calling malloc() with one size and free() with a
>> different one would have been a rich source of subtle bugs.
>
> Welcome to C23! free_sized(malloc(42), 73) -> UB.
>

I've used such a sized free function in my own libraries for a long time.

Yes, you can pass the wrong size (although there are debugging options
where it will keep track of the sizes and double-check, if you suspect
such a bug).

But the real problems are the same as they are now in C:

free(p) when p has not been assigned a value from malloc
free(p); free(p); call twice
--- forget to call free
free(q); free the wrong pointer
free(p+1); pass an invalid pointer

Getting a p=malloc() correctly matched up with just one free(p) in a
different part of the program, at a different time, is the hard bit.

The easy bit is this:

p = malloc(sizeof(*p));
...
free_sized(p, sizeof(*p));

(Is it still malloc or is there a malloc_sized? Since part of the point
is not wasting time and memory managing that extra metadata.)

Re: *rubeyes*: realloc(ptr, 0) is UB?

<86cytugvve.fsf@linuxsc.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49046&group=comp.lang.c#49046

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17687@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Sun, 21 Jan 2024 14:33:09 -0800
Organization: A noiseless patient Spider
Lines: 69
Message-ID: <86cytugvve.fsf@linuxsc.com>
References: <20240116162506.143@kylheku.com> <QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com> <9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com> <QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me> <20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me> <20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com> <87msszbb08.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="92215711b06d8c3aeb5738d6959250bf";
logging-data="429832"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+/LNHYRAdsVT9AiyIbVheTSoy0wNV4f5Y="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:qLGKMkZSZwrxvjtIl+fnIq5/h8s=
sha1:W+DN6BhSdw75vQ5kLwD5MkXyP+c=
 by: Tim Rentsch - Sun, 21 Jan 2024 22:33 UTC

Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:

> Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
>
>> Kaz Kylheku <433-929-6894@kylheku.com> writes:
>>
>>> On 2024-01-18, James Kuyper <jameskuyper@alumni.caltech.edu> wrote:
>>>
>>>> It's OK to rely upon the requirements imposed by an implementation when
>>>> the C standard doesn't impose any - but when you do so, you need to make
>>>> sure you actually know what those requirements are.
>>>
>>> Exactly, and in this specific case, it's not worth the effort compared
>>> to writing a realloc wrapper that avoids the undefined behavior, while
>>> itself providing a C99 conforming one.
>>>
>>> I'm not going to use realloc(ptr, 0) and check everyone's documentation.
>>>
>>> And then what if I don't find it defined? The what? Back to the
>>> wrapper I could have just written in the first place.
>>
>> [...]
>>
>> I think I would have *liked* to see C23 drop the special permission
>> to return a null pointer for a requested size of zero. C11 says (and
>> this applies to malloc, realloc, and all other allocation functions):
>>
>> If the space cannot be allocated, a null pointer is returned. If
>> the size of the space requested is zero, the behavior is
>> implementation-defined: either a null pointer is returned, or
>> the behavior is as if the size were some nonzero value, except
>> that the returned pointer shall not be used to access an object.
>>
>> This could have been changed to:
>>
>> If the space cannot be allocated, a null pointer is returned. If
>> the size of the space requested is zero, the behavior is as
>> if the size were some nonzero value, except that the returned
>> pointer shall not be used to access an object.
>>
>> Any existing implementations that always return a null pointer
>> for malloc(0) would have to be updated. That shouldn't be a
>> great burden.
>>
>> Note that malloc(0) or realloc(ptr, 0) can still fail and return
>> a null pointer if no space can be allocated, so all allocations
>> should still be checked. But with this proposed change, code
>> could rely on realloc(ptr, 0) returning a non-null pointer *unless*
>> available memory is critically low -- pretty much the same as in C11,
>> except that a null pointer would be an indication that something
>> is seriously wrong.
>>
>> (I remember seeing a discussion about making the behavior of
>> realloc(ptr, 0) undefined. I'm making inquiries, and I'll follow
>> up if I learn anything relevant.)
>
> I got a response from JeanHeyd Meneide.
>
> If realloc(ptr, 0) returns a null pointer there's no way to tell whether
> allocation failed (and ptr has not been freed), or the implementation
> returns a null pointer for zero-sized allocations (and ptr has been
> freed). Some implementations set errno, but C doesn't require it.

It's trivial to fix that problem: simply require implementations
to define a preprocessor symbol about how the implementation
works. Problem solved.

(There are other instances of implementation-defined behavior
that would benefit from analogous changes along these lines.)

Re: *rubeyes*: realloc(ptr, 0) is UB?

<868r4igvn7.fsf@linuxsc.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49047&group=comp.lang.c#49047

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17687@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Sun, 21 Jan 2024 14:38:04 -0800
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <868r4igvn7.fsf@linuxsc.com>
References: <20240116162506.143@kylheku.com> <QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com> <9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com> <QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me> <20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me> <20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com> <87msszbb08.fsf@nosuchdomain.example.com> <uoi92j$2gjc$1@dont-email.me> <20240120211754.732@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="92215711b06d8c3aeb5738d6959250bf";
logging-data="429832"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/OLyNxszT76EL6sAqR3kqIyVUIaff6NDU="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:uJWVdy6SUmCafqs7xuOZ2tAAcss=
sha1:2WKj+q4Ws5x6pTp7UHz/3mzn7kc=
 by: Tim Rentsch - Sun, 21 Jan 2024 22:38 UTC

Kaz Kylheku <433-929-6894@kylheku.com> writes:

> On 2024-01-21, Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
>
>> On Sat, 20 Jan 2024 19:50:31 -0800, Keith Thompson wrote:
>>
>>> If realloc(ptr, 0) returns a null pointer there's no way to tell whether
>>> allocation failed (and ptr has not been freed) ...
>>
>> Actually, that doesn?t seem like a reasonable interpretation, because it
>> leads to memory leaks.
>
> That's not an "interpretation"; that's just a fact.
>
> A null return from realloc has two documented meanings; situations exist
> in which it cannot be distinguished which one, in any portable way.

I don't agree that they can't be distinguished in _any_ portable
way.

I suppose a case could be made that they can't be distinguished in
any _convenient_ portable way. But that's not the same as _any_
portable way.

Re: *rubeyes*: realloc(ptr, 0) is UB?

<87edeab85u.fsf@nosuchdomain.example.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49048&group=comp.lang.c#49048

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.network!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S.Thompson+u@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Sun, 21 Jan 2024 15:04:13 -0800
Organization: None to speak of
Lines: 88
Message-ID: <87edeab85u.fsf@nosuchdomain.example.com>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com>
<87r0iech8j.fsf@nosuchdomain.example.com>
<87msszbb08.fsf@nosuchdomain.example.com> <86cytugvve.fsf@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="9cd31264156fc70cf12828e27041ba36";
logging-data="438581"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Cf0w+8vakM0+1jTnJSyiZ"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:UHPUR7N8cmF94tm+cFhF+bEVjCs=
sha1:/itDTcR7yDOBNr5M0MSL2dTQh4U=
 by: Keith Thompson - Sun, 21 Jan 2024 23:04 UTC

Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
> Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
>> Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
>>> Kaz Kylheku <433-929-6894@kylheku.com> writes:
>>>> On 2024-01-18, James Kuyper <jameskuyper@alumni.caltech.edu> wrote:
>>>>> It's OK to rely upon the requirements imposed by an implementation when
>>>>> the C standard doesn't impose any - but when you do so, you need to make
>>>>> sure you actually know what those requirements are.
>>>>
>>>> Exactly, and in this specific case, it's not worth the effort compared
>>>> to writing a realloc wrapper that avoids the undefined behavior, while
>>>> itself providing a C99 conforming one.
>>>>
>>>> I'm not going to use realloc(ptr, 0) and check everyone's documentation.
>>>>
>>>> And then what if I don't find it defined? The what? Back to the
>>>> wrapper I could have just written in the first place.
>>>
>>> [...]
>>>
>>> I think I would have *liked* to see C23 drop the special permission
>>> to return a null pointer for a requested size of zero. C11 says (and
>>> this applies to malloc, realloc, and all other allocation functions):
>>>
>>> If the space cannot be allocated, a null pointer is returned. If
>>> the size of the space requested is zero, the behavior is
>>> implementation-defined: either a null pointer is returned, or
>>> the behavior is as if the size were some nonzero value, except
>>> that the returned pointer shall not be used to access an object.
>>>
>>> This could have been changed to:
>>>
>>> If the space cannot be allocated, a null pointer is returned. If
>>> the size of the space requested is zero, the behavior is as
>>> if the size were some nonzero value, except that the returned
>>> pointer shall not be used to access an object.
>>>
>>> Any existing implementations that always return a null pointer
>>> for malloc(0) would have to be updated. That shouldn't be a
>>> great burden.
>>>
>>> Note that malloc(0) or realloc(ptr, 0) can still fail and return
>>> a null pointer if no space can be allocated, so all allocations
>>> should still be checked. But with this proposed change, code
>>> could rely on realloc(ptr, 0) returning a non-null pointer *unless*
>>> available memory is critically low -- pretty much the same as in C11,
>>> except that a null pointer would be an indication that something
>>> is seriously wrong.
>>>
>>> (I remember seeing a discussion about making the behavior of
>>> realloc(ptr, 0) undefined. I'm making inquiries, and I'll follow
>>> up if I learn anything relevant.)
>>
>> I got a response from JeanHeyd Meneide.
>>
>> If realloc(ptr, 0) returns a null pointer there's no way to tell whether
>> allocation failed (and ptr has not been freed), or the implementation
>> returns a null pointer for zero-sized allocations (and ptr has been
>> freed). Some implementations set errno, but C doesn't require it.
>
> It's trivial to fix that problem: simply require implementations
> to define a preprocessor symbol about how the implementation
> works. Problem solved.
>
> (There are other instances of implementation-defined behavior
> that would benefit from analogous changes along these lines.)

I tend to agree that such a preprocessor symbol would be an improvement.

I still think, as I wrote above, that removing the permission to return
a null pointer on a successful zero-sized allocation would be a greater
improvement.

A preprocessor symbol makes it easier for programmers to work around the
potential difference between implementations. The change I advocate
would make it completely unnecessary.

Except, of course, that most code would still have to allow for pre-C26
behavior, even if the change were adopted in C26. That's unavoidable in
the absence of time machines. On the gripping hand, since it seems that
most existing implementations (well, all of the few I've tried) return a
non-null pointer for malloc(0), it might be reasonable to ignore the few
pre-C26 implementations that return a null pointer.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

Re: *rubeyes*: realloc(ptr, 0) is UB?

<uokaiv$dn54$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49049&group=comp.lang.c#49049

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: malcolm.arthur.mclean@gmail.com (Malcolm McLean)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Sun, 21 Jan 2024 23:49:50 +0000
Organization: A noiseless patient Spider
Lines: 63
Message-ID: <uokaiv$dn54$1@dont-email.me>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me>
<uojudd$bs5q$1@dont-email.me> <uojv5f$c17v$1@dont-email.me>
<uok0ah$c5dj$1@dont-email.me> <87il3mbcfy.fsf@nosuchdomain.example.com>
<20240121134843.67@kylheku.com> <uok4n9$ctgb$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 21 Jan 2024 23:49:51 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c3c331bde9e02405d7c9ccae0ea0f574";
logging-data="449700"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX195u0upoPo3Fn20P6VHbLBm1+2twv5g+3I="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:RYIyixardXujGa/tVbLXYEk2tik=
Content-Language: en-GB
In-Reply-To: <uok4n9$ctgb$1@dont-email.me>
 by: Malcolm McLean - Sun, 21 Jan 2024 23:49 UTC

On 21/01/2024 22:09, bart wrote:
> On 21/01/2024 21:49, Kaz Kylheku wrote:
>> On 2024-01-21, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>>> David Brown <david.brown@hesbynett.no> writes:
>>> [...]
>>>> Personally, I think it would be often more efficient in modern C if
>>>> the allocation system didn't track sizes at all, and "free" passed the
>>>> original size as a parameter.  But that ship sailed long ago for
>>>> standard C.  (C++ supports a sized deallocation system, and of course
>>>> there's nothing to stop you making your own allocator system for C.)
>>>
>>> I suspect that calling malloc() with one size and free() with a
>>> different one would have been a rich source of subtle bugs.
>>
>> Welcome to C23! free_sized(malloc(42), 73) -> UB.
>>
>
> I've used such a sized free function in my own libraries for a long time.
>
> Yes, you can pass the wrong size (although there are debugging options
> where it will keep track of the sizes and double-check, if you suspect
> such a bug).
>
> But the real problems are the same as they are now in C:
>
>    free(p)           when p has not been assigned a value from malloc
>    free(p); free(p); call twice
>    ---               forget to call free
>    free(q);          free the wrong pointer
>    free(p+1);        pass an invalid pointer
>
> Getting a p=malloc() correctly matched up with just one free(p) in a
> different part of the program, at a different time, is the hard bit.
>
> The easy bit is this:
>
>     p = malloc(sizeof(*p));
>     ...
>     free_sized(p, sizeof(*p));
>
> (Is it still malloc or is there a malloc_sized? Since part of the point
> is not wasting time and memory managing that extra metadata.)
>
The thing is you have to be disciplined. I have a fairly strict rule
that either alloaction and free must be in the same function, or if a
function returns a structure that needs to be operated on, there is a
constructor which sets it up in a valid state and a matching destructor
(I always use the prefix kill, though a colleague who had been in the
military objected because it reminded him too much of the army).

One difficulty is when you pass up an allocatred pointer which is not to
a custom structure, eg from strdup(). So you have to break the rule and
treat the strdup call as though it were a call to malloc. Then rarely
you have graph like structures which add and subtract nodes. So you
isolate the code that adds or subtracts the node, and set up the whole
thing in a valid (but maybe empty) state in one call, and destroy the
whole thing in another. But you can't keep to the rule that each
addition of a node will be matched by a freeing of the node in the same
function.
--
Check out Basic Algorithms and my other books:
https://www.lulu.com/spotlight/bgy1mm

Re: *rubeyes*: realloc(ptr, 0) is UB?

<uokeuk$e718$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49053&group=comp.lang.c#49053

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Sun, 21 Jan 2024 17:04:19 -0800
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <uokeuk$e718$1@dont-email.me>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me>
<uojudd$bs5q$1@dont-email.me> <uojv5f$c17v$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 22 Jan 2024 01:04:20 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f5efac9e35c211335209a589d0078cec";
logging-data="465960"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+s7uYV4LAAfdJ+NXW3QE78KXJO8kIilAI="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:QIjy8yipm0bQ4EBKiVRBo+71b4Y=
In-Reply-To: <uojv5f$c17v$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Mon, 22 Jan 2024 01:04 UTC

On 1/21/2024 12:34 PM, bart wrote:
> On 21/01/2024 20:22, Chris M. Thomasson wrote:
>> On 1/21/2024 3:55 AM, bart wrote:
>
>>> malloc has sort of created a rod for its own back by needing to store
>>> the size of the allocation. That will take up some space even when
>>> malloc(0) is called, if NULL is not being returned.
>> [...]
>>
>> Why would malloc need to store the size of its allocations?
>>
>
> If you do this:
>
>     p = malloc(N);
>     ...
>     free(p);
>
> 'free' will need to know what N was used to allocate p in order to
> deallocate right size of memory.
>

Why?

Re: *rubeyes*: realloc(ptr, 0) is UB?

<uokf0t$e718$2@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49054&group=comp.lang.c#49054

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.network!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Sun, 21 Jan 2024 17:05:32 -0800
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <uokf0t$e718$2@dont-email.me>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me>
<uojudd$bs5q$1@dont-email.me> <uojv5f$c17v$1@dont-email.me>
<uok0ah$c5dj$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 22 Jan 2024 01:05:33 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f5efac9e35c211335209a589d0078cec";
logging-data="465960"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+NhWjz+gE61fFIYswFBiPwKrQlCauFQrE="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:kKO5Bb9sVM7dr7p/Dq36qB9SuSg=
Content-Language: en-US
In-Reply-To: <uok0ah$c5dj$1@dont-email.me>
 by: Chris M. Thomasson - Mon, 22 Jan 2024 01:05 UTC

On 1/21/2024 12:54 PM, David Brown wrote:
> On 21/01/2024 21:34, bart wrote:
>> On 21/01/2024 20:22, Chris M. Thomasson wrote:
>>> On 1/21/2024 3:55 AM, bart wrote:
>>
>>>> malloc has sort of created a rod for its own back by needing to
>>>> store the size of the allocation. That will take up some space even
>>>> when malloc(0) is called, if NULL is not being returned.
>>> [...]
>>>
>>> Why would malloc need to store the size of its allocations?
>>>
>>
>> If you do this:
>>
>>      p = malloc(N);
>>      ...
>>      free(p);
>>
>> 'free' will need to know what N was used to allocate p in order to
>> deallocate right size of memory.
>>
>
> It does not need to store the size of the allocation.  It merely has to
> store sufficient information to be able to figure out how to deallocate
> properly.  And any storage it needs can be in a different place from the
> allocation.
>
> Some malloc/free systems track the information separately from the
> allocation data.  One possibility is to use pools for different memory
> sizes.  When the user asks for X bytes, this is rounded up to the
> nearest pool size - call it Y - and the allocation is made from the Y
> pool, which can be viewed as an array of Y-size lumps.  Only need one
> single bit to track each allocation, to say which indexes in the array
> are used.  There are many other ways to do it.
>
> Personally, I think it would be often more efficient in modern C if the
> allocation system didn't track sizes at all, and "free" passed the
> original size as a parameter.  But that ship sailed long ago for
> standard C.  (C++ supports a sized deallocation system, and of course
> there's nothing to stop you making your own allocator system for C.)
>

Think of a block of memory aligned on a very large boundary, and padded
up to a boundary size. So, we free take take its address, round down to
a boundary and get at a header...

Re: *rubeyes*: realloc(ptr, 0) is UB?

<uokf8l$e718$3@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49055&group=comp.lang.c#49055

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Sun, 21 Jan 2024 17:09:40 -0800
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <uokf8l$e718$3@dont-email.me>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me>
<uojudd$bs5q$1@dont-email.me> <uojv5f$c17v$1@dont-email.me>
<uok0ah$c5dj$1@dont-email.me> <uokf0t$e718$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 22 Jan 2024 01:09:41 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f5efac9e35c211335209a589d0078cec";
logging-data="465960"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18jfJHtmUy3V84rF7akxejwLTQrKfPkKdo="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:9C7Q9Sxy+PuaLdd9oWNpEUs7BZM=
In-Reply-To: <uokf0t$e718$2@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Mon, 22 Jan 2024 01:09 UTC

On 1/21/2024 5:05 PM, Chris M. Thomasson wrote:
[...]
> Think of a block of memory aligned on a very large boundary, and padded
> up to a boundary size. So, we free take take its address, round down to
> a boundary and get at a header...

God damn typos!

free takes its address, rounds down to a boundary, and can get at a
header structure.

This works fine if we make sure if we take alignment and a large
boundary into account. Basically, we need each allocation to be at least
be as large as a word/pointer if you will. This pointer is used for a
linked list in the header, that sits on alignment boundaries.

Re: *rubeyes*: realloc(ptr, 0) is UB?

<uokfhc$e718$4@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49056&group=comp.lang.c#49056

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Sun, 21 Jan 2024 17:14:20 -0800
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <uokfhc$e718$4@dont-email.me>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me>
<uojudd$bs5q$1@dont-email.me> <uojv5f$c17v$1@dont-email.me>
<20240121131413.756@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 22 Jan 2024 01:14:21 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f5efac9e35c211335209a589d0078cec";
logging-data="465960"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/hiNg95yy7+EuYSMersgXEjFeQ1YWf95g="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:YhvmWTwXawfy/mu1D+6KY2ah5xM=
In-Reply-To: <20240121131413.756@kylheku.com>
Content-Language: en-US
 by: Chris M. Thomasson - Mon, 22 Jan 2024 01:14 UTC

On 1/21/2024 1:16 PM, Kaz Kylheku wrote:
> On 2024-01-21, bart <bc@freeuk.com> wrote:
>> On 21/01/2024 20:22, Chris M. Thomasson wrote:
>>> On 1/21/2024 3:55 AM, bart wrote:
>>
>>>> malloc has sort of created a rod for its own back by needing to store
>>>> the size of the allocation. That will take up some space even when
>>>> malloc(0) is called, if NULL is not being returned.
>>> [...]
>>>
>>> Why would malloc need to store the size of its allocations?
>>>
>>
>> If you do this:
>>
>> p = malloc(N);
>> ...
>> free(p);
>>
>> 'free' will need to know what N was used to allocate p in order to
>> deallocate right size of memory.
>
> While that is true in the abstract, it is not necesarily the case
> that it needs to pull N out of the p object.
>
> For instance, suppose N is a 64 byte allocation, and the allocator
> has special heaps for small allocations.
>
> It can figure out that p points into a heap that has 64 byte objects
> and then do some pointer arithmetic to figure out which one,
> and add that to a free list or bitmap or whatever.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Right!

Re: *rubeyes*: realloc(ptr, 0) is UB?

<uokigg$ekan$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49059&group=comp.lang.c#49059

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: malcolm.arthur.mclean@gmail.com (Malcolm McLean)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 02:05:04 +0000
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <uokigg$ekan$1@dont-email.me>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me>
<uojudd$bs5q$1@dont-email.me> <uojv5f$c17v$1@dont-email.me>
<uokeuk$e718$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 22 Jan 2024 02:05:04 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e6bb492280758c2bd2c6011639f214f0";
logging-data="479575"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+EJS6bM672xeKI+z59OLiebWtPKUnh3MU="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:lJcNrdun6iW08oySHT3Wt2PIc7E=
Content-Language: en-GB
In-Reply-To: <uokeuk$e718$1@dont-email.me>
 by: Malcolm McLean - Mon, 22 Jan 2024 02:05 UTC

On 22/01/2024 01:04, Chris M. Thomasson wrote:
> On 1/21/2024 12:34 PM, bart wrote:
>> On 21/01/2024 20:22, Chris M. Thomasson wrote:
>>> On 1/21/2024 3:55 AM, bart wrote:
>>
>>>> malloc has sort of created a rod for its own back by needing to
>>>> store the size of the allocation. That will take up some space even
>>>> when malloc(0) is called, if NULL is not being returned.
>>> [...]
>>>
>>> Why would malloc need to store the size of its allocations?
>>>
>>
>> If you do this:
>>
>>      p = malloc(N);
>>      ...
>>      free(p);
>>
>> 'free' will need to know what N was used to allocate p in order to
>> deallocate right size of memory.
>>
>
> Why?
It depends how malloc is implemented.
But the traditional way, using a contiguous pool and a linked list of
free blocks, each allocation is for a fixed number of units the same
size as the header, and you need to know how many units are in the block
to add it to the free list on deallocation, and also for
defragmentation. So the header is a pointer to the next free block plus
a size.
--
Check out Basic Algorithms and my other books:
https://www.lulu.com/spotlight/bgy1mm

Re: *rubeyes*: realloc(ptr, 0) is UB?

<uokkcg$erqf$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49060&group=comp.lang.c#49060

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Sun, 21 Jan 2024 18:37:03 -0800
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <uokkcg$erqf$1@dont-email.me>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me>
<uojudd$bs5q$1@dont-email.me> <uojv5f$c17v$1@dont-email.me>
<uokeuk$e718$1@dont-email.me> <uokigg$ekan$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 22 Jan 2024 02:37:04 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f5efac9e35c211335209a589d0078cec";
logging-data="487247"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX184mGZNCVeUDkcpH0I2AAzC/nvBlXi14HA="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:yLcitK2SJoKYzjio4RwRJn+9UB8=
In-Reply-To: <uokigg$ekan$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Mon, 22 Jan 2024 02:37 UTC

On 1/21/2024 6:05 PM, Malcolm McLean wrote:
> On 22/01/2024 01:04, Chris M. Thomasson wrote:
>> On 1/21/2024 12:34 PM, bart wrote:
>>> On 21/01/2024 20:22, Chris M. Thomasson wrote:
>>>> On 1/21/2024 3:55 AM, bart wrote:
>>>
>>>>> malloc has sort of created a rod for its own back by needing to
>>>>> store the size of the allocation. That will take up some space even
>>>>> when malloc(0) is called, if NULL is not being returned.
>>>> [...]
>>>>
>>>> Why would malloc need to store the size of its allocations?
>>>>
>>>
>>> If you do this:
>>>
>>>      p = malloc(N);
>>>      ...
>>>      free(p);
>>>
>>> 'free' will need to know what N was used to allocate p in order to
>>> deallocate right size of memory.
>>>
>>
>> Why?
> It depends how malloc is implemented.
> But the traditional way, using a contiguous pool and a linked list of
> free blocks, each allocation is for a fixed number of units the same
> size as the header, and you need to know how many units are in the block
> to add it to the free list on deallocation, and also for
> defragmentation. So the header is a pointer to the next free block plus
> a size.

Think of free rounding a block down to a large boundary, where access to
a header is right there...

Re: *rubeyes*: realloc(ptr, 0) is UB?

<wwvv87lwzuo.fsf@LkoBDZeT.terraraq.uk>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49067&group=comp.lang.c#49067

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!nntp-feed.chiark.greenend.org.uk!ewrotcd!nntp.terraraq.uk!.POSTED.tunnel.sfere.anjou.terraraq.org.uk!not-for-mail
From: invalid@invalid.invalid (Richard Kettlewell)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 08:12:47 +0000
Organization: terraraq NNTP server
Message-ID: <wwvv87lwzuo.fsf@LkoBDZeT.terraraq.uk>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com>
<87r0iech8j.fsf@nosuchdomain.example.com>
<87msszbb08.fsf@nosuchdomain.example.com> <86cytugvve.fsf@linuxsc.com>
<87edeab85u.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: innmantic.terraraq.uk; posting-host="tunnel.sfere.anjou.terraraq.org.uk:172.17.207.6";
logging-data="7829"; mail-complaints-to="usenet@innmantic.terraraq.uk"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:pt0SGF7PhLUcQWBzghc0TOeq7Tg=
X-Face: h[Hh-7npe<<b4/eW[]sat,I3O`t8A`(ej.H!F4\8|;ih)`7{@:A~/j1}gTt4e7-n*F?.Rl^
F<\{jehn7.KrO{!7=:(@J~]<.[{>v9!1<qZY,{EJxg6?Er4Y7Ng2\Ft>Z&W?r\c.!4DXH5PWpga"ha
+r0NzP?vnz:e/knOY)PI-
X-Boydie: NO
 by: Richard Kettlewell - Mon, 22 Jan 2024 08:12 UTC

Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>> It's trivial to fix that problem: simply require implementations
>> to define a preprocessor symbol about how the implementation
>> works. Problem solved.
>>
>> (There are other instances of implementation-defined behavior
>> that would benefit from analogous changes along these lines.)
>
> I tend to agree that such a preprocessor symbol would be an improvement.
>
> I still think, as I wrote above, that removing the permission to
> return a null pointer on a successful zero-sized allocation would be a
> greater improvement.

Fully agreed. That permission has been grit in the gears for a very long
time, for much of which I had the misfortune of having to deal with it
in real life thanks to IBM’s bad decisions.

Fixing it would have been, what, a 2-line change to the impacted
implementations, but apparently it’s better for all the users to deal
with the consequences instead.

> A preprocessor symbol makes it easier for programmers to work around the
> potential difference between implementations. The change I advocate
> would make it completely unnecessary.
>
> Except, of course, that most code would still have to allow for pre-C26
> behavior, even if the change were adopted in C26. That's unavoidable in
> the absence of time machines. On the gripping hand, since it seems that
> most existing implementations (well, all of the few I've tried) return a
> non-null pointer for malloc(0), it might be reasonable to ignore the few
> pre-C26 implementations that return a null pointer.

“Reasonable” isn’t really relevant (at least in my working environment):
either my code has to run on such implementations or it doesn’t.

--
https://www.greenend.org.uk/rjk/

Re: *rubeyes*: realloc(ptr, 0) is UB?

<uol8gb$l82b$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49068&group=comp.lang.c#49068

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!nntp.comgw.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.brown@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 09:20:27 +0100
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <uol8gb$l82b$1@dont-email.me>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me>
<uojudd$bs5q$1@dont-email.me> <uojv5f$c17v$1@dont-email.me>
<uok0ah$c5dj$1@dont-email.me> <20240121131638.975@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 22 Jan 2024 08:20:27 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="773b575d0a21626e330ecd5dcb685706";
logging-data="696395"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+NUf+4BCHA165YzUBuaHbYbguVMQ+m97U="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:nPP9HERE6HglAJoiigElT1UrLTw=
In-Reply-To: <20240121131638.975@kylheku.com>
Content-Language: en-GB
 by: David Brown - Mon, 22 Jan 2024 08:20 UTC

On 21/01/2024 22:20, Kaz Kylheku wrote:
> On 2024-01-21, David Brown <david.brown@hesbynett.no> wrote:
>> Personally, I think it would be often more efficient in modern C if the
>> allocation system didn't track sizes at all, and "free" passed the
>> original size as a parameter. But that ship sailed long ago for
>> standard C.
>
> That ship newly sailed in 2023.
>
> The N3096 draft describes a function free_sized that takes a size. If
> the size is wrong, the behavior is undefined.
>

I hadn't noticed that addition in C23 - I'll look it up.

> So now C has two ways to free an object: an efficient one where
> the program helps by giving the size, and the old free, which
> may have to do more work.

It would be a lot more efficient if the sized free was matched with an
appropriate malloc - if malloc() still has to track the size somewhere
in case the user calls free() instead of free_sized(), the gains are
much less.

>
> I think going forward, it may start to become wise to detect the
> implementation's support for free_sized (e.g. in a configure script)
> and use that as much as possible, using free only when it's
> inconvenient: for instance a function resembling POSIX strdup
> might not be able to to safely assume that it can do
> free_sized(str, strlen(str)+1), because the underlying buffer
> might be larger.
>

Re: *rubeyes*: realloc(ptr, 0) is UB?

<uol8nr$l82b$2@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49069&group=comp.lang.c#49069

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.niel.me!news.gegeweb.eu!gegeweb.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.brown@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 09:24:26 +0100
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <uol8nr$l82b$2@dont-email.me>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me>
<uojudd$bs5q$1@dont-email.me> <uojv5f$c17v$1@dont-email.me>
<uok0ah$c5dj$1@dont-email.me> <87il3mbcfy.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 22 Jan 2024 08:24:27 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="773b575d0a21626e330ecd5dcb685706";
logging-data="696395"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+/8AH4cRs8CaIMlptaxae4CrCrhWXwnh0="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:CWhvadx/ke4H5mvOUcn7u/bnRtQ=
Content-Language: en-GB
In-Reply-To: <87il3mbcfy.fsf@nosuchdomain.example.com>
 by: David Brown - Mon, 22 Jan 2024 08:24 UTC

On 21/01/2024 22:31, Keith Thompson wrote:
> David Brown <david.brown@hesbynett.no> writes:
> [...]
>> Personally, I think it would be often more efficient in modern C if
>> the allocation system didn't track sizes at all, and "free" passed the
>> original size as a parameter. But that ship sailed long ago for
>> standard C. (C++ supports a sized deallocation system, and of course
>> there's nothing to stop you making your own allocator system for C.)
>
> I suspect that calling malloc() with one size and free() with a
> different one would have been a rich source of subtle bugs.
>

Sure. But that's part of the fun of C :-)

Many calls to malloc are of the form :

p = malloc(sizeof(*p));

so freeing them with :

free_sized(p, sizeof(*p));

should not be a significant risk.

There are circumstances where you'd have to track the size as well, and
there is then plenty of scope for mistakes.

Re: *rubeyes*: realloc(ptr, 0) is UB?

<wwvh6j5mzxv.fsf@LkoBDZeT.terraraq.uk>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49071&group=comp.lang.c#49071

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!news.gegeweb.eu!gegeweb.org!nntp.terraraq.uk!.POSTED.tunnel.sfere.anjou.terraraq.org.uk!not-for-mail
From: invalid@invalid.invalid (Richard Kettlewell)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 10:21:00 +0000
Organization: terraraq NNTP server
Message-ID: <wwvh6j5mzxv.fsf@LkoBDZeT.terraraq.uk>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com>
<87r0iech8j.fsf@nosuchdomain.example.com>
<20240118185544.347@kylheku.com> <865xzmj4bv.fsf@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: innmantic.terraraq.uk; posting-host="tunnel.sfere.anjou.terraraq.org.uk:172.17.207.6";
logging-data="9635"; mail-complaints-to="usenet@innmantic.terraraq.uk"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:kGeF34uM5JlkbWNOJ8Jt9ot/Cgw=
X-Face: h[Hh-7npe<<b4/eW[]sat,I3O`t8A`(ej.H!F4\8|;ih)`7{@:A~/j1}gTt4e7-n*F?.Rl^
F<\{jehn7.KrO{!7=:(@J~]<.[{>v9!1<qZY,{EJxg6?Er4Y7Ng2\Ft>Z&W?r\c.!4DXH5PWpga"ha
+r0NzP?vnz:e/knOY)PI-
X-Boydie: NO
 by: Richard Kettlewell - Mon, 22 Jan 2024 10:21 UTC

Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
> That's a half-assed argument. There are other ways a pointer might
> have a null value than just being the result of a call to malloc().
> If code might call memset() et al with a zero size and a null
> pointer, it's better to address all possible cases at once rather
> than just some of them:
>
> static inline void *
> safer_memset( void *s, int c, size_t n ){
> return n ? memset( s, c, n ) : s;
> }
>
> static inline void *
> safer_memcpy( void *d, const void *s, size_t n ){
> return n ? memcpy( d, s, n ) : d;
> }
>
> /* ... etc ... */

Of course that’s what the cautious programmer must do practice. But in
terms of the total cost (over all users, implementers, etc) fixing the
definitions of memcpy/memset/etc (as well as malloc) would have been the
better answer.

Standard C is trying to have its own cake and eat it here: 0-sized
allocations can be represented by null pointers when it’s malloc, but
not when it’s memcpy.

--
https://www.greenend.org.uk/rjk/

Re: *rubeyes*: realloc(ptr, 0) is UB?

<wwvbk9dmzvk.fsf@LkoBDZeT.terraraq.uk>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49072&group=comp.lang.c#49072

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!feeder8.news.weretis.net!news.mb-net.net!open-news-network.org!news.gegeweb.eu!gegeweb.org!nntp.terraraq.uk!.POSTED.tunnel.sfere.anjou.terraraq.org.uk!not-for-mail
From: invalid@invalid.invalid (Richard Kettlewell)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 10:22:23 +0000
Organization: terraraq NNTP server
Message-ID: <wwvbk9dmzvk.fsf@LkoBDZeT.terraraq.uk>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com>
<87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me>
<877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: innmantic.terraraq.uk; posting-host="tunnel.sfere.anjou.terraraq.org.uk:172.17.207.6";
logging-data="9635"; mail-complaints-to="usenet@innmantic.terraraq.uk"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:UmXUd5RiqjBtvaQImpPKQ5cXklo=
X-Face: h[Hh-7npe<<b4/eW[]sat,I3O`t8A`(ej.H!F4\8|;ih)`7{@:A~/j1}gTt4e7-n*F?.Rl^
F<\{jehn7.KrO{!7=:(@J~]<.[{>v9!1<qZY,{EJxg6?Er4Y7Ng2\Ft>Z&W?r\c.!4DXH5PWpga"ha
+r0NzP?vnz:e/knOY)PI-
X-Boydie: NO
 by: Richard Kettlewell - Mon, 22 Jan 2024 10:22 UTC

bart <bc@freeuk.com> writes:
> malloc has sort of created a rod for its own back by needing to store
> the size of the allocation. That will take up some space even when
> malloc(0) is called, if NULL is not being returned.

The alternative is making a rod for the back of every single caller
(i.e. all consumers must track allocation sizes). I think the design of
malloc got this one right.

--
https://www.greenend.org.uk/rjk/

Re: *rubeyes*: realloc(ptr, 0) is UB?

<uoljea$n2pc$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49073&group=comp.lang.c#49073

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc@freeuk.com (bart)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 11:27:06 +0000
Organization: A noiseless patient Spider
Lines: 90
Message-ID: <uoljea$n2pc$1@dont-email.me>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me>
<wwvbk9dmzvk.fsf@LkoBDZeT.terraraq.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 22 Jan 2024 11:27:06 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e1e6f23a5b67b4655f7a372c9f27f255";
logging-data="756524"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+e0fV74ArH+RfPl+59Z/AhcV4sfgVSGp8="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:b8pX6j+IC8SeUcabCqfOLnkLCUI=
In-Reply-To: <wwvbk9dmzvk.fsf@LkoBDZeT.terraraq.uk>
Content-Language: en-GB
 by: bart - Mon, 22 Jan 2024 11:27 UTC

On 22/01/2024 10:22, Richard Kettlewell wrote:
> bart <bc@freeuk.com> writes:
>> malloc has sort of created a rod for its own back by needing to store
>> the size of the allocation. That will take up some space even when
>> malloc(0) is called, if NULL is not being returned.
>
> The alternative is making a rod for the back of every single caller
> (i.e. all consumers must track allocation sizes).

Not at all. Let's first emulate a pair of functions where the caller is
expected to remember the size:

void* malloc_s (size_t n) {return malloc(n);}
void free_s (void* p, size_t n) {free(p);}

Then a typical alloc/dealloc sequence might look like this:

typedef struct {int d,m,y;}Date;
Date* p;

p=malloc_s(sizeof(*p));
...
free_s(p, sizeof(*p));

Is that particularly onerous? If you have a fixed-size object like a
struct, then you will always know its size.

For variable-sized objects, then yes you need to keep a record of the
size, but the chances are you have to do that anyway. For example to be
able to iterate over that dynamic array.

But if you really wanted (for example when allocating variable length,
zero-terminated strings), you can write a couple of wrappers around
malloc_s and free_s to emulate what malloc and free provide in terms of
not needing to remember the allocation size:

typedef long long int i64;

void* malloc2(i64 n) {
void* p;

p=malloc_s(n+sizeof(i64));
if (p==NULL) return NULL;
*((i64*)p) = n;
return (char*)p+sizeof(i64);
}

void free2(void* p) {
i64* q = (i64*)((char*)p-sizeof(i64));
i64 size = *q;

free_s(q, *q);
}

Untested code, it's to demonstrate what's involved: you ask for an
allocation 8 bytes bigger, use the 8 bytes at the beginning to store the
size, and return a pointer to just after those 8 bytes. (I'm assuming
8-byte alignment will suffice.)

Now you can do this:

p=malloc2(sizeof(*p));
...
free2(p);

> I think the design of malloc got this one right.

I think it got it wrong. Now everyone is lumbered with an allocation
scheme that will ALWAYS have to store the size of that struct, perhaps
taking as much space as the struct itself.

Imagine allocating 100M of those structs, and also storing 100M copies
of sizeof(Date) or whatever metadata is needed.

Getting around that, by writing your own small-object allocator on top
of malloc, is a lot harder that adding your own size-tracking on top of
a malloc that does not store any extra data. As I've shown.

(This is also a scheme where, if a user needs to get the size of an
allocation block, it can do so:

i64 size_s(void* p) {
i64* q = (i64*)((char*)p-sizeof(i64));
return *q;
}

But this will be the requested size not the capacity of the allocated
block. That would depend on how malloc_s/free_s are implemented.)

Re: *rubeyes*: realloc(ptr, 0) is UB?

<uoll3q$nbib$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49074&group=comp.lang.c#49074

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.bbs.nz!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!news.in-chemnitz.de!news2.arglkargh.de!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc@freeuk.com (bart)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 11:55:38 +0000
Organization: A noiseless patient Spider
Lines: 93
Message-ID: <uoll3q$nbib$1@dont-email.me>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me>
<uojudd$bs5q$1@dont-email.me> <uojv5f$c17v$1@dont-email.me>
<uokeuk$e718$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 22 Jan 2024 11:55:38 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e1e6f23a5b67b4655f7a372c9f27f255";
logging-data="765515"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/hb2maitbG437mdX8LFFxw1+fgV1rsbMU="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:BkT968Qza7Md0QgunjYKPPRNpjI=
Content-Language: en-GB
In-Reply-To: <uokeuk$e718$1@dont-email.me>
 by: bart - Mon, 22 Jan 2024 11:55 UTC

On 22/01/2024 01:04, Chris M. Thomasson wrote:
> On 1/21/2024 12:34 PM, bart wrote:
>> On 21/01/2024 20:22, Chris M. Thomasson wrote:
>>> On 1/21/2024 3:55 AM, bart wrote:
>>
>>>> malloc has sort of created a rod for its own back by needing to
>>>> store the size of the allocation. That will take up some space even
>>>> when malloc(0) is called, if NULL is not being returned.
>>> [...]
>>>
>>> Why would malloc need to store the size of its allocations?
>>>
>>
>> If you do this:
>>
>>      p = malloc(N);
>>      ...
>>      free(p);
>>
>> 'free' will need to know what N was used to allocate p in order to
>> deallocate right size of memory.
>>
>
> Why?

Try writing an allocator with zero memory overheads that doesn't spend
all its time on trying to deduce the extend of the block being allocated.

Apparently, 'malloc' on my machines doesn't know about such techniques,
because if I run this program:

enum {n=1024};
char* lastp = malloc(n);
char* p;

for(int i=0; i<10; ++i) {
p=malloc(n);
printf("%p %d\n", p, p-lastp);
lastp=p;
}

Then on Windows I might get:

0000026d06d15b30 18288
0000026d06d15f40 1040
0000026d06d16350 1040
0000026d06d16760 1040
0000026d06d16b70 1040
0000026d06d16f80 1040
0000026d06d17390 1040
0000026d06d177a0 1040
0000026d06d17bb0 1040
0000026d06d17fd0 1056

Some odd results, but generally it uses 16 bytes more than 1024. If I
run it under WSL, the same thing. Also on rextester.com.

You're saying it should be only 1024 bytes that are occupied? OK, tell
the authors of these various mallocs that.

BTW if I run this program (in my language which uses an allocator that
requires a size to free):

const n=1024
ref byte p:=pcm_alloc(n), lastp

to 10 do
p:=pcm_alloc(n)
println p, p-lastp
lastp:=p
od

I get these results:

02E2D440 1024
02E2D840 1024
02E2DC40 1024
02E2E040 1024
02E2E440 1024
02E2E840 1024
02E2EC40 1024
02E2F040 1024
02E2F440 1024
02E2F840 1024

Unless I use a bigger N, since then it switches to using malloc, and I
get similar results to above. But the overheads then are less significant.

Re: *rubeyes*: realloc(ptr, 0) is UB?

<uolldi$nbib$2@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49075&group=comp.lang.c#49075

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc@freeuk.com (bart)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 12:00:51 +0000
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <uolldi$nbib$2@dont-email.me>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me>
<uojudd$bs5q$1@dont-email.me> <uojv5f$c17v$1@dont-email.me>
<uokeuk$e718$1@dont-email.me> <uoll3q$nbib$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 22 Jan 2024 12:00:50 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e1e6f23a5b67b4655f7a372c9f27f255";
logging-data="765515"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+r9G1Vjqs1Miv1/h/gXCDdrxDp66qCkuU="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:I2Rd4qDYqj1Si+nprvJ9hGFWBEY=
Content-Language: en-GB
In-Reply-To: <uoll3q$nbib$1@dont-email.me>
 by: bart - Mon, 22 Jan 2024 12:00 UTC

On 22/01/2024 11:55, bart wrote:

>
> BTW if I run this program (in my language which uses an allocator that
> requires a size to free):
>
>     const n=1024
>     ref byte p:=pcm_alloc(n), lastp

(p and lastp are declared in the wrong order. The results shown are from
the fixed version.)

> I get these results:
>
> 02E2D440 1024
> 02E2D840 1024
....

(If I compile to an .obj file, which enables high-memory code, and link
using gcc, then it will show the bigger addresses like the C, but still
with 1024-byte allocations.)

Re: *rubeyes*: realloc(ptr, 0) is UB?

<uolngi$nke1$2@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49076&group=comp.lang.c#49076

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: malcolm.arthur.mclean@gmail.com (Malcolm McLean)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 12:36:34 +0000
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <uolngi$nke1$2@dont-email.me>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <861qaaj3k9.fsf@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 22 Jan 2024 12:36:34 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e6bb492280758c2bd2c6011639f214f0";
logging-data="774593"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+kYFTEPXTjsWnfpoVS2TfJk/wwdl5Sb/s="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:5urqNSInCxq3AzMFsuXmAnZvgOw=
In-Reply-To: <861qaaj3k9.fsf@linuxsc.com>
Content-Language: en-GB
 by: Malcolm McLean - Mon, 22 Jan 2024 12:36 UTC

On 21/01/2024 12:04, Tim Rentsch wrote:
> Kaz Kylheku <433-929-6894@kylheku.com> writes:
>
> [...]
>
>> Not requiring the non-null return from malloc(0) to be distinct
>> from previous malloc(0) return values (whether they were freed or not),
>> could help to "sell" the idea of taking away the null return value.
>>
>> Some implementors might grumble that null return allowed malloc(0) to be
>> efficient by not allocating anything. If they were allowed to return
>> (void *) -1 or something, that would placate that concern. [...]
>
> You have the tail wagging the dog here. If the results of
> different malloc(0) calls don't need to be distinguishable,
> they might just as well all be null.
No, because it's natural to write

employees = malloc(Nemployees * sizeof(EMPLOYEE));
if (!employees)
goto out_of_memory;

You don't want to have to special case Nemployees == 0.

--
Check out Basic Algorithms and my other books:
https://www.lulu.com/spotlight/bgy1mm

Re: *rubeyes*: realloc(ptr, 0) is UB?

<8734upbbf1.fsf@nosuchdomain.example.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49079&group=comp.lang.c#49079

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.furie.org.uk!usenet.goja.nl.eu.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S.Thompson+u@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 08:06:10 -0800
Organization: None to speak of
Lines: 28
Message-ID: <8734upbbf1.fsf@nosuchdomain.example.com>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com>
<87r0iech8j.fsf@nosuchdomain.example.com>
<87msszbb08.fsf@nosuchdomain.example.com> <86cytugvve.fsf@linuxsc.com>
<87edeab85u.fsf@nosuchdomain.example.com>
<wwvv87lwzuo.fsf@LkoBDZeT.terraraq.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: dont-email.me; posting-host="9cd31264156fc70cf12828e27041ba36";
logging-data="845347"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19O8/OziSqj4YuBqWDvE7E5"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:J15maQjYhcqhn51C/tcY8S3O9oc=
sha1:/w6pV1xlcsIeWzNmw0oq6gfo6Rk=
 by: Keith Thompson - Mon, 22 Jan 2024 16:06 UTC

Richard Kettlewell <invalid@invalid.invalid> writes:
> Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>>> It's trivial to fix that problem: simply require implementations
>>> to define a preprocessor symbol about how the implementation
>>> works. Problem solved.
>>>
>>> (There are other instances of implementation-defined behavior
>>> that would benefit from analogous changes along these lines.)
>>
>> I tend to agree that such a preprocessor symbol would be an improvement.
>>
>> I still think, as I wrote above, that removing the permission to
>> return a null pointer on a successful zero-sized allocation would be a
>> greater improvement.
>
> Fully agreed. That permission has been grit in the gears for a very long
> time, for much of which I had the misfortune of having to deal with it
> in real life thanks to IBM’s bad decisions.

Can you expand on "IBM's bad decisions"?

[...]

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

Re: *rubeyes*: realloc(ptr, 0) is UB?

<87y1ch9w1q.fsf@nosuchdomain.example.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49080&group=comp.lang.c#49080

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S.Thompson+u@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 08:23:29 -0800
Organization: None to speak of
Lines: 42
Message-ID: <87y1ch9w1q.fsf@nosuchdomain.example.com>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com>
<87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me>
<877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <861qaaj3k9.fsf@linuxsc.com>
<uolngi$nke1$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="9cd31264156fc70cf12828e27041ba36";
logging-data="845347"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19FXCuHdCkktcIkVnYzaBRq"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:56dy6puI0gVCGYm2IzzXWh2WQC8=
sha1:MAw1jmvPxBrFK4DCe+to/KEbJvw=
 by: Keith Thompson - Mon, 22 Jan 2024 16:23 UTC

Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:
> On 21/01/2024 12:04, Tim Rentsch wrote:
>> Kaz Kylheku <433-929-6894@kylheku.com> writes:
>> [...]
>>
>>> Not requiring the non-null return from malloc(0) to be distinct
>>> from previous malloc(0) return values (whether they were freed or not),
>>> could help to "sell" the idea of taking away the null return value.
>>>
>>> Some implementors might grumble that null return allowed malloc(0) to be
>>> efficient by not allocating anything. If they were allowed to return
>>> (void *) -1 or something, that would placate that concern. [...]
>> You have the tail wagging the dog here. If the results of
>> different malloc(0) calls don't need to be distinguishable,
>> they might just as well all be null.
> No, because it's natural to write
>
> employees = malloc(Nemployees * sizeof(EMPLOYEE));
> if (!employees)
> goto out_of_memory;
>
> You don't want to have to special case Nemployees == 0.

There are three scenarios being considered.

1. malloc(0) always returns a null pointer.
2. All malloc(0) calls return the same non-null pointer, perhaps to a
single pre-allocated object.
3. malloc(0) acts like malloc(1), returning a null pointer only if
memory has been exhausted.

Implementations currently choose between 1 and 3; 2 would be
non-conforming. Kaz suggested allowing (or requiring?) 2. (I advocate
requiring 3, which is the current behavior of all the implementations
I've tried.)

Tim is talking about scenario 2, wh

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

Re: *rubeyes*: realloc(ptr, 0) is UB?

<87ttn59vvc.fsf@nosuchdomain.example.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49082&group=comp.lang.c#49082

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S.Thompson+u@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 08:27:19 -0800
Organization: None to speak of
Lines: 46
Message-ID: <87ttn59vvc.fsf@nosuchdomain.example.com>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com>
<87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me>
<877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <861qaaj3k9.fsf@linuxsc.com>
<uolngi$nke1$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="9cd31264156fc70cf12828e27041ba36";
logging-data="845347"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+4kJHLY0CV4WVUcwNo8ByD"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:eQFLos2uwXv+J6dMIWPlSXRpZKM=
sha1:d8e9UQqBrdB+E0sDK+ycEWf4nsI=
 by: Keith Thompson - Mon, 22 Jan 2024 16:27 UTC

Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:
> On 21/01/2024 12:04, Tim Rentsch wrote:
>> Kaz Kylheku <433-929-6894@kylheku.com> writes:
>> [...]
>>
>>> Not requiring the non-null return from malloc(0) to be distinct
>>> from previous malloc(0) return values (whether they were freed or not),
>>> could help to "sell" the idea of taking away the null return value.
>>>
>>> Some implementors might grumble that null return allowed malloc(0) to be
>>> efficient by not allocating anything. If they were allowed to return
>>> (void *) -1 or something, that would placate that concern. [...]
>> You have the tail wagging the dog here. If the results of
>> different malloc(0) calls don't need to be distinguishable,
>> they might just as well all be null.
> No, because it's natural to write
>
> employees = malloc(Nemployees * sizeof(EMPLOYEE));
> if (!employees)
> goto out_of_memory;
>
> You don't want to have to special case Nemployees == 0.

[Ignore my previous followup. I accidentally posted it before I
finished writing it.]

There are three scenarios being considered.

1. malloc(0) always returns a null pointer.
2. All malloc(0) calls return the same non-null pointer, perhaps to a
single pre-allocated object.
3. malloc(0) acts like malloc(1), returning a null pointer only if
memory has been exhausted.

Implementations currently choose between 1 and 3; 2 would be
non-conforming. Kaz suggested allowing (or requiring?) 2. (I advocate
requiring 3, which is the current behavior of all the implementations
I've tried.)

Tim is talking about scenario 2. Your code sample would work correctly
in that scenario.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

Re: *rubeyes*: realloc(ptr, 0) is UB?

<POwrN.323725$p%Mb.106787@fx15.iad>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49083&group=comp.lang.c#49083

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx15.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: scott@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Newsgroups: comp.lang.c
References: <20240116162506.143@kylheku.com> <9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com> <QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me> <20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me> <20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com> <uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com> <20240119134055.699@kylheku.com> <861qaaj3k9.fsf@linuxsc.com> <uolngi$nke1$2@dont-email.me>
Lines: 26
Message-ID: <POwrN.323725$p%Mb.106787@fx15.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Mon, 22 Jan 2024 16:34:55 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Mon, 22 Jan 2024 16:34:55 GMT
X-Received-Bytes: 2120
 by: Scott Lurndal - Mon, 22 Jan 2024 16:34 UTC

Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:
>On 21/01/2024 12:04, Tim Rentsch wrote:
>> Kaz Kylheku <433-929-6894@kylheku.com> writes:
>>
>> [...]
>>
>>> Not requiring the non-null return from malloc(0) to be distinct
>>> from previous malloc(0) return values (whether they were freed or not),
>>> could help to "sell" the idea of taking away the null return value.
>>>
>>> Some implementors might grumble that null return allowed malloc(0) to be
>>> efficient by not allocating anything. If they were allowed to return
>>> (void *) -1 or something, that would placate that concern. [...]
>>
>> You have the tail wagging the dog here. If the results of
>> different malloc(0) calls don't need to be distinguishable,
>> they might just as well all be null.
>No, because it's natural to write
>
>employees = malloc(Nemployees * sizeof(EMPLOYEE));
>if (!employees)
> goto out_of_memory;
>
>You don't want to have to special case Nemployees == 0.

I see no problem in special casing it.


devel / comp.lang.c / Re: *rubeyes*: realloc(ptr, 0) is UB?

Pages:1234567
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor