lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


C99 specifies that it is fine to pass zero to memcpy as the size parameter.

 

7.24.1(2): 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. On such a call, a

function that locates a character finds no occurrence, a function that compares two

character sequences returns zero, and a function that copies characters copies zero

characters.

 

My question wasn't about passing zero as the size/length, it was about acceptable values for the associated memory pointers when the size was zero. On two different occasions I’ve had C runtimes that have faulted when passed a NULL pointer to (say) memcpy when the length was zero. I agree that this is not in accordance with standards, nor is it particularly sensible, but I wanted clarity on how Lua handled such a situation (since the specs are silent on this).

 

Pointers must be always non zero for memcmp. See for example [1] which has the relevant std-references.

Another point is that compilers start to optimize away null-pointer checks when this pointer was used in memcmp. [2]

 

- Jörg

 

[1] http://stackoverflow.com/questions/16362925/can-i-pass-a-null-pointer-to-memcmp

[2] https://gcc.gnu.org/ml/gcc-patches/2015-05/msg02707.html