Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #151216

Re: memcpy can't take null.

From Guillaume <message@bottle.org>
Newsgroups comp.lang.c
Subject Re: memcpy can't take null.
Date 2020-04-08 01:41 +0200
Organization Aioe.org NNTP Server
Message-ID <r6j33u$15ev$1@gioia.aioe.org> (permalink)
References <6c3de07e-15c8-4cc0-a805-636a5976d086@googlegroups.com> <r6i3vk$14n3$1@gioia.aioe.org> <87zhbnl10s.fsf@nosuchdomain.example.com>

Show all headers | View raw


Le 07/04/2020 à 18:21, Keith Thompson a écrit :
> Guillaume <message@bottle.org> writes:
>> Le 05/04/2020 à 15:47, Malcolm McLean a écrit :
>>> memcpy() cannot take null as a pointer parametrer, even if the size argument
>>> is zero.
>>
>> Most of the string/memory functions of the C std lib don't do any kind
>> of error checking. You may consider that passing memcpy a null pointer
>> AND a size of 0 is not an error per se, but I wouldn't necessarily
>> agree (along with probably many others.)
> 
> It's not as ambiguous as you make it sound.  Passing a null pointer to
> memcpy has undefined behavior.

None of what I said makes it ambiguous. I just said most of those 
functions don't do any error checking for invalid parameters. Nothing 
ambiguous about it.

> 
>      The memcpy function copies n characters from the object pointed to
>      by s2 into the object pointed to by s1.
> 
> If s1 or s2 doesn't point to an object, then that argument is invalid as
> described in 7.1.4.

Exactly. To me an invalid argument is an error. Then the underlying 
function is implemented with error checking (so the behavior is defined) 
or with no error checking (so the behavior is obviously undefined.)

Error checking here meant "parameter validity checking".

Of course, note that the case exposed by Malcolm is just a particular 
case that *could* have been handled by memcpy(). But the standard is 
more general, as it generally talks about "pointers not pointing to an 
object". Which of course means NULL, but also any other value that would 
happen to point to a random location that would not have been allocated 
properly, or that would have been free'd.

In that regard, the usefulness of checking for just NULL when any other 
value could also be not pointing to an object is admittedly 
questionable, and is probably the main reason null pointers are not 
checked explicitely in those functions.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

memcpy can't take null. Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-04-05 06:47 -0700
  Re: memcpy can't take null. Öö Tiib <ootiib@hot.ee> - 2020-04-05 07:30 -0700
    Re: memcpy can't take null. Richard Damon <Richard@Damon-Family.org> - 2020-04-05 14:39 -0400
      Re: memcpy can't take null. Philipp Klaus Krause <pkk@spth.de> - 2020-04-06 18:46 +0200
        Re: memcpy can't take null. Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-04-06 12:45 -0700
          Re: memcpy can't take null. Philipp Klaus Krause <pkk@spth.de> - 2020-04-07 07:28 +0200
  Re: memcpy can't take null. Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-04-05 16:00 -0700
    Re: memcpy can't take null. Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2020-04-05 18:29 -0600
      Re: memcpy can't take null. scott@slp53.sl.home (Scott Lurndal) - 2020-04-06 18:08 +0000
        Re: memcpy can't take null. Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2020-04-06 15:44 -0600
    Re: memcpy can't take null. Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-04-06 13:54 -0700
      Re: memcpy can't take null. Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-04-06 14:30 -0700
  Re: memcpy can't take null. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-04-06 01:20 +0100
  Re: memcpy can't take null. Siri Cruise <chine.bleu@yahoo.com> - 2020-04-05 19:09 -0700
  Re: memcpy can't take null. Guillaume <message@bottle.org> - 2020-04-07 16:50 +0200
    Re: memcpy can't take null. Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-04-07 09:19 -0700
    Re: memcpy can't take null. Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-04-07 09:21 -0700
      Re: memcpy can't take null. Guillaume <message@bottle.org> - 2020-04-08 01:41 +0200
        Re: memcpy can't take null. scott@slp53.sl.home (Scott Lurndal) - 2020-04-08 00:30 +0000
          Re: memcpy can't take null. Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-04-08 02:06 -0700
            Re: memcpy can't take null. Richard Damon <Richard@Damon-Family.org> - 2020-04-08 09:17 -0400
              Re: memcpy can't take null. Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-04-09 07:16 -0700
            Re: memcpy can't take null. Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-04-08 13:33 -0700
              Re: memcpy can't take null. Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-04-09 09:05 -0700
                Re: memcpy can't take null. Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-04-09 13:28 -0700
                Re: memcpy can't take null. richard@cogsci.ed.ac.uk (Richard Tobin) - 2020-04-09 23:41 +0000
          Re: memcpy can't take null. Guillaume <message@bottle.org> - 2020-04-08 16:17 +0200
    Re: memcpy can't take null. Ike Naar <ike@sdf.lonestar.org> - 2020-04-07 22:01 +0000
      Re: memcpy can't take null. Guillaume <message@bottle.org> - 2020-04-08 01:32 +0200

csiph-web