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


Groups > comp.lang.c > #163253

Re: memcpy with NULL pointer

From Steve Keller <keller.steve@gmx.de>
Newsgroups comp.lang.c
Subject Re: memcpy with NULL pointer
Date 2021-10-29 23:06 +0200
Organization Aioe.org NNTP Server
Message-ID <slhno6$pe$1@gioia.aioe.org> (permalink)
References <slgdn2$1v7q$1@gioia.aioe.org> <86ee84f70q.fsf@linuxsc.com>

Show all headers | View raw


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

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


Thread

memcpy with NULL pointer Steve Keller <keller.steve@gmx.de> - 2021-10-29 11:08 +0200
  Re: memcpy with NULL pointer Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-10-29 03:40 -0700
    Re: memcpy with NULL pointer Steve Keller <keller.steve@gmx.de> - 2021-10-29 23:06 +0200
      Re: memcpy with NULL pointer Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-10-29 14:23 -0700
      Re: memcpy with NULL pointer scott@slp53.sl.home (Scott Lurndal) - 2021-10-29 21:48 +0000
        Re: memcpy with NULL pointer Steve Keller <keller.steve@gmx.de> - 2021-11-23 09:58 +0100
      Re: memcpy with NULL pointer Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-10-29 18:44 -0700
        Re: memcpy with NULL pointer Guillaume <message@bottle.org> - 2021-10-30 18:25 +0200
          Re: memcpy with NULL pointer Steve Keller <keller.steve@gmx.de> - 2021-11-23 10:26 +0100
            Re: memcpy with NULL pointer Dolores Filandro <dolfiland8@gmail.com> - 2021-12-20 13:32 -0800
              Re: memcpy with NULL pointer Öö Tiib <ootiib@hot.ee> - 2021-12-20 16:56 -0800
                Re: memcpy with NULL pointer Richard Damon <Richard@Damon-Family.org> - 2021-12-20 20:39 -0500
                Re: memcpy with NULL pointer Öö Tiib <ootiib@hot.ee> - 2021-12-20 18:31 -0800
    Re: memcpy with NULL pointer Peter 'Shaggy' Haywood <phaywood@alphalink.com.au> - 2021-11-03 11:24 +1100
      Re: memcpy with NULL pointer James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-11-03 10:52 -0400
        Re: memcpy with NULL pointer Manfred <noname@add.invalid> - 2021-11-03 17:46 +0100
      Re: memcpy with NULL pointer Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-11-06 14:57 -0700

csiph-web