Commit 86cefd11 authored by Stepan Palamarchuk's avatar Stepan Palamarchuk Committed by Facebook Github Bot

Properly handle appending to the tail of the chain

Summary: Currently appending to the tail of the chain would cause the cursor advancing to the beginning of the chain, which is not correct, instead we should advance to the tail.

Reviewed By: yfeldblum

Differential Revision: D6734999

fbshipit-source-id: b8b2585e0475b656f7b6bf4ed39686e2ccb2e432
parent 9bf26065
......@@ -888,12 +888,21 @@ class RWCursor
this->absolutePos_ += this->crtPos_ - this->crtBegin_;
this->crtBuf_->appendChain(std::move(buf));
if (nextBuf == this->buffer_) {
// We've just appended to the end of the buffer, so advance to the end.
this->crtBuf_ = this->buffer_->prev();
this->crtBegin_ = this->crtBuf_->data();
this->crtPos_ = this->crtEnd_ = this->crtBuf_->tail();
// This has already been accounted for, so remove it.
this->absolutePos_ -= this->crtEnd_ - this->crtBegin_;
} else {
// Jump past the new links
this->crtBuf_ = nextBuf;
this->crtPos_ = this->crtBegin_ = this->crtBuf_->data();
this->crtEnd_ = this->crtBuf_->tail();
}
}
}
uint8_t* writableData() {
return this->crtBuf_->writableData() + (this->crtPos_ - this->crtBegin_);
......
......@@ -426,6 +426,22 @@ TEST(IOBuf, cloneAndInsert) {
// Check that nextBuf got set correctly
cursor.read<uint8_t>();
}
{
// Check that inserting at the end of the buffer keeps it at the end.
RWPrivateCursor cursor(iobuf1.get());
Cursor(iobuf1.get()).clone(cloned, 1);
EXPECT_EQ(1, cloned->countChainElements());
EXPECT_EQ(1, cloned->computeChainDataLength());
cursor.advanceToEnd();
EXPECT_EQ(17, cursor.getCurrentPosition());
cursor.insert(std::move(cloned));
EXPECT_EQ(18, cursor.getCurrentPosition());
EXPECT_EQ(0, cursor.totalLength());
EXPECT_EQ(12, iobuf1->countChainElements());
EXPECT_EQ(18, iobuf1->computeChainDataLength());
EXPECT_TRUE(cursor.isAtEnd());
}
}
TEST(IOBuf, cloneWithEmptyBufAtStart) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment