From 0e58eeb5e9017decc46115a57c7c204a9a038023 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 2 Feb 2023 04:03:17 +0300 Subject: [PATCH] Simplify BinlogEvent::init/validate. --- tddb/td/db/binlog/Binlog.cpp | 3 ++- tddb/td/db/binlog/BinlogEvent.cpp | 35 +++++++++++++++---------------- tddb/td/db/binlog/BinlogEvent.h | 17 ++++++++------- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/tddb/td/db/binlog/Binlog.cpp b/tddb/td/db/binlog/Binlog.cpp index 65ebf26b4..77240da68 100644 --- a/tddb/td/db/binlog/Binlog.cpp +++ b/tddb/td/db/binlog/Binlog.cpp @@ -139,7 +139,8 @@ class BinlogReader { } event->debug_info_ = BinlogDebugInfo{__FILE__, __LINE__}; - TRY_STATUS(event->init(input_->cut_head(size_).move_as_buffer_slice())); + event->init(input_->cut_head(size_).move_as_buffer_slice()); + TRY_STATUS(event->validate()); offset_ += size_; event->offset_ = offset_; state_ = State::ReadLength; diff --git a/tddb/td/db/binlog/BinlogEvent.cpp b/tddb/td/db/binlog/BinlogEvent.cpp index ffb1a71a4..df2593b51 100644 --- a/tddb/td/db/binlog/BinlogEvent.cpp +++ b/tddb/td/db/binlog/BinlogEvent.cpp @@ -14,26 +14,18 @@ namespace td { -Status BinlogEvent::init(BufferSlice &&raw_event, bool check_crc) { +void BinlogEvent::init(BufferSlice &&raw_event) { TlParser parser(raw_event.as_slice()); - size_ = parser.fetch_int(); - LOG_CHECK(size_ == raw_event.size()) << size_ << " " << raw_event.size() << debug_info_; - id_ = parser.fetch_long(); + size_ = static_cast(parser.fetch_int()); + LOG_CHECK(size_ == raw_event.size()) << size_ << ' ' << raw_event.size() << debug_info_; + id_ = static_cast(parser.fetch_long()); type_ = parser.fetch_int(); flags_ = parser.fetch_int(); - extra_ = parser.fetch_long(); + extra_ = static_cast(parser.fetch_long()); CHECK(size_ >= MIN_SIZE); parser.fetch_string_raw(size_ - MIN_SIZE); // skip data crc32_ = static_cast(parser.fetch_int()); - if (check_crc) { - auto calculated_crc = crc32(raw_event.as_slice().substr(0, 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()); - } - } raw_event_ = std::move(raw_event); - return Status::OK(); } Slice BinlogEvent::get_data() const { @@ -41,15 +33,22 @@ Slice BinlogEvent::get_data() const { } Status BinlogEvent::validate() const { - BinlogEvent event; - if (raw_event_.size() < 4) { + if (raw_event_.size() < MIN_SIZE) { return Status::Error("Too small event"); } - uint32 size = TlParser(raw_event_.as_slice().substr(0, 4)).fetch_int(); - if (size_ != size) { + TlParser parser(raw_event_.as_slice()); + uint32 size = static_cast(parser.fetch_int()); + if (size_ != size || size_ != raw_event_.size()) { return Status::Error(PSLICE() << "Size of event changed: " << tag("was", size_) << tag("now", size)); } - return event.init(raw_event_.clone(), true); + parser.fetch_string_raw(size_ - TAIL_SIZE - sizeof(int)); // skip + auto stored_crc32 = static_cast(parser.fetch_int()); + auto calculated_crc = crc32(Slice(raw_event_.as_slice().data(), size_ - TAIL_SIZE)); + if (calculated_crc != crc32_ || calculated_crc != stored_crc32) { + return Status::Error(PSLICE() << "CRC mismatch " << tag("actual", format::as_hex(calculated_crc)) + << tag("expected", format::as_hex(crc32_)) << public_to_string()); + } + return Status::OK(); } BufferSlice BinlogEvent::create_raw(uint64 id, int32 type, int32 flags, const Storer &storer) { diff --git a/tddb/td/db/binlog/BinlogEvent.h b/tddb/td/db/binlog/BinlogEvent.h index 9dd5499a4..c80334de4 100644 --- a/tddb/td/db/binlog/BinlogEvent.h +++ b/tddb/td/db/binlog/BinlogEvent.h @@ -74,13 +74,16 @@ struct BinlogEvent { void clear() { raw_event_ = BufferSlice(); } + bool empty() const { return raw_event_.empty(); } + BinlogEvent clone() const { BinlogEvent result; result.debug_info_ = BinlogDebugInfo{__FILE__, __LINE__}; - result.init(raw_event_.clone()).ensure(); + result.init(raw_event_.clone()); + result.validate().ensure(); return result; } @@ -89,16 +92,12 @@ struct BinlogEvent { } BinlogEvent() = default; - //explicit BinlogEvent(BufferSlice &&raw_event) { - //init(std::move(raw_event), false).ensure(); - //} + BinlogEvent(BufferSlice &&raw_event, BinlogDebugInfo info) { debug_info_ = info; - init(std::move(raw_event), false).ensure(); + init(std::move(raw_event)); } - Status init(BufferSlice &&raw_event, bool check_crc = true) TD_WARN_UNUSED_RESULT; - static BufferSlice create_raw(uint64 id, int32 type, int32 flags, const Storer &storer); std::string public_to_string() const { @@ -106,7 +105,9 @@ struct BinlogEvent { << tag("data", get_data().size()) << "]" << debug_info_; } - Status validate() const; + void init(BufferSlice &&raw_event); + + Status validate() const TD_WARN_UNUSED_RESULT; void realloc(); };