From 44155da2d8326e5295ddf1b503a82cfc0d4228b6 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 11 Jun 2020 18:21:18 +0300 Subject: [PATCH] Fix BinlogEvent size checks. GitOrigin-RevId: 0fe66f0be34fd15618c3f19edaeec5e64072c170 --- tddb/td/db/binlog/Binlog.cpp | 4 ++-- tddb/td/db/binlog/Binlog.h | 2 -- tddb/td/db/binlog/BinlogEvent.cpp | 16 ++++++++-------- tddb/td/db/binlog/BinlogEvent.h | 11 ++++++----- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/tddb/td/db/binlog/Binlog.cpp b/tddb/td/db/binlog/Binlog.cpp index ae3cde0a3..c663470dc 100644 --- a/tddb/td/db/binlog/Binlog.cpp +++ b/tddb/td/db/binlog/Binlog.cpp @@ -118,10 +118,10 @@ class BinlogReader { it.advance(4, MutableSlice(buf, 4)); size_ = static_cast(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) { diff --git a/tddb/td/db/binlog/Binlog.h b/tddb/td/db/binlog/Binlog.h index 548d1d754..f5197b7ec 100644 --- a/tddb/td/db/binlog/Binlog.h +++ b/tddb/td/db/binlog/Binlog.h @@ -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 open_binlog(const string &path, int32 flags); size_t flush_events_buffer(bool force); void do_add_event(BinlogEvent &&event); diff --git a/tddb/td/db/binlog/BinlogEvent.cpp b/tddb/td/db/binlog/BinlogEvent.cpp index c4a819932..749deec87 100644 --- a/tddb/td/db/binlog/BinlogEvent.cpp +++ b/tddb/td/db/binlog/BinlogEvent.cpp @@ -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(size_ - MIN_EVENT_SIZE); + CHECK(size_ >= MIN_SIZE); + auto slice_data = parser.fetch_string_raw(size_ - MIN_SIZE); data_ = MutableSlice(const_cast(slice_data.begin()), slice_data.size()); crc32_ = static_cast(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()); @@ -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(raw_event.size())); @@ -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; } diff --git a/tddb/td/db/binlog/BinlogEvent.h b/tddb/td/db/binlog/BinlogEvent.h index 038c8cd9b..ad044603d 100644 --- a/tddb/td/db/binlog/BinlogEvent.h +++ b/tddb/td/db/binlog/BinlogEvent.h @@ -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 { @@ -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; @@ -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_;