summaryrefslogtreecommitdiff
path: root/Checkpoint.cpp
diff options
context:
space:
mode:
authorGreg Kaiser <gkaiser@google.com>2018-12-18 12:22:29 -0800
committerGreg Kaiser <gkaiser@google.com>2018-12-20 10:38:31 -0800
commit8859c9c9e71a4b81aaa29a26c419a63da536d11c (patch)
treecea8889130615af7efb6fb3599136a6a55e1a5f3 /Checkpoint.cpp
parent8ae16db72a259c604eded3532b37f629724a13a9 (diff)
Checkpoint: Assure proper buffer alignment
We have a char buffer on the stack, which we then cast to a struct, and then proceed to access elements in the struct. This is not safe across all platforms, as some platforms may require a certain alignment for members of the struct. We fix this by assuring an appropriate alignment for our char buffer. We also use C++ casting, and rename our buffer to differenciate it from the other 'buffer' variable in this function. Test: TreeHugger Change-Id: I8254cb6b8124e394bd805afd1ccca0faedb27ffa
Diffstat (limited to 'Checkpoint.cpp')
-rw-r--r--Checkpoint.cpp11
1 files changed, 5 insertions, 6 deletions
diff --git a/Checkpoint.cpp b/Checkpoint.cpp
index ce0d00c..28855e6 100644
--- a/Checkpoint.cpp
+++ b/Checkpoint.cpp
@@ -296,9 +296,9 @@ Status cp_restoreCheckpoint(const std::string& blockDevice) {
PLOG(ERROR) << "Cannot open " << blockDevice;
return Status::fromExceptionCode(errno, ("Cannot open " + blockDevice).c_str());
}
- char buffer[kBlockSize];
- device.read(buffer, kBlockSize);
- log_sector& ls = *(log_sector*)buffer;
+ alignas(alignof(log_sector)) char ls_buffer[kBlockSize];
+ device.read(ls_buffer, kBlockSize);
+ log_sector& ls = *reinterpret_cast<log_sector*>(ls_buffer);
if (ls.magic != kMagic) {
LOG(ERROR) << "No magic";
return Status::fromExceptionCode(EINVAL, "No magic");
@@ -307,10 +307,9 @@ Status cp_restoreCheckpoint(const std::string& blockDevice) {
LOG(INFO) << "Restoring " << ls.sequence << " log sectors";
for (int sequence = ls.sequence; sequence >= 0; sequence--) {
- char buffer[kBlockSize];
device.seekg(0);
- device.read(buffer, kBlockSize);
- log_sector& ls = *(log_sector*)buffer;
+ device.read(ls_buffer, kBlockSize);
+ ls = *reinterpret_cast<log_sector*>(ls_buffer);
if (ls.magic != kMagic) {
LOG(ERROR) << "No magic!";
return Status::fromExceptionCode(EINVAL, "No magic");