using detail::Writable<RWCursor<access>>::pushAtMost;
size_t pushAtMost(const uint8_t* buf, size_t len) {
+ // We have to explicitly check for an input length of 0.
+ // We support buf being nullptr in this case, but we need to avoid calling
+ // memcpy() with a null source pointer, since that is undefined behavior
+ // even if the length is 0.
+ if (len == 0) {
+ return 0;
+ }
+
size_t copied = 0;
for (;;) {
// Fast path: the current buffer is big enough.
using detail::Writable<Appender>::pushAtMost;
size_t pushAtMost(const uint8_t* buf, size_t len) {
+ // We have to explicitly check for an input length of 0.
+ // We support buf being nullptr in this case, but we need to avoid calling
+ // memcpy() with a null source pointer, since that is undefined behavior
+ // even if the length is 0.
+ if (len == 0) {
+ return 0;
+ }
+
size_t copied = 0;
for (;;) {
// Fast path: it all fits in one buffer.
EXPECT_EQ(1, rcursor2.readBE<uint64_t>());
EXPECT_EQ(10, rcursor2.readBE<uint64_t>());
EXPECT_EQ(20, rcursor2.readBE<uint32_t>());
-
}
TEST(IOBuf, Gather) {
EXPECT_THROW(rcursor.read<uint32_t>(), std::out_of_range);
EXPECT_EQ(0, rcursor.totalLength());
}
+
+TEST(IOBuf, pushEmptyByteRange) {
+ // Test pushing an empty ByteRange. This mainly tests that we do not
+ // trigger UBSAN warnings by calling memcpy() with an null source pointer,
+ // which is undefined behavior even if the length is 0.
+ IOBuf buf{IOBuf::CREATE, 2};
+ ByteRange emptyBytes;
+
+ // Test calling Cursor::push()
+ RWPrivateCursor wcursor(&buf);
+ wcursor.push(emptyBytes);
+ EXPECT_EQ(0, buf.computeChainDataLength());
+
+ // Test calling Appender::push()
+ Appender app(&buf, 16);
+ app.push(emptyBytes);
+ EXPECT_EQ(0, buf.computeChainDataLength());
+}