I want to understand if the following code is (always, sometimes or never) well-defined according to C11:
#include <string.h>
int main() {
char d[5];
char s[4] = "abc";
char *p = s;
strncpy(d, p, 4);
p += 4; // one-past end of "abc"
strncpy(d+4, p, 0); // is this undefined behavior?
return 0;
}
C11 7.24.2.4.2 says:
The strncpy function copies not more than n characters (characters that follow a null character are not copied) from the array pointed to by s2 to the array pointed to by s1.
Note that s2
is an array, not a string (so the lack of null-terminator when p == s+4
is not an issue).
7.24.1 (String function conventions) applies here (emphasis mine):
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.
The relevant part of the aforementioned 7.1.4 is (emphasis mine):
7.1.4 Use of library functions
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. If a function argument is described as being an array, the pointer actually passed to the function shall have a value such that all address computations and accesses to objects (that would be valid if the pointer did point to the first element of such an array) are in fact valid.
I'm having some trouble parsing the last part. The "all addresses computations and accesses to objects" seems to be trivially satisfied when n == 0
if I can suppose my implementation will not compute any addresses in this case.
In other words, in a strict interpretation of the standard, should I always refuse the program? Should I always allow it? Or is its correctness implementation-dependent (i.e., if the implementation computes the address of the first character before checking n
, then the above code has UB, otherwise it doesn't)?