Fix BinlogEvent size checks.

GitOrigin-RevId: 0fe66f0be34fd15618c3f19edaeec5e64072c170
This commit is contained in:
levlam 2020-06-11 18:21:18 +03:00
parent 42e3aefc02
commit 44155da2d8
4 changed files with 16 additions and 17 deletions

View File

@ -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) {

View File

@ -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);

View File

@ -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());
@ -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()));
@ -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;
}

View File

@ -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_;