Skip to content

Commit

Permalink
Fix BinlogEvent size checks.
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 0fe66f0be34fd15618c3f19edaeec5e64072c170
  • Loading branch information
levlam committed Jun 11, 2020
1 parent 42e3aef commit 44155da
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
4 changes: 2 additions & 2 deletions tddb/td/db/binlog/Binlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ class BinlogReader {
it.advance(4, MutableSlice(buf, 4));
size_ = static_cast<size_t>(TlParser(Slice(buf, 4)).fetch_int());

if (size_ > MAX_EVENT_SIZE) {
if (size_ > BinlogEvent::MAX_SIZE) {
return Status::Error(PSLICE() << "Too big event " << tag("size", size_));
}
if (size_ < MIN_EVENT_SIZE) {
if (size_ < BinlogEvent::MIN_SIZE) {
return Status::Error(PSLICE() << "Too small event " << tag("size", size_));
}
if (size_ % 4 != 0) {
Expand Down
2 changes: 0 additions & 2 deletions tddb/td/db/binlog/Binlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ class Binlog {
bool need_sync_{false};
enum class State { Empty, Load, Reindex, Run } state_{State::Empty};

static constexpr uint32 MAX_EVENT_SIZE = 65536;

Result<FileFd> open_binlog(const string &path, int32 flags);
size_t flush_events_buffer(bool force);
void do_add_event(BinlogEvent &&event);
Expand Down
16 changes: 8 additions & 8 deletions tddb/td/db/binlog/BinlogEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ Status BinlogEvent::init(BufferSlice &&raw_event, bool check_crc) {
type_ = parser.fetch_int();
flags_ = parser.fetch_int();
extra_ = parser.fetch_long();
CHECK(size_ >= MIN_EVENT_SIZE);
auto slice_data = parser.fetch_string_raw<Slice>(size_ - MIN_EVENT_SIZE);
CHECK(size_ >= MIN_SIZE);
auto slice_data = parser.fetch_string_raw<Slice>(size_ - MIN_SIZE);
data_ = MutableSlice(const_cast<char *>(slice_data.begin()), slice_data.size());
crc32_ = static_cast<uint32>(parser.fetch_int());
if (check_crc) {
CHECK(size_ >= EVENT_TAIL_SIZE);
auto calculated_crc = crc32(raw_event.as_slice().truncate(size_ - EVENT_TAIL_SIZE));
CHECK(size_ >= TAIL_SIZE);
auto calculated_crc = crc32(raw_event.as_slice().truncate(size_ - TAIL_SIZE));
if (calculated_crc != crc32_) {
return Status::Error(PSLICE() << "crc mismatch " << tag("actual", format::as_hex(calculated_crc))
<< tag("expected", format::as_hex(crc32_)) << public_to_string());
Expand All @@ -52,7 +52,7 @@ Status BinlogEvent::validate() const {
}

BufferSlice BinlogEvent::create_raw(uint64 id, int32 type, int32 flags, const Storer &storer) {
auto raw_event = BufferSlice{storer.size() + MIN_EVENT_SIZE};
auto raw_event = BufferSlice{storer.size() + MIN_SIZE};

TlStorerUnsafe tl_storer(raw_event.as_slice().ubegin());
tl_storer.store_int(narrow_cast<int32>(raw_event.size()));
Expand All @@ -61,11 +61,11 @@ BufferSlice BinlogEvent::create_raw(uint64 id, int32 type, int32 flags, const St
tl_storer.store_int(flags);
tl_storer.store_long(0);

CHECK(tl_storer.get_buf() == raw_event.as_slice().ubegin() + EVENT_HEADER_SIZE);
CHECK(tl_storer.get_buf() == raw_event.as_slice().ubegin() + HEADER_SIZE);
tl_storer.store_storer(storer);

CHECK(tl_storer.get_buf() == raw_event.as_slice().uend() - EVENT_TAIL_SIZE);
tl_storer.store_int(::td::crc32(raw_event.as_slice().truncate(raw_event.size() - EVENT_TAIL_SIZE)));
CHECK(tl_storer.get_buf() == raw_event.as_slice().uend() - TAIL_SIZE);
tl_storer.store_int(::td::crc32(raw_event.as_slice().truncate(raw_event.size() - TAIL_SIZE)));

return raw_event;
}
Expand Down
11 changes: 6 additions & 5 deletions tddb/td/db/binlog/BinlogEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ inline auto EmptyStorer() {
return create_default_storer(impl);
}

static constexpr size_t MAX_EVENT_SIZE = 1 << 24;
static constexpr size_t EVENT_HEADER_SIZE = 4 + 8 + 4 + 4 + 8;
static constexpr size_t EVENT_TAIL_SIZE = 4;
static constexpr size_t MIN_EVENT_SIZE = EVENT_HEADER_SIZE + EVENT_TAIL_SIZE;

extern int32 VERBOSITY_NAME(binlog);

struct BinlogDebugInfo {
Expand All @@ -46,6 +41,7 @@ struct BinlogDebugInfo {
const char *file{""};
int line{0};
};

inline StringBuilder &operator<<(StringBuilder &sb, const BinlogDebugInfo &info) {
if (info.line == 0) {
return sb;
Expand All @@ -54,6 +50,11 @@ inline StringBuilder &operator<<(StringBuilder &sb, const BinlogDebugInfo &info)
}

struct BinlogEvent {
static constexpr size_t MAX_SIZE = 1 << 24;
static constexpr size_t HEADER_SIZE = 4 + 8 + 4 + 4 + 8;
static constexpr size_t TAIL_SIZE = 4;
static constexpr size_t MIN_SIZE = HEADER_SIZE + TAIL_SIZE;

int64 offset_;

uint32 size_;
Expand Down

0 comments on commit 44155da

Please sign in to comment.