Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Check buffer size in JournalReader::ReadString before writing #1218

Merged
merged 1 commit into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/server/journal/serializer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,17 @@ template io::Result<uint16_t> JournalReader::ReadUInt<uint16_t>();
template io::Result<uint32_t> JournalReader::ReadUInt<uint32_t>();
template io::Result<uint64_t> JournalReader::ReadUInt<uint64_t>();

io::Result<size_t> JournalReader::ReadString(char* buffer) {
io::Result<size_t> JournalReader::ReadString(MutableSlice buffer) {
size_t size = 0;
SET_OR_UNEXPECT(ReadUInt<uint64_t>(), size);

if (auto ec = EnsureRead(size); ec)
return make_unexpected(ec);

buf_.ReadAndConsume(size, buffer);
if (size > buffer.size())
return make_unexpected(make_error_code(errc::bad_message));

buf_.ReadAndConsume(size, buffer.data());

return size;
}
Expand All @@ -158,9 +161,11 @@ std::error_code JournalReader::ReadCommand(journal::ParsedEntry::CmdData* data)
char* ptr = data->command_buf.get();
for (auto& span : data->cmd_args) {
size_t size;
SET_OR_RETURN(ReadString(ptr), size);
SET_OR_RETURN(ReadString({ptr, cmd_size}), size);
DCHECK(size <= cmd_size);
span = MutableSlice{ptr, size};
ptr += size;
cmd_size -= size;
}
return std::error_code{};
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/journal/serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct JournalReader {
template <typename UT> io::Result<UT> ReadUInt();

// Read and copy to buffer, return size.
io::Result<size_t> ReadString(char* buffer);
io::Result<size_t> ReadString(MutableSlice buffer);

// Read argument array into string buffer.
std::error_code ReadCommand(journal::ParsedEntry::CmdData* entry);
Expand Down