Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 32 additions & 22 deletions common/include/serde.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ struct serde<std::string> {
/// @copydoc serde::deserialize
void deserialize(std::istream& is, std::string* items, unsigned num) const {
unsigned i = 0;
bool failure = false;
try {
for (; i < num; i++) {
uint32_t length;
Expand All @@ -176,14 +175,18 @@ struct serde<std::string> {
new (&items[i]) std::string(std::move(str));
}
} catch (std::istream::failure&) {
failure = true;
// the stream sets its state before throwing, so this is reported below
} catch (...) {
for (unsigned j = 0; j < i; ++j) {
items[j].~basic_string();
}
throw;
}
if (failure || !is.good()) {
// clean up what we've already allocated
if (!is.good()) {
for (unsigned j = 0; j < i; ++j) {
items[j].~basic_string();
}
throw std::runtime_error("error reading from std::istream at item " + std::to_string(i));
throw std::runtime_error("error reading from std::istream at item " + std::to_string(i));
}
}

Expand All @@ -208,25 +211,32 @@ struct serde<std::string> {
size_t bytes_read = 0;
unsigned i = 0;
bool failure = false;
for (; i < num && !failure; ++i) {
uint32_t length;
if (bytes_read + sizeof(length) > capacity) {
bytes_read += sizeof(length); // we'll use this to report the error
failure = true;
break;
try {
for (; i < num && !failure; ++i) {
uint32_t length;
if (bytes_read + sizeof(length) > capacity) {
bytes_read += sizeof(length); // we'll use this to report the error
failure = true;
break;
}
memcpy(&length, ptr, sizeof(length));
ptr = static_cast<const char*>(ptr) + sizeof(uint32_t);
bytes_read += sizeof(length);

if (bytes_read + length > capacity) {
bytes_read += length; // we'll use this to report the error
failure = true;
break;
}
new (&items[i]) std::string(static_cast<const char*>(ptr), length);
ptr = static_cast<const char*>(ptr) + length;
bytes_read += length;
}
memcpy(&length, ptr, sizeof(length));
ptr = static_cast<const char*>(ptr) + sizeof(uint32_t);
bytes_read += sizeof(length);

if (bytes_read + length > capacity) {
bytes_read += length; // we'll use this to report the error
failure = true;
break;
} catch (...) {
for (unsigned j = 0; j < i; ++j) {
items[j].~basic_string();
}
new (&items[i]) std::string(static_cast<const char*>(ptr), length);
ptr = static_cast<const char*>(ptr) + length;
bytes_read += length;
throw;
}

if (failure) {
Expand Down
Loading