You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@stdcxx.apache.org by Eric Lemings <Er...@roguewave.com> on 2008/04/11 02:42:06 UTC

Null src pointer in memcpy?

 
Is it safe to pass a null pointer as the 2nd argument to memcpy()?  Or
undefined?
 
Brad.
 

Re: Null src pointer in memcpy?

Posted by Martin Sebor <se...@roguewave.com>.
Eric Lemings wrote:
>  
> I was just asking because I observed such a call in _rw_bufcat().
> It happens when the printf-like functions in tests/src/printf.cpp
> are creating a new buffer for formatting operations.

Fix it :)

> 
> Brad.
> 
>> -----Original Message-----
>> From: Martin Sebor [mailto:msebor@gmail.com] On Behalf Of Martin Sebor
>> Sent: Thursday, April 10, 2008 7:40 PM
>> To: dev@stdcxx.apache.org
>> Subject: Re: Null src pointer in memcpy?
>>
>> This is explicitly required in 7.1.4 of C99:
>>
>>        7.1.4 Use of library functions
>>
>>    -1- Each of the following statements applies unless 
>> explicitly stated
>>        otherwise in the detailed descriptions that follow: If 
>> an argument
>>        to a function has an invalid value (such as a value outside the
>>        domain of the function, or a pointer outside the address space
>>        of the program, or a null pointer, or a pointer to 
>> non-modifiable
>>        storage when the corresponding parameter is not 
>> const-qualified)
>>        or a type (after promotion) not expected by a function with
>>        variable number of arguments, the behavior is undefined. ...
>>
>> Here's an answer to the same question on comp.lang.c.moderated:
>>    http://tinyurl.com/6eqo3n
>>
>> Martin Sebor wrote:
>>> Eric Lemings wrote:
>>>>  
>>>> Is it safe to pass a null pointer as the 2nd argument to 
>> memcpy()?  Or
>>>> undefined?
>>> Assuming the third argument is 0. Strictly speaking I believe it's
>>> undefined because memcpy(s1, s2, n) is specified to "copy n bytes
>>> from the object pointed to by s2 into the object pointed to by s1"
>>> and a null pointer doesn't point to an object. An object is defined
>>> as "a region of storage in the execution environment, the contents
>>> of which can represent values."
>>>
>>> AFAIK, most implementations allow null pointers for no-op calls to
>>> memcpy() but gcc issues a warning when it detects at compile time
>>> that a null pointer is passed as the first or second argument to
>>> memcpy().
>>>
>>> Martin
>>>
>>


RE: Null src pointer in memcpy?

Posted by Eric Lemings <Er...@roguewave.com>.
 
I was just asking because I observed such a call in _rw_bufcat().
It happens when the printf-like functions in tests/src/printf.cpp
are creating a new buffer for formatting operations.

Brad.

> -----Original Message-----
> From: Martin Sebor [mailto:msebor@gmail.com] On Behalf Of Martin Sebor
> Sent: Thursday, April 10, 2008 7:40 PM
> To: dev@stdcxx.apache.org
> Subject: Re: Null src pointer in memcpy?
> 
> This is explicitly required in 7.1.4 of C99:
> 
>        7.1.4 Use of library functions
> 
>    -1- Each of the following statements applies unless 
> explicitly stated
>        otherwise in the detailed descriptions that follow: If 
> an argument
>        to a function has an invalid value (such as a value outside the
>        domain of the function, or a pointer outside the address space
>        of the program, or a null pointer, or a pointer to 
> non-modifiable
>        storage when the corresponding parameter is not 
> const-qualified)
>        or a type (after promotion) not expected by a function with
>        variable number of arguments, the behavior is undefined. ...
> 
> Here's an answer to the same question on comp.lang.c.moderated:
>    http://tinyurl.com/6eqo3n
> 
> Martin Sebor wrote:
> > Eric Lemings wrote:
> >>  
> >> Is it safe to pass a null pointer as the 2nd argument to 
> memcpy()?  Or
> >> undefined?
> > 
> > Assuming the third argument is 0. Strictly speaking I believe it's
> > undefined because memcpy(s1, s2, n) is specified to "copy n bytes
> > from the object pointed to by s2 into the object pointed to by s1"
> > and a null pointer doesn't point to an object. An object is defined
> > as "a region of storage in the execution environment, the contents
> > of which can represent values."
> > 
> > AFAIK, most implementations allow null pointers for no-op calls to
> > memcpy() but gcc issues a warning when it detects at compile time
> > that a null pointer is passed as the first or second argument to
> > memcpy().
> > 
> > Martin
> > 
> 
> 

Re: Null src pointer in memcpy?

Posted by Martin Sebor <se...@roguewave.com>.
This is explicitly required in 7.1.4 of C99:

       7.1.4 Use of library functions

   -1- Each of the following statements applies unless explicitly stated
       otherwise in the detailed descriptions that follow: If an argument
       to a function has an invalid value (such as a value outside the
       domain of the function, or a pointer outside the address space
       of the program, or a null pointer, or a pointer to non-modifiable
       storage when the corresponding parameter is not const-qualified)
       or a type (after promotion) not expected by a function with
       variable number of arguments, the behavior is undefined. ...

Here's an answer to the same question on comp.lang.c.moderated:
   http://tinyurl.com/6eqo3n

Martin Sebor wrote:
> Eric Lemings wrote:
>>  
>> Is it safe to pass a null pointer as the 2nd argument to memcpy()?  Or
>> undefined?
> 
> Assuming the third argument is 0. Strictly speaking I believe it's
> undefined because memcpy(s1, s2, n) is specified to "copy n bytes
> from the object pointed to by s2 into the object pointed to by s1"
> and a null pointer doesn't point to an object. An object is defined
> as "a region of storage in the execution environment, the contents
> of which can represent values."
> 
> AFAIK, most implementations allow null pointers for no-op calls to
> memcpy() but gcc issues a warning when it detects at compile time
> that a null pointer is passed as the first or second argument to
> memcpy().
> 
> Martin
> 


Re: Null src pointer in memcpy?

Posted by Martin Sebor <se...@roguewave.com>.
Eric Lemings wrote:
>  
> Is it safe to pass a null pointer as the 2nd argument to memcpy()?  Or
> undefined?

Assuming the third argument is 0. Strictly speaking I believe it's
undefined because memcpy(s1, s2, n) is specified to "copy n bytes
from the object pointed to by s2 into the object pointed to by s1"
and a null pointer doesn't point to an object. An object is defined
as "a region of storage in the execution environment, the contents
of which can represent values."

AFAIK, most implementations allow null pointers for no-op calls to
memcpy() but gcc issues a warning when it detects at compile time
that a null pointer is passed as the first or second argument to
memcpy().

Martin


RE: Null src pointer in memcpy?

Posted by Travis Vitek <Tr...@roguewave.com>.

>-----Original Message-----
>From: Eric Lemings [mailto:Eric.Lemings@roguewave.com] 
>Sent: Thursday, April 10, 2008 5:42 PM
>To: dev@stdcxx.apache.org
>Subject: Null src pointer in memcpy?
>
> 
>Is it safe to pass a null pointer as the 2nd argument to memcpy()?  Or
>undefined?
>

The function is documented to copy n bytes from s2 into s1. If n is
non-zero, then it must attempt to copy that many bytes from s2. So it is
safe only when the third parameter is 0.

>Brad.
> 
>