Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

There's a whole WORLD in a mud puddle! -- Doug Clifford


devel / comp.lang.c / Re: The return value of realloc(p,0)

SubjectAuthor
* The return value of realloc(p,0)Kenny McCormack
+* The return value of realloc(p,0)Richard Damon
|+* The return value of realloc(p,0)Robert Latest
||`* The return value of realloc(p,0)Tim Rentsch
|| `* The return value of realloc(p,0)Vir Campestris
||  `* The return value of realloc(p,0)Tim Rentsch
||   +* The return value of realloc(p,0)Vir Campestris
||   |+- The return value of realloc(p,0)Kaz Kylheku
||   |`* The return value of realloc(p,0)Tim Rentsch
||   | +* The return value of realloc(p,0)Scott Lurndal
||   | |`* The return value of realloc(p,0)Tim Rentsch
||   | | `* The return value of realloc(p,0)Scott Lurndal
||   | |  `* The return value of realloc(p,0)Tim Rentsch
||   | |   `* The return value of realloc(p,0)Scott Lurndal
||   | |    +* The return value of realloc(p,0)Spiros Bousbouras
||   | |    |`- The return value of realloc(p,0)Malcolm McLean
||   | |    +* The return value of realloc(p,0)Tim Rentsch
||   | |    |`* The return value of realloc(p,0)Scott Lurndal
||   | |    | `- The return value of realloc(p,0)Tim Rentsch
||   | |    `- The return value of realloc(p,0)Vir Campestris
||   | `- The return value of realloc(p,0)Vir Campestris
||   `* The return value of realloc(p,0)Kaz Kylheku
||    +* The return value of realloc(p,0)Lew Pitcher
||    |+* The return value of realloc(p,0)Kaz Kylheku
||    ||`- The return value of realloc(p,0)Lew Pitcher
||    |`* The return value of realloc(p,0)Tim Rentsch
||    | `* The return value of realloc(p,0)Kaz Kylheku
||    |  `- The return value of realloc(p,0)Tim Rentsch
||    `- The return value of realloc(p,0)Tim Rentsch
|`* The return value of realloc(p,0)Tim Rentsch
| `- The return value of realloc(p,0)Scott Lurndal
+* The return value of realloc(p,0)Chris M. Thomasson
|`* The return value of realloc(p,0)Malcolm McLean
| `* The return value of realloc(p,0)Ben Bacarisse
|  `* The return value of realloc(p,0)Chris M. Thomasson
|   `- The return value of realloc(p,0)Chris M. Thomasson
`* The return value of realloc(p,0)Tim Rentsch
 `- The return value of realloc(p,0)Scott Lurndal

Pages:12
Re: The return value of realloc(p,0)

<ud4tqb$1h5ng$4@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: vir.campestris@invalid.invalid (Vir Campestris)
Newsgroups: comp.lang.c
Subject: Re: The return value of realloc(p,0)
Date: Mon, 4 Sep 2023 16:40:59 +0100
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <ud4tqb$1h5ng$4@dont-email.me>
References: <ua63if$2u50c$2@news.xmission.com>
<DeyxM.134678$ens9.28488@fx45.iad> <kl972jF24chU3@mid.individual.net>
<86edjju668.fsf@linuxsc.com> <ucqrn0$3dvle$1@dont-email.me>
<86wmxas6ty.fsf@linuxsc.com> <ucsejp$3nrkk$5@dont-email.me>
<86sf7xspk3.fsf@linuxsc.com> <JCqIM.79177$m8Ke.22576@fx08.iad>
<86zg23r6fi.fsf@linuxsc.com> <0g2JM.580001$U3w1.358152@fx09.iad>
<86a5u3qdwt.fsf@linuxsc.com> <w37JM.963104$TPw2.634145@fx17.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 4 Sep 2023 15:40:59 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="852649fb83ca7e885db61a42e853a290";
logging-data="1611504"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+l/0jtRDsUlqPDcJ3qv7Nc9w/Bpwr6ptA="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:+A/9jwZy0FLhNrUxbeQrT8tHv5M=
In-Reply-To: <w37JM.963104$TPw2.634145@fx17.iad>
Content-Language: en-GB
 by: Vir Campestris - Mon, 4 Sep 2023 15:40 UTC

On 03/09/2023 22:40, Scott Lurndal wrote:
> I've never used it (realloc) in forty-plus years of C programming.
>
> As for better utilization of memory, there may be cases where
> a growth request can be fulfilled by expanding the existing
> allocation (e.g. glibc _int_realloc). I'd think that would be
> less likely in modern threaded environments, although I'd like
> to see some real workload data, perhaps by instrumenting
> _int_realloc to keep statistics on the frequency of cases
> where the memcpy is not required.

If I have a block of memory, and I want to change its size, it's
possible that the heap management routines can optimise to something
better than make new allocation then memcpy. Perhaps there's enough free
space just past my buffer for my expansion. Perhaps there's enough free
memory that it's not worth moving the data to a smaller block.

It's not something I've used often, but I have used it occasionally.

Andy

Re: The return value of realloc(p,0)

<86fs3sq05m.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!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: The return value of realloc(p,0)
Date: Tue, 05 Sep 2023 05:23:17 -0700
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <86fs3sq05m.fsf@linuxsc.com>
References: <ua63if$2u50c$2@news.xmission.com> <DeyxM.134678$ens9.28488@fx45.iad> <kl972jF24chU3@mid.individual.net> <86edjju668.fsf@linuxsc.com> <ucqrn0$3dvle$1@dont-email.me> <86wmxas6ty.fsf@linuxsc.com> <ucsejp$3nrkk$5@dont-email.me> <86sf7xspk3.fsf@linuxsc.com> <JCqIM.79177$m8Ke.22576@fx08.iad> <86zg23r6fi.fsf@linuxsc.com> <0g2JM.580001$U3w1.358152@fx09.iad> <86a5u3qdwt.fsf@linuxsc.com> <w37JM.963104$TPw2.634145@fx17.iad> <86sf7uq01g.fsf@linuxsc.com> <RKlJM.780912$mPI2.44757@fx15.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="6793c8bc2747e0ba6d3890411794594c";
logging-data="2084334"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/5efvof0YzfamZhbiS2LgrqVs5g0ykKro="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:aGscR8XnfORX+XZ+3/z3qshMLXc=
sha1:kbQpmk18iwM0xdTKYsXHajk6maY=
 by: Tim Rentsch - Tue, 5 Sep 2023 12:23 UTC

scott@slp53.sl.home (Scott Lurndal) writes:

[ to realloc or malloc/copy/free ? ]

> Most of the code I've worked on has been bare metal (operating
> systems, hypervisors, run-time firmware) where dynamic allocation
> is either discouraged or managed with fixed size pool allocators
> or slab allocators, et cetera. I suppose that's influenced my
> thinking about the desirability of realloc() and run-time heap
> management.
>
> Clearly others have found it useful.

Now that I understand your perspective I think we are on the
same page (no pun intended). Stand-alone environments do
indeed promote a different mindset with respect to malloc()
and to memory allocation generally.

The return value of realloc(p,0)

<ua63if$2u50c$2@news.xmission.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.lang.c
Subject: The return value of realloc(p,0)
Date: Sun, 30 Jul 2023 16:36:31 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <ua63if$2u50c$2@news.xmission.com>
Injection-Date: Sun, 30 Jul 2023 16:36:31 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="3085324"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Sun, 30 Jul 2023 16:36 UTC

(Inspired by a sub-thread in one or another of the usual, interminable
navel-gazing threads for which CLC is justly famous...)

(On my system) "man realloc" says that:

If size was equal to 0, either NULL or a pointer suitable to be passed
to free() is returned.

which is odd, since, although the standard says either one is legal, the
implementation ought to know. Experimentation indicates that the answer is
the former (NULL).

But what I find interesting is the wording of the second alternative.
Earlier on, it says that the above call (realloc(p,0)) is equivalent to
free(p). So, that means that once the call is made, the pointer is no
longer valid. The text quoted above indicates that whatever is returned by
the call (if not NULL) is valid to be passed to free(). But wouldn't that
(doing so) lead to a dreaded "double free" ?

Note: I may have left some T's uncrossed and/or some I's undotted, but I am
running out of time, so I must cut this short. Rest assured that I have
considered all the possibilities, even if I have not delineated them all
here.

--
"Remember when teachers, public employees, Planned Parenthood, NPR and PBS
crashed the stock market, wiped out half of our 401Ks, took trillions in
TARP money, spilled oil in the Gulf of Mexico, gave themselves billions in
bonuses, and paid no taxes? Yeah, me neither."

Re: The return value of realloc(p,0)

<DeyxM.134678$ens9.28488@fx45.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx45.iad.POSTED!not-for-mail
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
Subject: Re: The return value of realloc(p,0)
Content-Language: en-US
Newsgroups: comp.lang.c
References: <ua63if$2u50c$2@news.xmission.com>
From: Richard@Damon-Family.org (Richard Damon)
In-Reply-To: <ua63if$2u50c$2@news.xmission.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 50
Message-ID: <DeyxM.134678$ens9.28488@fx45.iad>
X-Complaints-To: abuse@easynews.com
Organization: Forte - www.forteinc.com
X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly.
Date: Sun, 30 Jul 2023 14:46:58 -0400
X-Received-Bytes: 3392
 by: Richard Damon - Sun, 30 Jul 2023 18:46 UTC

On 7/30/23 12:36 PM, Kenny McCormack wrote:
> (Inspired by a sub-thread in one or another of the usual, interminable
> navel-gazing threads for which CLC is justly famous...)
>
> (On my system) "man realloc" says that:
>
> If size was equal to 0, either NULL or a pointer suitable to be passed
> to free() is returned.
>
> which is odd, since, although the standard says either one is legal, the
> implementation ought to know. Experimentation indicates that the answer is
> the former (NULL).
>
> But what I find interesting is the wording of the second alternative.
> Earlier on, it says that the above call (realloc(p,0)) is equivalent to
> free(p). So, that means that once the call is made, the pointer is no
> longer valid. The text quoted above indicates that whatever is returned by
> the call (if not NULL) is valid to be passed to free(). But wouldn't that
> (doing so) lead to a dreaded "double free" ?
>
> Note: I may have left some T's uncrossed and/or some I's undotted, but I am
> running out of time, so I must cut this short. Rest assured that I have
> considered all the possibilities, even if I have not delineated them all
> here.
>

My understanding is that, in effect, the call to realloc(p,0), if it is
using the second option of returning a suitable pointer, is effectively
do a free(p), and then allocating a "0 byte" block of memory and
returning the address of that. this pointer might have the same value as
the input pointer, or it might not. Calling free on the returned pointer
is allowed, and in fact needed to avoid a memory leak (of 0 bytes +
overhead) but calling free on the input pointer (unless the return
happens to match it) would be undefined behavior, as it was freed.

This is just like ANY use of the input pointer after the call, as it
might no longer be valid, unless of course, the return pointer happened
to match the input pointer as it was able to do the operation "in place".

As to why the document both behaviors, it may be that they support
multiple implementation libraries, and don't want to restrict them more
than the Standard does. It is NOT "Impleentation Defined Behavor" as to
which it does, it is just Unspecified, so the implementation can do
either, and could in fact do both at different times.

This just goes back to the Standards Committee not wanting to take sides
in a philosophical debate about 0-byte objects back when this came up,
and then not wanting to break code that depends on either behavior, such
code just needs to use implementations that promise the behavior they
depend on.

Re: The return value of realloc(p,0)

<ua6cnc$30dbn$6@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!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: The return value of realloc(p,0)
Date: Sun, 30 Jul 2023 12:12:44 -0700
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <ua6cnc$30dbn$6@dont-email.me>
References: <ua63if$2u50c$2@news.xmission.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 30 Jul 2023 19:12:44 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="cd71dc00a2eb6664fbb78c38275530f3";
logging-data="3159415"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+4T9cXklFVO6gtDNlDpcNo5ZXi5PvBjgA="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.13.0
Cancel-Lock: sha1:8iYmBr+OzkwT+OCBQYMxMamzN5w=
Content-Language: en-US
In-Reply-To: <ua63if$2u50c$2@news.xmission.com>
 by: Chris M. Thomasson - Sun, 30 Jul 2023 19:12 UTC

On 7/30/2023 9:36 AM, Kenny McCormack wrote:
> (Inspired by a sub-thread in one or another of the usual, interminable
> navel-gazing threads for which CLC is justly famous...)
>
> (On my system) "man realloc" says that:
>
> If size was equal to 0, either NULL or a pointer suitable to be passed
> to free() is returned.
>
> which is odd, since, although the standard says either one is legal, the
> implementation ought to know. Experimentation indicates that the answer is
> the former (NULL).
>
> But what I find interesting is the wording of the second alternative.
> Earlier on, it says that the above call (realloc(p,0)) is equivalent to
> free(p). So, that means that once the call is made, the pointer is no
> longer valid. The text quoted above indicates that whatever is returned by
> the call (if not NULL) is valid to be passed to free(). But wouldn't that
> (doing so) lead to a dreaded "double free" ?
>
> Note: I may have left some T's uncrossed and/or some I's undotted, but I am
> running out of time, so I must cut this short. Rest assured that I have
> considered all the possibilities, even if I have not delineated them all
> here.
>

void* p = realloc(p,0);
free(p);

Is fine. free(NULL) is fine.

Even if p is not NULL, it still can be passed to free. Keep in mind that
if p is not NULL it should never be used because its pointing to no
memory at all. What am I missing here?

Thanks.

Re: The return value of realloc(p,0)

<3bfac81a-5bdd-4be7-b2e6-c4a0ee4f68c0n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:440e:b0:40f:91be:b62a with SMTP id ka14-20020a05622a440e00b0040f91beb62amr8807qtb.6.1690794981721;
Mon, 31 Jul 2023 02:16:21 -0700 (PDT)
X-Received: by 2002:a05:6870:c98d:b0:1b3:ecdb:ff21 with SMTP id
hi13-20020a056870c98d00b001b3ecdbff21mr18515188oab.3.1690794981392; Mon, 31
Jul 2023 02:16:21 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Mon, 31 Jul 2023 02:16:21 -0700 (PDT)
In-Reply-To: <ua6cnc$30dbn$6@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23a8:400a:5601:1c70:988a:9a4c:dd97;
posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 2a00:23a8:400a:5601:1c70:988a:9a4c:dd97
References: <ua63if$2u50c$2@news.xmission.com> <ua6cnc$30dbn$6@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <3bfac81a-5bdd-4be7-b2e6-c4a0ee4f68c0n@googlegroups.com>
Subject: Re: The return value of realloc(p,0)
From: malcolm.arthur.mclean@gmail.com (Malcolm McLean)
Injection-Date: Mon, 31 Jul 2023 09:16:21 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 3313
 by: Malcolm McLean - Mon, 31 Jul 2023 09:16 UTC

On Sunday, 30 July 2023 at 20:12:58 UTC+1, Chris M. Thomasson wrote:
> On 7/30/2023 9:36 AM, Kenny McCormack wrote:
> > (Inspired by a sub-thread in one or another of the usual, interminable
> > navel-gazing threads for which CLC is justly famous...)
> >
> > (On my system) "man realloc" says that:
> >
> > If size was equal to 0, either NULL or a pointer suitable to be passed
> > to free() is returned.
> >
> > which is odd, since, although the standard says either one is legal, the
> > implementation ought to know. Experimentation indicates that the answer is
> > the former (NULL).
> >
> > But what I find interesting is the wording of the second alternative.
> > Earlier on, it says that the above call (realloc(p,0)) is equivalent to
> > free(p). So, that means that once the call is made, the pointer is no
> > longer valid. The text quoted above indicates that whatever is returned by
> > the call (if not NULL) is valid to be passed to free(). But wouldn't that
> > (doing so) lead to a dreaded "double free" ?
> >
> > Note: I may have left some T's uncrossed and/or some I's undotted, but I am
> > running out of time, so I must cut this short. Rest assured that I have
> > considered all the possibilities, even if I have not delineated them all
> > here.
> >
> void* p = realloc(p,0);
> free(p);
>
> Is fine. free(NULL) is fine.
>
> Even if p is not NULL, it still can be passed to free. Keep in mind that
> if p is not NULL it should never be used because its pointing to no
> memory at all. What am I missing here?
>
p is a valid pointer, but to a buffer of zero bytes. So it can be passed to any
function expecting a buffer, as long as that function is written to handle the
N ==0 case properly.
If we have this code.
unsigned char randomjunk[sizeof(void*)];
mempcy(&p, randomjunk, sizeof(void *));
functionexpectingabuffer(p, 0);

Then it's UB because the system can pick up that we're loading random junk
into an address register and trigger some sort of memory protection.

However is p is returned from realloc(ptr, 0) then it's OK.

Re: The return value of realloc(p,0)

<87cz08w18i.fsf@bsb.me.uk>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.usenet@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: The return value of realloc(p,0)
Date: Mon, 31 Jul 2023 16:26:53 +0100
Organization: A noiseless patient Spider
Lines: 58
Message-ID: <87cz08w18i.fsf@bsb.me.uk>
References: <ua63if$2u50c$2@news.xmission.com> <ua6cnc$30dbn$6@dont-email.me>
<3bfac81a-5bdd-4be7-b2e6-c4a0ee4f68c0n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="b326b269c93766db184fda50f82c858f";
logging-data="3483988"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Ogu+6urg6D2r89hjToYtVU0k4kOZiHhY="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:tp8CFXJq96rNs00yj/y0l5EKej8=
sha1:VtcxE1h46IDGP8wKxStE96ga928=
X-BSB-Auth: 1.24a7824ee888c7f79344.20230731162653BST.87cz08w18i.fsf@bsb.me.uk
 by: Ben Bacarisse - Mon, 31 Jul 2023 15:26 UTC

Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:

> On Sunday, 30 July 2023 at 20:12:58 UTC+1, Chris M. Thomasson wrote:

>> void* p = realloc(p,0);

Danger Will Robinson! This is UB: p is indeterminate when passed to
realloc.

>> free(p);
>>
>> Is fine. free(NULL) is fine.
>>
>> Even if p is not NULL, it still can be passed to free. Keep in mind that
>> if p is not NULL it should never be used because its pointing to no
>> memory at all. What am I missing here?

Yes. If p in either NULL or a valid pointer from a prior allocation,
then

p = realloc(p, 0);
free(p);

is fine which was the point you wanted to make. It's unfortunate that
your example had an unrelated problem.

> p is a valid pointer, but to a buffer of zero bytes. So it can be
> passed to any function expecting a buffer, as long as that function is
> written to handle the N ==0 case properly.
> If we have this code.
> unsigned char randomjunk[sizeof(void*)];
> mempcy(&p, randomjunk, sizeof(void *));
> functionexpectingabuffer(p, 0);
>
> Then it's UB because the system can pick up that we're loading random junk
> into an address register and trigger some sort of memory protection.

I see you have decided to join Bart in misusing the term UB. That code
is UB regardless of what the system can or can't do. It's UB because
the language standard says you can't expect any defined behaviour from
that code.

Also, your supposed "reason" could lead readers astray. There is no UB
in this:

unsigned char randomjunk[sizeof (void*)];
mempcy(&p, randomjunk, sizeof p);

The undefined behaviour comes from /any/ access to the value in p after
this, even if that access does not involve it being treated as an
address. The confusion comes from the fact "loading random junk into an
address register" might be taken to refer to these lines, not the
passing of p to a function. After all, there no reason to think that
the code you show will result in a junk value getting into an address
register.

--
Ben.

Re: The return value of realloc(p,0)

<ua99ft$3cuas$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!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: The return value of realloc(p,0)
Date: Mon, 31 Jul 2023 14:35:57 -0700
Organization: A noiseless patient Spider
Lines: 73
Message-ID: <ua99ft$3cuas$1@dont-email.me>
References: <ua63if$2u50c$2@news.xmission.com> <ua6cnc$30dbn$6@dont-email.me>
<3bfac81a-5bdd-4be7-b2e6-c4a0ee4f68c0n@googlegroups.com>
<87cz08w18i.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 31 Jul 2023 21:35:57 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="4a4b7044b62dfd75cc9233ad5dc1fea1";
logging-data="3570012"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+oZNANHLDECgakzTov1Cjx+hys3lO3GhE="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.13.0
Cancel-Lock: sha1:ltaay3jlrBR7/unxNo9unlubNZ0=
In-Reply-To: <87cz08w18i.fsf@bsb.me.uk>
Content-Language: en-US
 by: Chris M. Thomasson - Mon, 31 Jul 2023 21:35 UTC

On 7/31/2023 8:26 AM, Ben Bacarisse wrote:
> Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:
>
>> On Sunday, 30 July 2023 at 20:12:58 UTC+1, Chris M. Thomasson wrote:
>
>>> void* p = realloc(p,0);
>
> Danger Will Robinson! This is UB: p is indeterminate when passed to
> realloc.

AHHH SHIT!!!

void* b = realloc(a, 0);

God damn it! Sorry!

>
>>> free(p);
>>>
>>> Is fine. free(NULL) is fine.
>>>
>>> Even if p is not NULL, it still can be passed to free. Keep in mind that
>>> if p is not NULL it should never be used because its pointing to no
>>> memory at all. What am I missing here?
>
> Yes. If p in either NULL or a valid pointer from a prior allocation,
> then
>
> p = realloc(p, 0);
> free(p);
>
> is fine which was the point you wanted to make. It's unfortunate that
> your example had an unrelated problem.

Yeah. Shit happens. Thanks for the heads up, Ben. God Damn it.

>
>> p is a valid pointer, but to a buffer of zero bytes. So it can be
>> passed to any function expecting a buffer, as long as that function is
>> written to handle the N ==0 case properly.
>> If we have this code.
>> unsigned char randomjunk[sizeof(void*)];
>> mempcy(&p, randomjunk, sizeof(void *));
>> functionexpectingabuffer(p, 0);
>>
>> Then it's UB because the system can pick up that we're loading random junk
>> into an address register and trigger some sort of memory protection.
>
> I see you have decided to join Bart in misusing the term UB. That code
> is UB regardless of what the system can or can't do. It's UB because
> the language standard says you can't expect any defined behaviour from
> that code.
>
> Also, your supposed "reason" could lead readers astray. There is no UB
> in this:
>
> unsigned char randomjunk[sizeof (void*)];
> mempcy(&p, randomjunk, sizeof p);
>
> The undefined behaviour comes from /any/ access to the value in p after
> this, even if that access does not involve it being treated as an
> address. The confusion comes from the fact "loading random junk into an
> address register" might be taken to refer to these lines, not the
> passing of p to a function. After all, there no reason to think that
> the code you show will result in a junk value getting into an address
> register.
>

Re: The return value of realloc(p,0)

<ua99ih$3cuas$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!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: The return value of realloc(p,0)
Date: Mon, 31 Jul 2023 14:37:21 -0700
Organization: A noiseless patient Spider
Lines: 26
Message-ID: <ua99ih$3cuas$2@dont-email.me>
References: <ua63if$2u50c$2@news.xmission.com> <ua6cnc$30dbn$6@dont-email.me>
<3bfac81a-5bdd-4be7-b2e6-c4a0ee4f68c0n@googlegroups.com>
<87cz08w18i.fsf@bsb.me.uk> <ua99ft$3cuas$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 31 Jul 2023 21:37:22 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="4a4b7044b62dfd75cc9233ad5dc1fea1";
logging-data="3570012"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18wOkkfN2WOaVnaM5JpZz4a++SsFwt4EzQ="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.13.0
Cancel-Lock: sha1:/CCx1FeepYbWj3cg77vV17VvcaE=
In-Reply-To: <ua99ft$3cuas$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Mon, 31 Jul 2023 21:37 UTC

On 7/31/2023 2:35 PM, Chris M. Thomasson wrote:
> On 7/31/2023 8:26 AM, Ben Bacarisse wrote:
>> Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:
>>
>>> On Sunday, 30 July 2023 at 20:12:58 UTC+1, Chris M. Thomasson wrote:
>>
>>>> void* p = realloc(p,0);
>>
>> Danger Will Robinson!  This is UB: p is indeterminate when passed to
>> realloc.

Face palm! How about danger Roger Wilco from SpaceQuest! ;^o

ARGGHHH!

>
> AHHH SHIT!!!
>
> void* b = realloc(a, 0);
>
> God damn it! Sorry!
[...]

Re: The return value of realloc(p,0)

<86v8dvgwlv.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!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: The return value of realloc(p,0)
Date: Thu, 03 Aug 2023 23:17:16 -0700
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <86v8dvgwlv.fsf@linuxsc.com>
References: <ua63if$2u50c$2@news.xmission.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="7a95792a6e880c00df8e418e5a126a38";
logging-data="1243314"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/DrM9E6hnpvXDo/dxEO7Wn+ptiWu+flok="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:EKGjPjEOPbbuDm0bLDTdk5H49jY=
sha1:SviaX23e7giC9AruoTfiyYwZ9Vw=
 by: Tim Rentsch - Fri, 4 Aug 2023 06:17 UTC

gazelle@shell.xmission.com (Kenny McCormack) writes:

> (On my system) "man realloc" says that:
>
> If size was equal to 0, either NULL or a pointer suitable to
> be passed to free() is returned.
>
> which is odd, since, although the standard says either one is
> legal, the implementation ought to know. [...]

Probably the man page was written to express both possibilities,
so it could be used without change in different distributions.
Normally man pages are separate from whatever C implementation
is present; indeed there may be more than one, with different
choices made in the different implementations.

> But what I find interesting is the wording of the second
> alternative. Earlier on, it says that the above call
> (realloc(p,0)) is equivalent to free(p). [...]

The man page is either wrong or poorly written. I expect most
people have seen plenty of cases where what a man page says does
not exactly match what the C standard mandates.

Re: The return value of realloc(p,0)

<86r0ojgvux.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!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: The return value of realloc(p,0)
Date: Thu, 03 Aug 2023 23:33:26 -0700
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <86r0ojgvux.fsf@linuxsc.com>
References: <ua63if$2u50c$2@news.xmission.com> <DeyxM.134678$ens9.28488@fx45.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="7a95792a6e880c00df8e418e5a126a38";
logging-data="1247365"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/N21AMJGBQot46XX7XKZMe629r7gGRmuY="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:PvMpakJ4kziausdySrGP/OiNTi0=
sha1:wAoz3A5r7K6SZ0xZCMYuXeyf60o=
 by: Tim Rentsch - Fri, 4 Aug 2023 06:33 UTC

Richard Damon <Richard@Damon-Family.org> writes:

> On 7/30/23 12:36 PM, Kenny McCormack wrote:
>
>> (On my system) "man realloc" says that:
>>
>> If size was equal to 0, either NULL or a pointer suitable to
>> be passed to free() is returned.
>>
>> which is odd, since, although the standard says either one is
>> legal, the implementation ought to know.

[...]

> As to why the document both behaviors, it may be that they support
> multiple implementation libraries, and don't want to restrict them
> more than the Standard does. It is NOT "Impleentation Defined
> Behavor" as to which it does, it is just Unspecified, so the
> implementation can do either, and could in fact do both at
> different times.

The choice for whether an allocation (or reallocation) of size 0
returns a null pointer or a "unique pointer" has been implementation
defined since C90: section 7.10.3 paragraph 1 in C90, 7.20.3 p1 in
n1256 for C99, and 7.22.3 p1 in n1570 for C11.

Re: The return value of realloc(p,0)

<px7zM.474555$GMN3.222566@fx16.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx16.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: The return value of realloc(p,0)
Newsgroups: comp.lang.c
References: <ua63if$2u50c$2@news.xmission.com> <86v8dvgwlv.fsf@linuxsc.com>
Lines: 22
Message-ID: <px7zM.474555$GMN3.222566@fx16.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Fri, 04 Aug 2023 14:01:57 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Fri, 04 Aug 2023 14:01:57 GMT
X-Received-Bytes: 1511
 by: Scott Lurndal - Fri, 4 Aug 2023 14:01 UTC

Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>gazelle@shell.xmission.com (Kenny McCormack) writes:
>
>> (On my system) "man realloc" says that:
>>
>> If size was equal to 0, either NULL or a pointer suitable to
>> be passed to free() is returned.
>>
>> which is odd, since, although the standard says either one is
>> legal, the implementation ought to know. [...]
>
>Probably the man page was written to express both possibilities,
>so it could be used without change in different distributions.
>Normally man pages are separate from whatever C implementation
>is present; indeed there may be more than one, with different
>choices made in the different implementations.

Some linux distributions simply provide the POSIX/SuS pages
rather than developing their own set of manual pages. Thus
the page will have generic elements not necessarily specific to
the implementation.

Re: The return value of realloc(p,0)

<WE7zM.474556$GMN3.407254@fx16.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx16.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: The return value of realloc(p,0)
Newsgroups: comp.lang.c
References: <ua63if$2u50c$2@news.xmission.com> <DeyxM.134678$ens9.28488@fx45.iad> <86r0ojgvux.fsf@linuxsc.com>
Lines: 37
Message-ID: <WE7zM.474556$GMN3.407254@fx16.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Fri, 04 Aug 2023 14:09:58 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Fri, 04 Aug 2023 14:09:58 GMT
X-Received-Bytes: 2158
 by: Scott Lurndal - Fri, 4 Aug 2023 14:09 UTC

Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>Richard Damon <Richard@Damon-Family.org> writes:
>
>> On 7/30/23 12:36 PM, Kenny McCormack wrote:
>>
>>> (On my system) "man realloc" says that:
>>>
>>> If size was equal to 0, either NULL or a pointer suitable to
>>> be passed to free() is returned.
>>>
>>> which is odd, since, although the standard says either one is
>>> legal, the implementation ought to know.
>
>[...]
>
>> As to why the document both behaviors, it may be that they support
>> multiple implementation libraries, and don't want to restrict them
>> more than the Standard does. It is NOT "Impleentation Defined
>> Behavor" as to which it does, it is just Unspecified, so the
>> implementation can do either, and could in fact do both at
>> different times.
>
>The choice for whether an allocation (or reallocation) of size 0
>returns a null pointer or a "unique pointer" has been implementation
>defined since C90: section 7.10.3 paragraph 1 in C90, 7.20.3 p1 in
>n1256 for C99, and 7.22.3 p1 in n1570 for C11.

Even earlier, the System V Interface Definition (SVID3), first published
in 1983, says:

"If the size of the space requested is zero, the bahavior is
implementation defined; the value returned will either be a NULL
pointer or an unique pointer."

The Future Directions section indicates that the interface defintions
will use size_t instead of unsigned for the size parameter and the
older versions were moved to level 2 on 1989-09-30.

Pages:12
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor