From 046ad3b75253e2ca892afda9f6e2ad4d513f9fbf Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 25 Jun 2018 02:58:43 +0300 Subject: [PATCH] Make TlStorerUnsafe buffer unsigned. GitOrigin-RevId: 8bb19c10e4edd8b015d3a2be0449426c9c07f7df --- td/mtproto/Handshake.cpp | 2 +- td/mtproto/PacketStorer.h | 5 ++- td/mtproto/utils.h | 5 ++- td/telegram/MessagesManager.cpp | 2 +- td/telegram/StickersManager.cpp | 2 +- td/telegram/files/FileDb.cpp | 2 +- td/telegram/files/FileLocation.h | 2 +- td/telegram/logevent/LogEvent.h | 14 ++++---- tddb/td/db/BinlogKeyValue.h | 5 ++- tddb/td/db/binlog/BinlogEvent.h | 6 ++-- tdutils/td/utils/tl_helpers.h | 4 +-- tdutils/td/utils/tl_storers.h | 55 ++++++++++++++++---------------- 12 files changed, 49 insertions(+), 55 deletions(-) diff --git a/td/mtproto/Handshake.cpp b/td/mtproto/Handshake.cpp index 37fc5d937..b917d24bd 100644 --- a/td/mtproto/Handshake.cpp +++ b/td/mtproto/Handshake.cpp @@ -193,7 +193,7 @@ Status AuthKeyHandshake::on_server_dh_params(Slice message, Callback *connection string encrypted_data_str(encrypted_data_size_with_pad, 0); MutableSlice encrypted_data = encrypted_data_str; as(encrypted_data.begin() + 20) = data.get_id(); - tl_store_unsafe(data, encrypted_data.begin() + 20 + 4); + tl_store_unsafe(data, encrypted_data.ubegin() + 20 + 4); sha1(Slice(encrypted_data.ubegin() + 20, data_size), encrypted_data.ubegin()); Random::secure_bytes(encrypted_data.ubegin() + encrypted_data_size, encrypted_data_size_with_pad - encrypted_data_size); diff --git a/td/mtproto/PacketStorer.h b/td/mtproto/PacketStorer.h index 63c0322ef..b58f07e95 100644 --- a/td/mtproto/PacketStorer.h +++ b/td/mtproto/PacketStorer.h @@ -29,10 +29,9 @@ class PacketStorer } size_t store(uint8 *ptr) const override { - char *start = reinterpret_cast(ptr); - TlStorerUnsafe storer(start); + TlStorerUnsafe storer(ptr); this->do_store(storer); - return storer.get_buf() - start; + return static_cast(storer.get_buf() - ptr); } using Impl::Impl; diff --git a/td/mtproto/utils.h b/td/mtproto/utils.h index 2e6b442b4..482e1e719 100644 --- a/td/mtproto/utils.h +++ b/td/mtproto/utils.h @@ -86,11 +86,10 @@ class TLObjectStorer : public Storer { return size_; } size_t store(uint8 *ptr) const override { - char *p = reinterpret_cast(ptr); - TlStorerUnsafe storer(p); + TlStorerUnsafe storer(ptr); storer.store_binary(object_.get_id()); object_.store(storer); - return storer.get_buf() - p; + return static_cast(storer.get_buf() - ptr); } }; diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 0ce89761a..72f552502 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -4856,7 +4856,7 @@ BufferSlice MessagesManager::get_dialog_database_value(const Dialog *d) { BufferSlice value_buffer{storer_calc_length.get_length()}; auto value = value_buffer.as_slice(); - LogEventStorerUnsafe storer_unsafe(value.begin()); + LogEventStorerUnsafe storer_unsafe(value.ubegin()); store(*d, storer_unsafe); return value_buffer; } diff --git a/td/telegram/StickersManager.cpp b/td/telegram/StickersManager.cpp index de3cbff74..fb078540e 100644 --- a/td/telegram/StickersManager.cpp +++ b/td/telegram/StickersManager.cpp @@ -2407,7 +2407,7 @@ string StickersManager::get_sticker_set_database_value(const StickerSet *s, bool LOG(DEBUG) << "Sticker set " << s->id << " serialized size is " << value.size(); - LogEventStorerUnsafe storer_unsafe(value.begin()); + LogEventStorerUnsafe storer_unsafe(value.ubegin()); store_sticker_set(s, with_stickers, storer_unsafe); return value.str(); diff --git a/td/telegram/files/FileDb.cpp b/td/telegram/files/FileDb.cpp index 802dc0a50..b888d956c 100644 --- a/td/telegram/files/FileDb.cpp +++ b/td/telegram/files/FileDb.cpp @@ -297,7 +297,7 @@ Status fix_file_remote_location_key_bug(SqliteDb &db) { kv.init_with_connection(db.clone(), "files").ensure(); auto ptr = StackAllocator::alloc(4); MutableSlice prefix = ptr.as_slice(); - TlStorerUnsafe(prefix.begin()).store_int(OLD_KEY_MAGIC); + TlStorerUnsafe(prefix.ubegin()).store_int(OLD_KEY_MAGIC); kv.get_by_prefix(prefix, [&](Slice key, Slice value) { CHECK(TlParser(key).fetch_int() == OLD_KEY_MAGIC); auto remote_str = PSTRING() << key.substr(4, 4) << Slice("\0\0\0\0") << key.substr(8); diff --git a/td/telegram/files/FileLocation.h b/td/telegram/files/FileLocation.h index 2211a897d..83c87f46f 100644 --- a/td/telegram/files/FileLocation.h +++ b/td/telegram/files/FileLocation.h @@ -1293,7 +1293,7 @@ string as_key(const T &object) { BufferSlice key_buffer{calc_length.get_length()}; auto key = key_buffer.as_slice(); - TlStorerUnsafe storer(key.begin()); + TlStorerUnsafe storer(key.ubegin()); storer.store_int(T::KEY_MAGIC); object.as_key().store(storer); return key.str(); diff --git a/td/telegram/logevent/LogEvent.h b/td/telegram/logevent/LogEvent.h index 9d3307f00..30769ba78 100644 --- a/td/telegram/logevent/LogEvent.h +++ b/td/telegram/logevent/LogEvent.h @@ -168,15 +168,14 @@ class StorerImpl : public Storer { td::store(event_, storer); return storer.get_length(); } - size_t store(uint8 *ptr_x) const override { - char *ptr = reinterpret_cast(ptr_x); + size_t store(uint8 *ptr) const override { WithContext storer(ptr); storer.set_context(G()); storer.store_int(T::version()); td::store(magic(event_), storer); td::store(event_, storer); - return storer.get_buf() - ptr; + return static_cast(storer.get_buf() - ptr); } private: @@ -227,7 +226,7 @@ class LogEventStorerCalcLength : public WithContext { public: - explicit LogEventStorerUnsafe(char *buf) : WithContext(buf) { + explicit LogEventStorerUnsafe(unsigned char *buf) : WithContext(buf) { store_int(static_cast(Version::Next) - 1); set_context(G()); } @@ -262,15 +261,14 @@ class LogEventStorerImpl : public Storer { td::store(event_, storer); return storer.get_length(); } - size_t store(uint8 *ptr_x) const override { - char *ptr = reinterpret_cast(ptr_x); + size_t store(uint8 *ptr) const override { LogEventStorerUnsafe storer(ptr); td::store(event_, storer); #ifdef TD_DEBUG T check_result; log_event_parse(check_result, Slice(ptr, storer.get_buf())).ensure(); #endif - return storer.get_buf() - ptr; + return static_cast(storer.get_buf() - ptr); } private: @@ -284,7 +282,7 @@ BufferSlice log_event_store(const T &data) { BufferSlice value_buffer{storer_calc_length.get_length()}; - LogEventStorerUnsafe storer_unsafe(value_buffer.as_slice().begin()); + LogEventStorerUnsafe storer_unsafe(value_buffer.as_slice().ubegin()); store(data, storer_unsafe); #ifdef TD_DEBUG diff --git a/tddb/td/db/BinlogKeyValue.h b/tddb/td/db/BinlogKeyValue.h index 04df413d5..e515677b5 100644 --- a/tddb/td/db/BinlogKeyValue.h +++ b/tddb/td/db/BinlogKeyValue.h @@ -57,11 +57,10 @@ class BinlogKeyValue : public KeyValueSyncInterface { store(storer); return storer.get_length(); } - size_t store(uint8 *ptr_x) const override { - auto ptr = reinterpret_cast(ptr_x); + size_t store(uint8 *ptr) const override { TlStorerUnsafe storer(ptr); store(storer); - return storer.get_buf() - ptr; + return static_cast(storer.get_buf() - ptr); } }; diff --git a/tddb/td/db/binlog/BinlogEvent.h b/tddb/td/db/binlog/BinlogEvent.h index 334f7cdcd..df4ff930e 100644 --- a/tddb/td/db/binlog/BinlogEvent.h +++ b/tddb/td/db/binlog/BinlogEvent.h @@ -95,17 +95,17 @@ inline StringBuilder &operator<<(StringBuilder &sb, const BinlogEvent &event) { inline BufferSlice BinlogEvent::create_raw(uint64 id, int32 type, int32 flags, const Storer &storer) { auto raw_event = BufferSlice{storer.size() + MIN_EVENT_SIZE}; - TlStorerUnsafe tl_storer(raw_event.as_slice().begin()); + TlStorerUnsafe tl_storer(raw_event.as_slice().ubegin()); tl_storer.store_int(narrow_cast(raw_event.size())); tl_storer.store_long(id); tl_storer.store_int(type); tl_storer.store_int(flags); tl_storer.store_long(0); - CHECK(tl_storer.get_buf() == raw_event.as_slice().begin() + EVENT_HEADER_SIZE); + CHECK(tl_storer.get_buf() == raw_event.as_slice().ubegin() + EVENT_HEADER_SIZE); tl_storer.store_storer(storer); - CHECK(tl_storer.get_buf() == raw_event.as_slice().end() - EVENT_TAIL_SIZE); + 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))); return raw_event; diff --git a/tdutils/td/utils/tl_helpers.h b/tdutils/td/utils/tl_helpers.h index 1334b1282..fe6343458 100644 --- a/tdutils/td/utils/tl_helpers.h +++ b/tdutils/td/utils/tl_helpers.h @@ -187,12 +187,12 @@ string serialize(const T &object) { if (!is_aligned_pointer<4>(key.data())) { auto ptr = StackAllocator::alloc(length); MutableSlice data = ptr.as_slice(); - TlStorerUnsafe storer(data.begin()); + TlStorerUnsafe storer(data.ubegin()); store(object, storer); key.assign(data.begin(), data.size()); } else { MutableSlice data = key; - TlStorerUnsafe storer(data.begin()); + TlStorerUnsafe storer(data.ubegin()); store(object, storer); } return key; diff --git a/tdutils/td/utils/tl_storers.h b/tdutils/td/utils/tl_storers.h index b6a2b0969..da2eb07cb 100644 --- a/tdutils/td/utils/tl_storers.h +++ b/tdutils/td/utils/tl_storers.h @@ -17,11 +17,11 @@ namespace td { class TlStorerUnsafe { - char *buf; + unsigned char *buf_; public: - explicit TlStorerUnsafe(char *buf) : buf(buf) { - CHECK(is_aligned_pointer<4>(buf)); + explicit TlStorerUnsafe(unsigned char *buf) : buf_(buf) { + CHECK(is_aligned_pointer<4>(buf_)); } TlStorerUnsafe(const TlStorerUnsafe &other) = delete; @@ -29,13 +29,13 @@ class TlStorerUnsafe { template void store_binary(const T &x) { - std::memcpy(buf, reinterpret_cast(&x), sizeof(T)); - buf += sizeof(T); + std::memcpy(buf_, reinterpret_cast(&x), sizeof(T)); + buf_ += sizeof(T); } void store_int(int32 x) { - *reinterpret_cast(buf) = x; - buf += sizeof(int32); + *reinterpret_cast(buf_) = x; + buf_ += sizeof(int32); } void store_long(int64 x) { @@ -43,45 +43,45 @@ class TlStorerUnsafe { } void store_slice(Slice slice) { - std::memcpy(buf, slice.begin(), slice.size()); - buf += slice.size(); + std::memcpy(buf_, slice.begin(), slice.size()); + buf_ += slice.size(); } void store_storer(const Storer &storer) { - size_t size = storer.store(reinterpret_cast(buf)); - buf += size; + size_t size = storer.store(buf_); + buf_ += size; } template void store_string(const T &str) { size_t len = str.size(); if (len < 254) { - *buf++ = static_cast(len); + *buf_++ = static_cast(len); len++; } else if (len < (1 << 24)) { - *buf++ = static_cast(static_cast(254)); - *buf++ = static_cast(len & 255); - *buf++ = static_cast((len >> 8) & 255); - *buf++ = static_cast(len >> 16); + *buf_++ = static_cast(254); + *buf_++ = static_cast(len & 255); + *buf_++ = static_cast((len >> 8) & 255); + *buf_++ = static_cast(len >> 16); } else { LOG(FATAL) << "String size " << len << " is too big to be stored"; } - std::memcpy(buf, str.data(), str.size()); - buf += str.size(); + std::memcpy(buf_, str.data(), str.size()); + buf_ += str.size(); switch (len & 3) { case 1: - *buf++ = '\0'; + *buf_++ = 0; // fallthrough case 2: - *buf++ = '\0'; + *buf_++ = 0; // fallthrough case 3: - *buf++ = '\0'; + *buf_++ = 0; } } - char *get_buf() const { - return buf; + unsigned char *get_buf() const { + return buf_; } }; @@ -272,12 +272,11 @@ size_t tl_calc_length(const T &data) { return storer_calc_length.get_length(); } -template -size_t tl_store_unsafe(const T &data, CharT *dst) { - char *start = reinterpret_cast(dst); - TlStorerUnsafe storer_unsafe(start); +template +size_t tl_store_unsafe(const T &data, unsigned char *dst) { + TlStorerUnsafe storer_unsafe(dst); data.store(storer_unsafe); - return storer_unsafe.get_buf() - start; + return static_cast(storer_unsafe.get_buf() - dst); } } // namespace td