make io::Cursor::push() safe to call with an empty ByteRange
authorAdam Simpkins <simpkins@fb.com>
Tue, 27 Jun 2017 02:15:05 +0000 (19:15 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Tue, 27 Jun 2017 02:27:16 +0000 (19:27 -0700)
commit46c5dbce6c04234ae0185d3fadb62556c7e7625b
tree3b42dbdc0f069a3e6dcca085c5cbcc1613ea2d44
parent2734e379f411f7d9757f9b8d13c020249a6c2dd6
make io::Cursor::push() safe to call with an empty ByteRange

Summary:
Clang's UndefinedBehaviorSanitizer flagged an issue that pushAtMost() could
call `memcpy(dest, nullptr, 0)` if the input was an empty `ByteRange`.  A
default constructed `ByteRange` (or `StringPiece`) will be empty and both its
begin and end pointers will be null.  Unfortunately it is undefined behavior to
call `memcpy()` with a null source pointer even if the length is 0.

This updates the `Cursor` and `Appender` code to avoid calling `memcpy()` at
all when the input length is 0.

Reviewed By: yfeldblum

Differential Revision: D5322917

fbshipit-source-id: 67fce9579f97e7e93a5767b11cc5e43ff7be5876
folly/io/Cursor.h
folly/io/test/IOBufCursorTest.cpp