Skip to content

Commit

Permalink
Merge pull request #5365: buffer: critical bufferlist::zero bug
Browse files Browse the repository at this point in the history
Reviewed-by: Loic Dachary <ldachary@redhat.com>
  • Loading branch information
ldachary committed Aug 30, 2015
2 parents 0e9f427 + 0fde3a2 commit 8f58be4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/common/buffer.cc
Expand Up @@ -1165,12 +1165,23 @@ static simple_spinlock_t buffer_debug_lock = SIMPLE_SPINLOCK_INITIALIZER;
it != _buffers.end();
++it) {
if (p + it->length() > o) {
if (p >= o && p+it->length() <= o+l)
it->zero(); // all
else if (p >= o)
it->zero(0, o+l-p); // head
else
it->zero(o-p, it->length()-(o-p)); // tail
if (p >= o && p+it->length() <= o+l) {
// 'o'------------- l -----------|
// 'p'-- it->length() --|
it->zero();
} else if (p >= o) {
// 'o'------------- l -----------|
// 'p'------- it->length() -------|
it->zero(0, o+l-p);
} else if (p + it->length() <= o+l) {
// 'o'------------- l -----------|
// 'p'------- it->length() -------|
it->zero(o-p, it->length()-(o-p));
} else {
// 'o'----------- l -----------|
// 'p'---------- it->length() ----------|
it->zero(o-p, l);
}
}
p += it->length();
if (o+l <= p)
Expand Down
11 changes: 11 additions & 0 deletions src/test/bufferlist.cc
Expand Up @@ -2221,6 +2221,17 @@ TEST(BufferList, zero) {
bl.zero((unsigned)3, (unsigned)3);
EXPECT_EQ(0, ::memcmp("ABC\0\0\0GHIKLM", bl.c_str(), 9));
}
{
bufferlist bl;
bufferptr ptr1(4);
bufferptr ptr2(4);
memset(ptr1.c_str(), 'a', 4);
memset(ptr2.c_str(), 'b', 4);
bl.append(ptr1);
bl.append(ptr2);
bl.zero((unsigned)2, (unsigned)4);
EXPECT_EQ(0, ::memcmp("aa\0\0\0\0bb", bl.c_str(), 8));
}
}

TEST(BufferList, EmptyAppend) {
Expand Down

0 comments on commit 8f58be4

Please sign in to comment.