Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

May the bluebird of happiness twiddle your bits.


devel / comp.lang.c / Re: memcpy with NULL pointer

SubjectAuthor
* memcpy with NULL pointerSteve Keller
`* Re: memcpy with NULL pointerTim Rentsch
 +* Re: memcpy with NULL pointerSteve Keller
 |+- Re: memcpy with NULL pointerKeith Thompson
 |+* Re: memcpy with NULL pointerScott Lurndal
 ||`- Re: memcpy with NULL pointerSteve Keller
 |`* Re: memcpy with NULL pointerTim Rentsch
 | `* Re: memcpy with NULL pointerGuillaume
 |  `* Re: memcpy with NULL pointerSteve Keller
 |   `* Re: memcpy with NULL pointerDolores Filandro
 |    `* Re: memcpy with NULL pointerÖö Tiib
 |     `* Re: memcpy with NULL pointerRichard Damon
 |      `- Re: memcpy with NULL pointerÖö Tiib
 `* Re: memcpy with NULL pointerPeter 'Shaggy' Haywood
  +* Re: memcpy with NULL pointerJames Kuyper
  |`- Re: memcpy with NULL pointerManfred
  `- Re: memcpy with NULL pointerTim Rentsch

1
memcpy with NULL pointer

<slgdn2$1v7q$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18796&group=comp.lang.c#18796

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!6aZaJJIPCrhkZ+V8lLkdvw.user.46.165.242.75.POSTED!not-for-mail
From: keller.s...@gmx.de (Steve Keller)
Newsgroups: comp.lang.c
Subject: memcpy with NULL pointer
Date: Fri, 29 Oct 2021 11:08:48 +0200
Organization: Aioe.org NNTP Server
Message-ID: <slgdn2$1v7q$1@gioia.aioe.org>
Injection-Info: gioia.aioe.org; logging-data="64762"; posting-host="6aZaJJIPCrhkZ+V8lLkdvw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Steve Keller - Fri, 29 Oct 2021 09:08 UTC

I wonder whether calling the mem* and str* functions with a NULL
pointer has defined bahavior if the count parameter is also 0, like in
this call

#include <string.h>

...
memcpy(NULL, NULL, 0);

The same for memmove(NULL, NULL, 0), memcmp(NULL, NULL, 0),
strncpy(dst, NULL, 0), strncat(dst, NULL, 0), strncmp(NULL, NULL, 0).

It seems the standard doesn't say anything about this.

Since no memory access via the NULL pointer is done I'd assume this
should not result in undefined behavior.

Steve

Re: memcpy with NULL pointer

<86ee84f70q.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18799&group=comp.lang.c#18799

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: memcpy with NULL pointer
Date: Fri, 29 Oct 2021 03:40:53 -0700
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <86ee84f70q.fsf@linuxsc.com>
References: <slgdn2$1v7q$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="b7f56eff5ad6c7d9130605d054ce58ef";
logging-data="21911"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19j0BVsjtaJgsUCSDBgtPJh4xpvm6RN6p0="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:QxivDQL+ekYLYUvzVsk0FwVS79I=
sha1:DNkXD76jMGRNANa13e78xNvtzxA=
 by: Tim Rentsch - Fri, 29 Oct 2021 10:40 UTC

Steve Keller <keller.steve@gmx.de> writes:

> I wonder whether calling the mem* and str* functions with a NULL
> pointer has defined bahavior if the count parameter is also 0,
> like in this call
>
> #include <string.h>
>
> ...
> memcpy(NULL, NULL, 0);
>
> The same for memmove(NULL, NULL, 0), memcmp(NULL, NULL, 0),
> strncpy(dst, NULL, 0), strncat(dst, NULL, 0), strncmp(NULL, NULL, 0).

They are all undefined behavior.

> It seems the standard doesn't say anything about this.

It does. Paragraph 2 of 7.24.1, "String function conventions", says
this:

Where an argument declared as size_t n specifies the length
of the array for a function, n can have the value zero on a
call to that function. Unless explicitly stated otherwise in
the description of a particular function in this subclause,
pointer arguments on such a call shall still have valid
values, as described in 7.1.4. [...]

A null pointer is not among the set of valid values. Refer to
section 7.1.4, paragraph 1, for details. (I did a quick check of
the six functions you mentioned and did not see any indication
that they are exceptions to the above rule. Of course, I cannot
promise that such quick checks are 100% reliable, so please feel
free to double check me on that.)

> Since no memory access via the NULL pointer is done I'd assume
> this should not result in undefined behavior.

That's a plausible assumption but not one that the C standard
supports.

Re: memcpy with NULL pointer

<slhno6$pe$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18822&group=comp.lang.c#18822

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!6aZaJJIPCrhkZ+V8lLkdvw.user.46.165.242.75.POSTED!not-for-mail
From: keller.s...@gmx.de (Steve Keller)
Newsgroups: comp.lang.c
Subject: Re: memcpy with NULL pointer
Date: Fri, 29 Oct 2021 23:06:14 +0200
Organization: Aioe.org NNTP Server
Message-ID: <slhno6$pe$1@gioia.aioe.org>
References: <slgdn2$1v7q$1@gioia.aioe.org> <86ee84f70q.fsf@linuxsc.com>
Injection-Info: gioia.aioe.org; logging-data="814"; posting-host="6aZaJJIPCrhkZ+V8lLkdvw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Steve Keller - Fri, 29 Oct 2021 21:06 UTC

Tim Rentsch <tr.17687@z991.linuxsc.com> writes:

> > The same for memmove(NULL, NULL, 0), memcmp(NULL, NULL, 0),
> > strncpy(dst, NULL, 0), strncat(dst, NULL, 0), strncmp(NULL, NULL, 0).
>
> They are all undefined behavior.

:-(

> > It seems the standard doesn't say anything about this.
>
> It does. Paragraph 2 of 7.24.1, "String function conventions", says
> this:
>
> Where an argument declared as size_t n specifies the length
> of the array for a function, n can have the value zero on a
> call to that function. Unless explicitly stated otherwise in
> the description of a particular function in this subclause,
> pointer arguments on such a call shall still have valid
> values, as described in 7.1.4. [...]

Hm, I've overseen that paragraph and the reference to 7.1.4 in "String
handling" (7.21 in my C9X draft, 7.24 in the final standard?).

> A null pointer is not among the set of valid values. Refer to
> section 7.1.4, paragraph 1, for details. (I did a quick check of
> the six functions you mentioned and did not see any indication
> that they are exceptions to the above rule. Of course, I cannot
> promise that such quick checks are 100% reliable, so please feel
> free to double check me on that.)
>
> > Since no memory access via the NULL pointer is done I'd assume
> > this should not result in undefined behavior.

I've read the description of all of these functions in "String
handling <string.h>" and found no exception, so you're right. As I
have overseen the above I came to my false assumption.

> That's a plausible assumption but not one that the C standard
> supports.

That's a pity. I really wish the standard wouldn't leave these and
thing like malloc(0) undefined or implementation-defined. This would
avoid quite some checks on corner cases, like in

void func(int n) {
char *ptr, tmp[n];
if (n > 0 && !(ptr = malloc(n))) {
// handle error
}
...
int cmp_res = (n == 0) ? 0 : memcmp(ptr, ..., n);
if (n > 0)
memcpy(tmp, ptr, n);
...
}

BTW, has a VLA like char tmp[n]; with value zero for n defined
behavior?

Steve

Re: memcpy with NULL pointer

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

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18823&group=comp.lang.c#18823

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: memcpy with NULL pointer
Date: Fri, 29 Oct 2021 14:23:22 -0700
Organization: None to speak of
Lines: 20
Message-ID: <87cznnzfsl.fsf@nosuchdomain.example.com>
References: <slgdn2$1v7q$1@gioia.aioe.org> <86ee84f70q.fsf@linuxsc.com>
<slhno6$pe$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="d19e72ac6c40f753288e1cb579e60581";
logging-data="29326"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+GtAzkjDtEOjvKqxM6s+Hh"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:wrD4IBefMTKCR3FPuYG1yGdu4IQ=
sha1:yB/9oz6oAFHr/kOjZ2QMiFBEcVU=
 by: Keith Thompson - Fri, 29 Oct 2021 21:23 UTC

Steve Keller <keller.steve@gmx.de> writes:
[...]
> BTW, has a VLA like char tmp[n]; with value zero for n defined
> behavior?

No, the behavior is undefined if n is not positive.

N2176 (C17 draft) 6.7.6.2p5:

If the size is an expression that is not an integer constant expression:
if it occurs in a declaration at function prototype scope, it is treated
as if it were replaced by * ; otherwise, each time it is evaluated it
shall have a value greater than zero.

That "shall" is outside a constraint, so violating it has undefined behavior.

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

Re: memcpy with NULL pointer

<v_ZeJ.6340$QB1.5143@fx42.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18824&group=comp.lang.c#18824

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!paganini.bofh.team!news.dns-netz.com!news.freedyn.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!news-out.netnews.com!news.alt.net!fdc2.netnews.com!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx42.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: memcpy with NULL pointer
Newsgroups: comp.lang.c
References: <slgdn2$1v7q$1@gioia.aioe.org> <86ee84f70q.fsf@linuxsc.com> <slhno6$pe$1@gioia.aioe.org>
Lines: 16
Message-ID: <v_ZeJ.6340$QB1.5143@fx42.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Fri, 29 Oct 2021 21:48:11 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Fri, 29 Oct 2021 21:48:11 GMT
X-Received-Bytes: 1305
 by: Scott Lurndal - Fri, 29 Oct 2021 21:48 UTC

Steve Keller <keller.steve@gmx.de> writes:
>Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>
>> > The same for memmove(NULL, NULL, 0), memcmp(NULL, NULL, 0),
>> > strncpy(dst, NULL, 0), strncat(dst, NULL, 0), strncmp(NULL, NULL, 0).
>>
>> They are all undefined behavior.
>
>:-(

We actually looked into adding EFAULT as an error to the mem* and str*
functions back in the 90's. Ultimately the overhead to validate
the pointers was too great (absent dedicated hardware support such
as CHERI[*]) and the proposal was discarded.

[*] https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/

Re: memcpy with NULL pointer

<86bl37e17c.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18833&group=comp.lang.c#18833

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: memcpy with NULL pointer
Date: Fri, 29 Oct 2021 18:44:07 -0700
Organization: A noiseless patient Spider
Lines: 67
Message-ID: <86bl37e17c.fsf@linuxsc.com>
References: <slgdn2$1v7q$1@gioia.aioe.org> <86ee84f70q.fsf@linuxsc.com> <slhno6$pe$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="6172d9893ff5ddcd59d33e927dfcf2b5";
logging-data="13150"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19dbZ0JTMKlTCNaDy5sTFX4LS9a1pftB8c="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:laOAbm4fPAsQhz3lcrbRggbk9+c=
sha1:oKHgEd0ZPHO1D9nj29fZuH/sQn4=
 by: Tim Rentsch - Sat, 30 Oct 2021 01:44 UTC

Steve Keller <keller.steve@gmx.de> writes:

> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>
>>> The same for memmove(NULL, NULL, 0), memcmp(NULL, NULL, 0),
>>> strncpy(dst, NULL, 0), strncat(dst, NULL, 0), strncmp(NULL, NULL, 0).
>>
>> They are all undefined behavior.
>
> :-(
>
>>> It seems the standard doesn't say anything about this.
>>
>> It does. Paragraph 2 of 7.24.1, "String function conventions", says
>> this:
>>
>> Where an argument declared as size_t n specifies the length
>> of the array for a function, n can have the value zero on a
>> call to that function. Unless explicitly stated otherwise in
>> the description of a particular function in this subclause,
>> pointer arguments on such a call shall still have valid
>> values, as described in 7.1.4. [...]
>
> Hm, I've overseen that paragraph and the reference to 7.1.4 in
> "String handling" (7.21 in my C9X draft, 7.24 in the final
> standard?).

Do you mean overlooked rather than overseen?

>> A null pointer is not among the set of valid values. Refer to
>> section 7.1.4, paragraph 1, for details. (I did a quick check of
>> the six functions you mentioned and did not see any indication
>> that they are exceptions to the above rule. Of course, I cannot
>> promise that such quick checks are 100% reliable, so please feel
>> free to double check me on that.)
>>
>>> Since no memory access via the NULL pointer is done I'd assume
>>> this should not result in undefined behavior.
>
> I've read the description of all of these functions in "String
> handling <string.h>" and found no exception, so you're right. As I
> have overseen the above I came to my false assumption.
>
>> That's a plausible assumption but not one that the C standard
>> supports.
>
> That's a pity. I really wish the standard wouldn't leave these
> and thing like malloc(0) undefined or implementation-defined.
> [...]

Rather than wishing the C standard would be different than it is,
avoid the problem by using wrapper functions (disclaimer: not
compiled):

static inline void *
safer_memcpy( void *d, const void *s, size_t n ){
return n ? memcpy( d, s, n ) : d;
}

static inline void *
safer_memcmp( const void *s1, const void *s2, size_t n ){
return n ? memcpy( s1, s2, n ) : 0;
}

...

and move on to the next trouble spot.

Re: memcpy with NULL pointer

<sljrlk$e21$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18837&group=comp.lang.c#18837

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!DlgLRg+VHgX+keYJDqJrig.user.46.165.242.75.POSTED!not-for-mail
From: mess...@bottle.org (Guillaume)
Newsgroups: comp.lang.c
Subject: Re: memcpy with NULL pointer
Date: Sat, 30 Oct 2021 18:25:20 +0200
Organization: Aioe.org NNTP Server
Message-ID: <sljrlk$e21$1@gioia.aioe.org>
References: <slgdn2$1v7q$1@gioia.aioe.org> <86ee84f70q.fsf@linuxsc.com>
<slhno6$pe$1@gioia.aioe.org> <86bl37e17c.fsf@linuxsc.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="14401"; posting-host="DlgLRg+VHgX+keYJDqJrig.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.2.1
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: fr
 by: Guillaume - Sat, 30 Oct 2021 16:25 UTC

Le 30/10/2021 à 03:44, Tim Rentsch a écrit :
> Steve Keller <keller.steve@gmx.de> writes:
>
>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>>
>>>> The same for memmove(NULL, NULL, 0), memcmp(NULL, NULL, 0),
>>>> strncpy(dst, NULL, 0), strncat(dst, NULL, 0), strncmp(NULL, NULL, 0).
>>>
>>> They are all undefined behavior.
>>
>> :-(
>>
>>>> It seems the standard doesn't say anything about this.
>>>
>>> It does. Paragraph 2 of 7.24.1, "String function conventions", says
>>> this:
>>>
>>> Where an argument declared as size_t n specifies the length
>>> of the array for a function, n can have the value zero on a
>>> call to that function. Unless explicitly stated otherwise in
>>> the description of a particular function in this subclause,
>>> pointer arguments on such a call shall still have valid
>>> values, as described in 7.1.4. [...]
>>
>> Hm, I've overseen that paragraph and the reference to 7.1.4 in
>> "String handling" (7.21 in my C9X draft, 7.24 in the final
>> standard?).
>
> Do you mean overlooked rather than overseen?
>
>>> A null pointer is not among the set of valid values. Refer to
>>> section 7.1.4, paragraph 1, for details. (I did a quick check of
>>> the six functions you mentioned and did not see any indication
>>> that they are exceptions to the above rule. Of course, I cannot
>>> promise that such quick checks are 100% reliable, so please feel
>>> free to double check me on that.)
>>>
>>>> Since no memory access via the NULL pointer is done I'd assume
>>>> this should not result in undefined behavior.
>>
>> I've read the description of all of these functions in "String
>> handling <string.h>" and found no exception, so you're right. As I
>> have overseen the above I came to my false assumption.
>>
>>> That's a plausible assumption but not one that the C standard
>>> supports.
>>
>> That's a pity. I really wish the standard wouldn't leave these
>> and thing like malloc(0) undefined or implementation-defined.
>> [...]
>
> Rather than wishing the C standard would be different than it is,
> avoid the problem by using wrapper functions (disclaimer: not
> compiled):

Yes of course. It's straighforward and this way, you get the checks if
you want/need them instread of imposing them on everyone regardless of
the use case.

Those std functions should be considered "low-level" functions.
I for one frankly don't care if any of the above examples, like
"memcmp(NULL, NULL, 0)" are UB. If you are bound to call those with such
parameters, there's something wrong with your coding style IMHO. And
now, in particular cases for which it would make sense (because it could
make your code more elegant or something), then you can use wrappers as
suggested above.

Don't get me wrong - I rather like functions that do exhaustive
parameter validation in general. But I can also recognize tradeoffs to
this rule when performance could be an issue, for instance for this kind
of "low-level" functions.

If there was something to complain about, possibly, that would be some
inconsistency in how C std functions check their parameters, rather than
a lack of checks. For instance, it's OK to call free() with a NULL
pointer, but using a NULL pointer in some other C std functions
expecting a pointer is UB. That is kind of WTF to me. But we're usually
fine if we just RTFM.

Re: memcpy with NULL pointer

<ue195i-892.ln1@aretha.foo>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18848&group=comp.lang.c#18848

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!buffer2.nntp.dca1.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Wed, 03 Nov 2021 04:08:15 -0500
Message-Id: <ue195i-892.ln1@aretha.foo>
From: phayw...@alphalink.com.au (Peter 'Shaggy' Haywood)
Subject: Re: memcpy with NULL pointer
Newsgroups: comp.lang.c
Date: Wed, 03 Nov 2021 11:24:30 +1100
References: <slgdn2$1v7q$1@gioia.aioe.org> <86ee84f70q.fsf@linuxsc.com>
User-Agent: KNode/0.10.9
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7Bit
Lines: 28
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-dmleTsHnFaeWDYW38gZTMFYuxMDj9rfmpYbGeWsXZ6D5EHnQgt32hyaR0xw5Tk74PhVuIiz+peWUfGZ!QZAG1lI3sipCF212tnao8rCUfBnM6GfC9a9CC4aZf43JBMhaQFQ=
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 1796
 by: Peter 'Shaggy&# - Wed, 3 Nov 2021 00:24 UTC

Groovy hepcat Tim Rentsch was jivin' in comp.lang.c on Fri, 29 Oct 2021
09:40 pm. It's a cool scene! Dig it.

> Steve Keller <keller.steve@gmx.de> writes:
>
>> I wonder whether calling the mem* and str* functions with a NULL
>> pointer has defined bahavior if the count parameter is also 0,
>> like in this call

[Snip.]

>> Since no memory access via the NULL pointer is done I'd assume
>> this should not result in undefined behavior.
>
> That's a plausible assumption but not one that the C standard
> supports.

But, of course, undefined behaviour could be perfectly benign. Just
don't count on it in every case.

--

----- Dig the NEW and IMPROVED news sig!! -----

-------------- Shaggy was here! ---------------
Ain't I'm a dawg!!

Re: memcpy with NULL pointer

<slu7mp$v2k$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18855&group=comp.lang.c#18855

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: memcpy with NULL pointer
Date: Wed, 3 Nov 2021 10:52:07 -0400
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <slu7mp$v2k$1@dont-email.me>
References: <slgdn2$1v7q$1@gioia.aioe.org> <86ee84f70q.fsf@linuxsc.com>
<ue195i-892.ln1@aretha.foo>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 3 Nov 2021 14:52:09 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="1ab98d2828418c7ffff95c8af1da43b1";
logging-data="31828"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18XECQWhZcnLPDA/kPWeoWC/QMFD7/FN1g="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
Cancel-Lock: sha1:kNu2EuSAtwHvemP8+eNViw7Quh0=
In-Reply-To: <ue195i-892.ln1@aretha.foo>
Content-Language: en-US
 by: James Kuyper - Wed, 3 Nov 2021 14:52 UTC

On 11/2/21 8:24 PM, Peter 'Shaggy' Haywood wrote:
> Groovy hepcat Tim Rentsch was jivin' in comp.lang.c on Fri, 29 Oct 2021
> 09:40 pm. It's a cool scene! Dig it.
>
>> Steve Keller <keller.steve@gmx.de> writes:
>>
>>> I wonder whether calling the mem* and str* functions with a NULL
>>> pointer has defined bahavior if the count parameter is also 0,
>>> like in this call
>
> [Snip.]
>
>>> Since no memory access via the NULL pointer is done I'd assume
>>> this should not result in undefined behavior.
>>
>> That's a plausible assumption but not one that the C standard
>> supports.
>
> But, of course, undefined behaviour could be perfectly benign. Just
> don't count on it in every case.

"undefined behavior" means only that the C standard imposes no
requirements - if some other document does impose requirements, it's
fine to rely upon those requirements being met - so long as you know
that the document has authority over all of the systems to which you
might need to port your code. However, if you know of no such document,
or at least none that has authority over a particular system of interest
to you, you should not count on it, at all.

Re: memcpy with NULL pointer

<sluede$u71$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18859&group=comp.lang.c#18859

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Puiiztk9lHEEQC0y3uUjRA.user.46.165.242.75.POSTED!not-for-mail
From: non...@add.invalid (Manfred)
Newsgroups: comp.lang.c
Subject: Re: memcpy with NULL pointer
Date: Wed, 3 Nov 2021 17:46:37 +0100
Organization: Aioe.org NNTP Server
Message-ID: <sluede$u71$1@gioia.aioe.org>
References: <slgdn2$1v7q$1@gioia.aioe.org> <86ee84f70q.fsf@linuxsc.com>
<ue195i-892.ln1@aretha.foo> <slu7mp$v2k$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="30945"; posting-host="Puiiztk9lHEEQC0y3uUjRA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.2.1
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
 by: Manfred - Wed, 3 Nov 2021 16:46 UTC

On 11/3/2021 3:52 PM, James Kuyper wrote:
> On 11/2/21 8:24 PM, Peter 'Shaggy' Haywood wrote:
>> Groovy hepcat Tim Rentsch was jivin' in comp.lang.c on Fri, 29 Oct 2021
>> 09:40 pm. It's a cool scene! Dig it.
>>
>>> Steve Keller <keller.steve@gmx.de> writes:
>>>
>>>> I wonder whether calling the mem* and str* functions with a NULL
>>>> pointer has defined bahavior if the count parameter is also 0,
>>>> like in this call
>>
>> [Snip.]
>>
>>>> Since no memory access via the NULL pointer is done I'd assume
>>>> this should not result in undefined behavior.
>>>
>>> That's a plausible assumption but not one that the C standard
>>> supports.
>>
>> But, of course, undefined behaviour could be perfectly benign. Just
>> don't count on it in every case.
>
> "undefined behavior" means only that the C standard imposes no
> requirements - if some other document does impose requirements, it's
> fine to rely upon those requirements being met - so long as you know
> that the document has authority over all of the systems to which you
> might need to port your code. However, if you know of no such document,
> or at least none that has authority over a particular system of interest
> to you, you should not count on it, at all.
>

Especially considering that it's easy enough to write you own memcpy
that is robust against null pointers, although not heavily optimized.
On the other hand, aggressive optimization is the most likely motivation
for the requirement of valid pointers in all cases.

Re: memcpy with NULL pointer

<86zgqhaqxd.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18894&group=comp.lang.c#18894

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: memcpy with NULL pointer
Date: Sat, 06 Nov 2021 14:57:02 -0700
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <86zgqhaqxd.fsf@linuxsc.com>
References: <slgdn2$1v7q$1@gioia.aioe.org> <86ee84f70q.fsf@linuxsc.com> <ue195i-892.ln1@aretha.foo>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="352b810152e65a952f2faa649d7ab5f1";
logging-data="26974"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/I6WpccnxH1VOTm6qSbnzkjta8NFEl5Yc="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:xBIPwVkEnOhFJPeQyBIashYC1lg=
sha1:URILVPCWFlms00jjEj0r06YTJEc=
 by: Tim Rentsch - Sat, 6 Nov 2021 21:57 UTC

Peter 'Shaggy' Haywood <phaywood@alphalink.com.au> writes:

> Groovy hepcat Tim Rentsch was jivin' in comp.lang.c on Fri, 29 Oct 2021
> 09:40 pm. It's a cool scene! Dig it.
>
>> Steve Keller <keller.steve@gmx.de> writes:
>>
>>> I wonder whether calling the mem* and str* functions with a NULL
>>> pointer has defined bahavior if the count parameter is also 0,
>>> like in this call
>
> [Snip.]
>
>>> Since no memory access via the NULL pointer is done I'd assume
>>> this should not result in undefined behavior.
>>
>> That's a plausible assumption but not one that the C standard
>> supports.
>
> But, of course, undefined behaviour could be perfectly benign. Just
> don't count on it in every case.

Bad advice in this particular case.

Re: memcpy with NULL pointer

<sniagb$e3g$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19158&group=comp.lang.c#19158

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Io3YJcW+bIl0vIqWMYAJPw.user.46.165.242.75.POSTED!not-for-mail
From: keller.s...@gmx.de (Steve Keller)
Newsgroups: comp.lang.c
Subject: Re: memcpy with NULL pointer
Date: Tue, 23 Nov 2021 09:58:50 +0100
Organization: Aioe.org NNTP Server
Message-ID: <sniagb$e3g$1@gioia.aioe.org>
References: <slgdn2$1v7q$1@gioia.aioe.org> <86ee84f70q.fsf@linuxsc.com> <slhno6$pe$1@gioia.aioe.org> <v_ZeJ.6340$QB1.5143@fx42.iad>
Injection-Info: gioia.aioe.org; logging-data="14448"; posting-host="Io3YJcW+bIl0vIqWMYAJPw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Steve Keller - Tue, 23 Nov 2021 08:58 UTC

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

> We actually looked into adding EFAULT as an error to the mem* and str*
> functions back in the 90's. Ultimately the overhead to validate
> the pointers was too great (absent dedicated hardware support such
> as CHERI[*]) and the proposal was discarded.

Doing a check and returning EFAULT also feels wrong for me. I think
it's best when these cases simply are no special corner cases. No
expensive check, no error, no UB or implementation defined behavior.
Just do the equivalent of

memcpy(void *dst, const void *src, size_t n) {
char *d = dst;
const char *s = src;
while (n-- > 0)
*d++ = *s++;
}

For n == 0, the value of src and dst doesn't matter. There's no
special definition of the behavior, it's just that 0 items beginning
at dst or src are accessed and therefore the pointers are used to
access anything at all.

And I don't see any optimizations that are only possible if these
special cases are left undefined.

BTW, I have also always hated in math lectures when definitions using
e.g. subsets defined the term in question in a way that excluded the
empty set and/or the full set. This often required handling theses
excluded cases specially in many theorems and proofs. And simplifying
the definition to not exclude some cases would also simplify all
theorems based on it, and everything would just look cleaner.

Steve

Re: memcpy with NULL pointer

<snic3l$1653$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19159&group=comp.lang.c#19159

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Io3YJcW+bIl0vIqWMYAJPw.user.46.165.242.75.POSTED!not-for-mail
From: keller.s...@gmx.de (Steve Keller)
Newsgroups: comp.lang.c
Subject: Re: memcpy with NULL pointer
Date: Tue, 23 Nov 2021 10:26:13 +0100
Organization: Aioe.org NNTP Server
Message-ID: <snic3l$1653$1@gioia.aioe.org>
References: <slgdn2$1v7q$1@gioia.aioe.org> <86ee84f70q.fsf@linuxsc.com> <slhno6$pe$1@gioia.aioe.org> <86bl37e17c.fsf@linuxsc.com> <sljrlk$e21$1@gioia.aioe.org>
Injection-Info: gioia.aioe.org; logging-data="39075"; posting-host="Io3YJcW+bIl0vIqWMYAJPw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Steve Keller - Tue, 23 Nov 2021 09:26 UTC

Guillaume <message@bottle.org> writes:

> > Rather than wishing the C standard would be different than it is,
> > avoid the problem by using wrapper functions (disclaimer: not
> > compiled):
>
> Yes of course. It's straighforward and this way, you get the checks if
> you want/need them instread of imposing them on everyone regardless of
> the use case.

I wouldn't propose additional checks. Just define the behavior
naturally, i.e. if the size arguement is 0, it means 0 elements
starting at the pointed adresses are accessed, so the pointers don't
matter at all.

> Those std functions should be considered "low-level" functions.
> I for one frankly don't care if any of the above examples, like
> "memcmp(NULL, NULL, 0)" are UB. If you are bound to call those with
> such parameters, there's something wrong with your coding style
> IMHO.

Not in my opinion. There are often cases where the size of a dynamic
array to be allocated and then worked on, is calculated from other
values and 0 is a possible outcome[1]. This is, of cource some
special case, but often the algorithms work the same on these special
cases and no special handling would be required if the functions from
the standard lib like malloc() and memcpy() wouldn't behave specially.

[1] Even if sometimes these are not very useful use-cases of the
software, I prefer no to add arbitrary and unneeded limits to the
range of valid arguments, making the code more complex and less
general.

> And now, in particular cases for which it would make sense
> (because it could make your code more elegant or something), then you
> can use wrappers as suggested above.

Yes, and I will do this. But for the reader of the code it means, he
sees calls to safe_memcpy() et al. and has to first lookup where and
how they are defined instead of just seeing memcpy() that everyone
knows.

I know that's not that big a deal and there are more complex things in
the software field to be solved, but still I'd like more simple, clean,
and elegant low-level libs to work with.

Steve

Re: memcpy with NULL pointer

<6c11b24b-2f0c-4f0e-8c18-e34023f3fe59n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19583&group=comp.lang.c#19583

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:301:: with SMTP id s1mr46672qkm.771.1640035925615;
Mon, 20 Dec 2021 13:32:05 -0800 (PST)
X-Received: by 2002:a05:620a:94c:: with SMTP id w12mr107340qkw.190.1640035925460;
Mon, 20 Dec 2021 13:32:05 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Mon, 20 Dec 2021 13:32:05 -0800 (PST)
In-Reply-To: <snic3l$1653$1@gioia.aioe.org>
Injection-Info: google-groups.googlegroups.com; posting-host=108.54.83.172; posting-account=BtBOEwoAAAAFZDKynGutKL1bxHMXo3Ay
NNTP-Posting-Host: 108.54.83.172
References: <slgdn2$1v7q$1@gioia.aioe.org> <86ee84f70q.fsf@linuxsc.com>
<slhno6$pe$1@gioia.aioe.org> <86bl37e17c.fsf@linuxsc.com> <sljrlk$e21$1@gioia.aioe.org>
<snic3l$1653$1@gioia.aioe.org>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <6c11b24b-2f0c-4f0e-8c18-e34023f3fe59n@googlegroups.com>
Subject: Re: memcpy with NULL pointer
From: dolfila...@gmail.com (Dolores Filandro)
Injection-Date: Mon, 20 Dec 2021 21:32:05 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 11
 by: Dolores Filandro - Mon, 20 Dec 2021 21:32 UTC

> I wouldn't propose additional checks. Just define the behavior
> naturally, i.e. if the size arguement is 0, it means 0 elements
> starting at the pointed adresses are accessed, so the pointers don't
> matter at all.

For string functions such as memmove and strncat,
there may be work which needs to be done before any copying takes place,
which would need valid pointers to objects even if no copying takes place.

An implementer could program around that
but the need for string functions to be able to take NULL arguments isn't strong.

Re: memcpy with NULL pointer

<7cee7cab-2957-44a7-94c1-3d69a68efbacn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19591&group=comp.lang.c#19591

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:115:: with SMTP id u21mr299204qtw.472.1640048165658;
Mon, 20 Dec 2021 16:56:05 -0800 (PST)
X-Received: by 2002:a05:622a:11ce:: with SMTP id n14mr550665qtk.432.1640048165515;
Mon, 20 Dec 2021 16:56:05 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Mon, 20 Dec 2021 16:56:05 -0800 (PST)
In-Reply-To: <6c11b24b-2f0c-4f0e-8c18-e34023f3fe59n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=94.246.251.164; posting-account=pysjKgkAAACLegAdYDFznkqjgx_7vlUK
NNTP-Posting-Host: 94.246.251.164
References: <slgdn2$1v7q$1@gioia.aioe.org> <86ee84f70q.fsf@linuxsc.com>
<slhno6$pe$1@gioia.aioe.org> <86bl37e17c.fsf@linuxsc.com> <sljrlk$e21$1@gioia.aioe.org>
<snic3l$1653$1@gioia.aioe.org> <6c11b24b-2f0c-4f0e-8c18-e34023f3fe59n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <7cee7cab-2957-44a7-94c1-3d69a68efbacn@googlegroups.com>
Subject: Re: memcpy with NULL pointer
From: oot...@hot.ee (Öö Tiib)
Injection-Date: Tue, 21 Dec 2021 00:56:05 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 17
 by: Öö Tiib - Tue, 21 Dec 2021 00:56 UTC

On Monday, 20 December 2021 at 23:32:12 UTC+2, Dolores Filandro wrote:
> > I wouldn't propose additional checks. Just define the behavior
> > naturally, i.e. if the size arguement is 0, it means 0 elements
> > starting at the pointed adresses are accessed, so the pointers don't
> > matter at all.
>
> For string functions such as memmove and strncat,
> there may be work which needs to be done before any copying takes place,
> which would need valid pointers to objects even if no copying takes place.
>
> An implementer could program around that
> but the need for string functions to be able to take NULL arguments isn't strong.

So the functions are expected to be designed inefficient for situation where
zero bytes are needed to be moved or copied ... as these do some additional
work? That indicates need for wrapper even on cases when all the pointers
are valid just the size argument is 0 to get rid of that additional work
overhead in standard library.

Re: memcpy with NULL pointer

<bfawJ.156416$831.50997@fx40.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19595&group=comp.lang.c#19595

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!news-out.netnews.com!news.alt.net!fdc2.netnews.com!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx40.iad.POSTED!not-for-mail
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0)
Gecko/20100101 Thunderbird/91.4.0
Subject: Re: memcpy with NULL pointer
Content-Language: en-US
Newsgroups: comp.lang.c
References: <slgdn2$1v7q$1@gioia.aioe.org> <86ee84f70q.fsf@linuxsc.com>
<slhno6$pe$1@gioia.aioe.org> <86bl37e17c.fsf@linuxsc.com>
<sljrlk$e21$1@gioia.aioe.org> <snic3l$1653$1@gioia.aioe.org>
<6c11b24b-2f0c-4f0e-8c18-e34023f3fe59n@googlegroups.com>
<7cee7cab-2957-44a7-94c1-3d69a68efbacn@googlegroups.com>
From: Rich...@Damon-Family.org (Richard Damon)
In-Reply-To: <7cee7cab-2957-44a7-94c1-3d69a68efbacn@googlegroups.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Lines: 42
Message-ID: <bfawJ.156416$831.50997@fx40.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: Mon, 20 Dec 2021 20:39:20 -0500
X-Received-Bytes: 3257
 by: Richard Damon - Tue, 21 Dec 2021 01:39 UTC

On 12/20/21 7:56 PM, Öö Tiib wrote:
> On Monday, 20 December 2021 at 23:32:12 UTC+2, Dolores Filandro wrote:
>>> I wouldn't propose additional checks. Just define the behavior
>>> naturally, i.e. if the size arguement is 0, it means 0 elements
>>> starting at the pointed adresses are accessed, so the pointers don't
>>> matter at all.
>>
>> For string functions such as memmove and strncat,
>> there may be work which needs to be done before any copying takes place,
>> which would need valid pointers to objects even if no copying takes place.
>>
>> An implementer could program around that
>> but the need for string functions to be able to take NULL arguments isn't strong.
>
> So the functions are expected to be designed inefficient for situation where
> zero bytes are needed to be moved or copied ... as these do some additional
> work? That indicates need for wrapper even on cases when all the pointers
> are valid just the size argument is 0 to get rid of that additional work
> overhead in standard library.

They are ALLOWED to be inefficient for the zero byte case, and it is
allowed for the system to be hostile to treating NULL pointers casually.

For instance, it might have special registers for using pointers as
addresses and manipulating them, but use the 'normal' registers for just
checking for a NULL pointer, and loading a NULL pointer into the special
registers traps.

If the code begins with some alignment checks, this might trap on doing
this on a NULL pointer.

This leads to either ALL uses need to pay the penalty of checking for
zero byte transfers, or we need to restrict using NULL pointers as
parameters.

Since if the application wants the skip on 0 behavior, it can easily
write a wrapper function (and even make it inline to negate the
additional cost) while it is impossible to remove the cost for the check
if it was added in the library, says it makes sense to write the
requirements to allow the most efficient library code.

This is common thinking in the C library design.

Re: memcpy with NULL pointer

<ee8dd0bb-e5cf-4fda-9b2b-79c403cb7ab5n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19599&group=comp.lang.c#19599

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:1652:: with SMTP id y18mr746662qtj.63.1640053890919;
Mon, 20 Dec 2021 18:31:30 -0800 (PST)
X-Received: by 2002:a05:622a:4c:: with SMTP id y12mr755698qtw.21.1640053890775;
Mon, 20 Dec 2021 18:31:30 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Mon, 20 Dec 2021 18:31:30 -0800 (PST)
In-Reply-To: <bfawJ.156416$831.50997@fx40.iad>
Injection-Info: google-groups.googlegroups.com; posting-host=94.246.251.164; posting-account=pysjKgkAAACLegAdYDFznkqjgx_7vlUK
NNTP-Posting-Host: 94.246.251.164
References: <slgdn2$1v7q$1@gioia.aioe.org> <86ee84f70q.fsf@linuxsc.com>
<slhno6$pe$1@gioia.aioe.org> <86bl37e17c.fsf@linuxsc.com> <sljrlk$e21$1@gioia.aioe.org>
<snic3l$1653$1@gioia.aioe.org> <6c11b24b-2f0c-4f0e-8c18-e34023f3fe59n@googlegroups.com>
<7cee7cab-2957-44a7-94c1-3d69a68efbacn@googlegroups.com> <bfawJ.156416$831.50997@fx40.iad>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <ee8dd0bb-e5cf-4fda-9b2b-79c403cb7ab5n@googlegroups.com>
Subject: Re: memcpy with NULL pointer
From: oot...@hot.ee (Öö Tiib)
Injection-Date: Tue, 21 Dec 2021 02:31:30 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 77
 by: Öö Tiib - Tue, 21 Dec 2021 02:31 UTC

On Tuesday, 21 December 2021 at 03:39:31 UTC+2, Richard Damon wrote:
> On 12/20/21 7:56 PM, Öö Tiib wrote:
> > On Monday, 20 December 2021 at 23:32:12 UTC+2, Dolores Filandro wrote:
> >>> I wouldn't propose additional checks. Just define the behavior
> >>> naturally, i.e. if the size arguement is 0, it means 0 elements
> >>> starting at the pointed adresses are accessed, so the pointers don't
> >>> matter at all.
> >>
> >> For string functions such as memmove and strncat,
> >> there may be work which needs to be done before any copying takes place,
> >> which would need valid pointers to objects even if no copying takes place.
> >>
> >> An implementer could program around that
> >> but the need for string functions to be able to take NULL arguments isn't strong.
> >
> > So the functions are expected to be designed inefficient for situation where
> > zero bytes are needed to be moved or copied ... as these do some additional
> > work? That indicates need for wrapper even on cases when all the pointers
> > are valid just the size argument is 0 to get rid of that additional work
> > overhead in standard library.
>
> They are ALLOWED to be inefficient for the zero byte case, and it is
> allowed for the system to be hostile to treating NULL pointers casually.

Yes standard allows whatever amount of inefficiency as it is not its concern
to deal with issues that market takes care of anyway. Programmers will
avoid inefficient implementations.

> For instance, it might have special registers for using pointers as
> addresses and manipulating them, but use the 'normal' registers for just
> checking for a NULL pointer, and loading a NULL pointer into the special
> registers traps.

My question was about valid non-NULL pointers and additional work done
with those despite the size to copy or move is 0. No competent user
passes NULL pointers there anyway so NULL is red herring.

> If the code begins with some alignment checks, this might trap on doing
> this on a NULL pointer.

Alignment checks are good example of additional work that is done
but later discarded as being not worth doing at all.

> This leads to either ALL uses need to pay the penalty of checking for
> zero byte transfers, or we need to restrict using NULL pointers as
> parameters.

All users pay that penalty if doing too lot of zero byte transfers, period.
NULL is red herring.

> Since if the application wants the skip on 0 behavior, it can easily
> write a wrapper function (and even make it inline to negate the
> additional cost) while it is impossible to remove the cost for the check
> if it was added in the library, says it makes sense to write the
> requirements to allow the most efficient library code.
>
> This is common thinking in the C library design.

Modern programmers tend to expect compilers to optimize out calls
that do nothing when capable to predict that ... not to add additional
unexpected works. Programmers will avoid inefficient implementations
and so the inefficiency is expected only on platforms compiling for
what is controlled by monopoly ... like iOS devices and such.

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor