What’s the difference between the following length and pointer pairs?
In many circumstances, all five result in identical behavior. Other times, the behavior can vary wildly. One obvious difference is whether the pointer can be passed to free, though I’m going to ignore that difference for the rest of this post.
The first case is interesting, but sufficiently different to postone discussion for later.
The behavior of malloc(0) is implementation defined by the standard. The two legal possibilities are to return a null pointer or a unique pointer. In many implementations, the second option is accomplished by means of internally increasing the length to 1 (which is then likely rounded up to 16 or so). Legally, one cannot deference this pointer, although in practice some bytes are allocated which means that won’t crash.
Returning NULL introduces the possibility of an interesting programming error. Lots of code, including some I’ve written myself, assume that a NULL return from malloc is an error.
If len is zero, this will incorrectly report an error and exit unless an additional && len != 0 check is added. One may also subscribe to the “don’t check malloc” school of thought.
OpenBSD malloc handles zero specially in a different way. Zero sized allocations return chunks from pages that have been mprotected with PROT_NONE. Any attempt to dereference this pointer will crash.
Note that the unique pointer requirement prohibits an implementation from cheating.
That’s not a conforming implementation because successive calls will return the same value.
So case two is approximately the same as either case one or case three, depending on implementation.
Assuming malloc doesn’t fail, cases three, four, and five are approximately equal in most circumstances. The primary difference would be using sizeof(ptr) / sizeof(ptr), such as in a loop construct. That either gives the wrong answer, the right answer, or no answer (compilation failure).
Case four may not be technically permitted by the standard, but compilers will likely eat it anyway.
The biggest difference between these cases and the first case is that they will pass a null check, which can have semantic meaning. It’s the difference between an empty array and a missing array. Similarly, although it takes one byte of storage, the empty string is not a null string.
Now we return to the discussion of case one and null objects. Consider this call.
Set 0 bytes of ptr to 0. Which of the five pointers above make this a valid call? Three, four, and five for certain. Two if it’s a unique pointer. But what if ptr is NULL?
The C standard has this to say in the section on “Use of library functions”.
The section on “String function conventions” clarifies further.
Hmmm. Sounds like memsetting 0 bytes of NULL is undefined. None of memset, memcpy, or memmove are documented to accept null pointers. As a counter example, however, the description for snprintf does say “If n is zero, nothing is written, and s may be a null pointer.” The POSIX read function similarly states that zero length reads are not an error, but an implementation may in that case still check other parameters for errors such as invalid buffer pointers.
What does this mean in practice? The most straightforward way to implement functions like memset or memcpy handles the zero length case by simply not entering the loop and not doing anything. Usually undefined behavior in C rears its head when something happens, but in this case it’s already been defined that nothing happens with normal pointers. Checking for abnormal pointers would require more work.
Detecting non-null abnormal pointers is usually quite difficult. memcpy certainly doesn’t try, letting the program crash if the pointer is bad. The read system call also doesn’t check. Instead it defers checking to the copyout function which sets up a fault handler to detect errors. While a null check could be added, null pointers are no less invalid to these functions than 0x1 or 0xffffffff pointers for which no special handling exists.
What this means in practice is lots of code which (deliberately or inadvertently) assumes a null pointer and a zero length is valid. In response to a discussion at the s2k15 hackathon, I decided to run an experiment. I modified memcpy to log an error and abort when either pointer was NULL and installed a new libc.
Well, that didn’t take long. Wonder what it’s doing?
And, uh, ok. This is obviously going to get real exciting real fast. Revert.
I started down this road because the intersection of “undefined but should work” code and optimizing C compilers has not been pleasant. A smart C compiler may observe a call to memcpy, flag both pointers as valid, and then delete any null checks. Forwards and backwards.
The above code will obviously misbehave if the null check is deleted by the compiler and a null pointer is passed.
I had some concerns about this because in the past similar dereference then check constructs have been optimized into security vulnerabilities. That’s bad news for a lot of software not ready for this level of standards compliance.
At first, I was unable to coax the compiler into deleting a null check after a memcpy “dereference”, but that’s not insurance it won’t happen. gcc 4.9 says it will optimize the check away. The OpenBSD package of gcc 4.9 (which contains various patches) won’t delete the check by default, even at -O3, but enabling -fdelete-null-pointer-checks results in the check being deleted. Not sure yet where clang stands. Initial testing says no, but that’s no guarantee. In theory, it can perform this optimization too.