Using malloc (0) and memcpy

I read somewhere: the dereferencing of the pointer returned by the "new" zero-size distribution is UB. Is it the same in C? If yes, then the following UB code? (assuming size = 0)

a->object[index].data = malloc(size); memcpy(a->object[index].data, bytes, size); 

As far as I understand, no. Just wanted to double check.

+6
source share
3 answers

When you pass 0 as an argument to malloc , then the free allocated memory is for the pointer that malloc returns.

The result is determined by implementation.

C11: 7.22.3 Memory management functions:

[...] If the size of the requested space is zero, the behavior is determined by the implementation : a null pointer is returned or the behavior looks as if the size was a non-zero value, except that the returned pointer should not be used to access the object.

The standard also says:

The free function releases the space indicated by ptr , that is, available for further allocation. If ptr is a null pointer, no action occurs .

Thus, in any case of the implementation defined during the implementation, the release will not cause undefined behavior.

Now move on to another part of the question.

7.1.4 Using library functions:

If the function argument has an invalid value (for example, a value outside the function area or a pointer outside the program address space, or a null pointer or a pointer to unmodifiable storage when the corresponding parameter is not constant) or a type (after promotion) not expected by a function with a variable number of arguments , undefined behavior .

C11: 7.24.1 p (2):

If the argument declared as size_t n indicates the length of the array for function n, it may have a null value when this function is called. Unless explicitly indicated otherwise in the description of a particular function in this subclause, the arguments of the pointer pointer to such a call shall have valid values, as described in 7.1.4 . With this call, the function that finds the character does not find any occurrence, the function that compares the two sequences of characters returns zero, and the function that copies the characters copies the zero characters.

+9
source

According to my reading of the current standard (or n1570 , the final public draft of it), the code has Undefined Behavior:

7.24.1 Standard conventions

  1. If the argument declared as size_t n indicates the length of the array for the function, n may be zero when this function is called. Unless explicitly indicated otherwise in the description of a specific function in this subclause, the arguments of the pointer pointer on such a call shall have valid values, as described in 7.1.4 .

7.1.4 Using library functions

  • Each of the following statements applies, unless explicitly stated otherwise in the following detailed descriptions: If the function argument has an invalid value (for example, a value outside the function domain or a pointer outside the program address space or a null pointer or pointer to an unmodifiable storage, if the corresponding parameter is not constant) or type (after promotion), which is not expected to function with a variable number of arguments, the behavior is undefined .

(Emphasis added.)

+1
source
  size_t size = 0; a->object[index].data = malloc(size); memcpy(a->object[index].data, bytes, size); 

Technically, this behavior is undefined.

malloc can return a null pointer to distribute null bytes and pass a null pointer to memcpy undefined behavior (even if the third argument is 0 ).

From the mighty C-standard (emphasis mine):

(C99, 7.1.4p1) "[...] unless explicitly stated otherwise in the detailed descriptions: If the function argument has an invalid value (for example, a value outside the function domain or a pointer outside the program address space, or a null pointer , or a pointer to unmodifiable storage when the corresponding parameter is not const-qualified) or the type (after promotion) is not expected by a function with a variable number of arguments, the behavior is undefined "

+1
source

Source: https://habr.com/ru/post/972148/


All Articles