From 7ee7dcfd609731d3c7f51b74c1d99fb3fa51c413 Mon Sep 17 00:00:00 2001 From: Jason Dillaman Date: Fri, 6 Mar 2015 15:40:48 -0500 Subject: [PATCH] tests: librados_test_stub reads should deep-copy If a client of librados_test_stub modified a bufferlist retrieved via a read call, the client will actually be changing the contents of the file. Therefore, read calls should deep-copy the contents of the buffer::ptrs. Signed-off-by: Jason Dillaman (cherry picked from commit 76fe8d73ff79da2d734f70680208a2c188b58671) --- .../librados_test_stub/TestMemIoCtxImpl.cc | 19 +++++++++++++++++-- .../librados_test_stub/TestMemIoCtxImpl.h | 1 + 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/test/librados_test_stub/TestMemIoCtxImpl.cc b/src/test/librados_test_stub/TestMemIoCtxImpl.cc index a9dac606155ae..2e6518a174efe 100644 --- a/src/test/librados_test_stub/TestMemIoCtxImpl.cc +++ b/src/test/librados_test_stub/TestMemIoCtxImpl.cc @@ -237,7 +237,9 @@ int TestMemIoCtxImpl::read(const std::string& oid, size_t len, uint64_t off, } len = clip_io(off, len, file->data.length()); if (bl != NULL && len > 0) { - bl->substr_of(file->data, off, len); + bufferlist bit; + bit.substr_of(file->data, off, len); + append_clone(bit, bl); } return 0; } @@ -353,7 +355,9 @@ int TestMemIoCtxImpl::sparse_read(const std::string& oid, uint64_t off, } } if (data_bl != NULL && len > 0) { - data_bl->substr_of(file->data, off, len); + bufferlist bit; + bit.substr_of(file->data, off, len); + append_clone(bit, data_bl); } return 0; } @@ -511,6 +515,17 @@ int TestMemIoCtxImpl::zero(const std::string& oid, uint64_t off, uint64_t len) { return write(oid, bl, len, off); } +void TestMemIoCtxImpl::append_clone(bufferlist& src, bufferlist* dest) { + // deep-copy the src to ensure our memory-based mock RADOS data cannot + // be modified by callers + if (src.length() > 0) { + bufferlist::iterator iter = src.begin(); + buffer::ptr ptr; + iter.copy(src.length(), ptr); + dest->append(ptr); + } +} + size_t TestMemIoCtxImpl::clip_io(size_t off, size_t len, size_t bl_len) { if (off >= bl_len) { len = 0; diff --git a/src/test/librados_test_stub/TestMemIoCtxImpl.h b/src/test/librados_test_stub/TestMemIoCtxImpl.h index cfec62490ebb2..68adf2105e0be 100644 --- a/src/test/librados_test_stub/TestMemIoCtxImpl.h +++ b/src/test/librados_test_stub/TestMemIoCtxImpl.h @@ -59,6 +59,7 @@ private: TestMemRadosClient *m_client; TestMemRadosClient::Pool *m_pool; + void append_clone(bufferlist& src, bufferlist* dest); size_t clip_io(size_t off, size_t len, size_t bl_len); void ensure_minimum_length(size_t len, bufferlist *bl); -- 2.39.5