Fix DataNodeView: Only allow read access to data region

This commit is contained in:
Sebastian Messmer 2015-03-05 22:22:22 +01:00
parent 398b564156
commit e64ab10059
2 changed files with 4 additions and 15 deletions

View File

@ -94,23 +94,12 @@ public:
return GetOffset<DataNodeLayout::HEADERSIZE_BYTES, Entry>();
}
template<typename Entry>
Entry *DataBegin() {
return const_cast<Entry*>(const_cast<const DataNodeView*>(this)->DataBegin<Entry>());
}
template<typename Entry>
const Entry *DataEnd() const {
const unsigned int NUM_ENTRIES = layout().datasizeBytes() / sizeof(Entry);
return DataBegin<Entry>() + NUM_ENTRIES;
}
template<typename Entry>
Entry *DataEnd() {
return const_cast<Entry*>(const_cast<const DataNodeView*>(this)->DataEnd<Entry>());
}
DataNodeLayout layout() const {
return DataNodeLayout(_block->size());
}

View File

@ -63,10 +63,10 @@ TEST_F(DataNodeViewTest, DataIsStored) {
auto key = block->key();
{
DataNodeView view(std::move(block));
std::memcpy(view.DataBegin<uint8_t>(), randomData.data(), randomData.size());
view.write(randomData.data(), 0, randomData.size());
}
DataNodeView view(blockStore->load(key));
EXPECT_EQ(0, std::memcmp(view.DataBegin<uint8_t>(), randomData.data(), randomData.size()));
EXPECT_EQ(0, std::memcmp(view.data(), randomData.data(), randomData.size()));
}
TEST_F(DataNodeViewTest, HeaderAndBodyDontOverlap) {
@ -77,12 +77,12 @@ TEST_F(DataNodeViewTest, HeaderAndBodyDontOverlap) {
DataNodeView view(std::move(block));
view.setDepth(3);
view.setSize(1000000000u);
std::memcpy(view.DataBegin<uint8_t>(), randomData.data(), DATASIZE_BYTES);
view.write(randomData.data(), 0, DATASIZE_BYTES);
}
DataNodeView view(blockStore->load(key));
EXPECT_EQ(3, view.Depth());
EXPECT_EQ(1000000000u, view.Size());
EXPECT_EQ(0, std::memcmp(view.DataBegin<uint8_t>(), randomData.data(), DATASIZE_BYTES));
EXPECT_EQ(0, std::memcmp(view.data(), randomData.data(), DATASIZE_BYTES));
}
TEST_F(DataNodeViewTest, DataBeginWorksWithOneByteEntries) {