[PATCH] Avoid zero-byte memcpy to NULL in raw_ostream

Jordan Rose jordan_rose at apple.com
Fri Mar 29 14:34:17 PDT 2013


Hi chandlerc,

I've been running the static analyzer over LLVM with experimental destructor support enabled. It produces a warning (actually many duplicate warnings) in raw_ostream.h about a null argument to memcpy, which happens in `operator<<(StringRef)` when the stream is unbuffered and the string to print is empty.

I don't actually know of any platforms that implement memcpy in such a way that a zero-byte copy to NULL would be problematic, but it's technically undefined behavior. This patch fixes that at the cost of an extra branch in raw_ostream...which will probably be optimized and predicted down to almost nothing anyway.

(We do have a bug outstanding to silence string warnings if the access length is 0, but LLVM often tries to avoid undefined behavior anyway.)

C11 7.24.1p2: 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.

C11 7.1.4p1: [...] 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. [...]

http://llvm-reviews.chandlerc.com/D596

Files:
  include/llvm/Support/raw_ostream.h

Index: include/llvm/Support/raw_ostream.h
===================================================================
--- include/llvm/Support/raw_ostream.h
+++ include/llvm/Support/raw_ostream.h
@@ -154,6 +154,8 @@
   raw_ostream &operator<<(StringRef Str) {
     // Inline fast path, particularly for strings with a known length.
     size_t Size = Str.size();
+    if (Size == 0)
+      return;
 
     // Make sure we can use the fast path.
     if (OutBufCur+Size > OutBufEnd)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D596.1.patch
Type: text/x-patch
Size: 473 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130329/b060b714/attachment.bin>


More information about the llvm-commits mailing list