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


Groups > comp.lang.c > #163230

Re: memcpy with NULL pointer

Path csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From Tim Rentsch <tr.17687@z991.linuxsc.com>
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> (permalink)
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=
Xref csiph.com comp.lang.c:163230

Show key headers only | View raw


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.

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