From 116f0028a2abe9bbcc9d7ea7c15888662dc94cee Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 1 Aug 2019 22:12:04 +0300 Subject: [PATCH] Make G() safe. GitOrigin-RevId: d417008f94ce6f8c587561af352d696ef55895b3 --- td/telegram/Client.cpp | 3 +++ td/telegram/Global.h | 11 +++++++++-- tdactor/td/actor/impl/ActorInfo-decl.h | 5 +++++ tdactor/test/actors_main.cpp | 5 +++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/td/telegram/Client.cpp b/td/telegram/Client.cpp index c205e5a1..4f17ed9d 100644 --- a/td/telegram/Client.cpp +++ b/td/telegram/Client.cpp @@ -122,6 +122,9 @@ class MultiTd : public Actor { public: explicit TdActorContext(std::string tag) : tag_(std::move(tag)) { } + int32 get_id() const override { + return 0x172ae58d; + } std::string tag_; }; auto context = std::make_shared(to_string(td_id)); diff --git a/td/telegram/Global.h b/td/telegram/Global.h index 5ce47327..b9236ffa 100644 --- a/td/telegram/Global.h +++ b/td/telegram/Global.h @@ -68,6 +68,11 @@ class Global : public ActorContext { Global(Global &&other) = delete; Global &operator=(Global &&other) = delete; + static constexpr int32 ID = -572104940; + int32 get_id() const override { + return ID; + } + #define td_db() get_td_db_impl(__FILE__, __LINE__) TdDb *get_td_db_impl(const char *file, int line) { LOG_CHECK(td_db_) << close_flag() << " " << file << " " << line; @@ -410,8 +415,10 @@ class Global : public ActorContext { }; inline Global *G() { - CHECK(Scheduler::context()); - return static_cast(Scheduler::context()); + ActorContext *context = Scheduler::context(); + CHECK(context); + CHECK(context->get_id() == Global::ID); + return static_cast(context); } } // namespace td diff --git a/tdactor/td/actor/impl/ActorInfo-decl.h b/tdactor/td/actor/impl/ActorInfo-decl.h index 1bfd7a29..c4746ef3 100644 --- a/tdactor/td/actor/impl/ActorInfo-decl.h +++ b/tdactor/td/actor/impl/ActorInfo-decl.h @@ -32,6 +32,11 @@ class ActorContext { ActorContext(ActorContext &&) = delete; ActorContext &operator=(ActorContext &&) = delete; virtual ~ActorContext() = default; + + virtual int32 get_id() const { + return 0; + } + const char *tag_ = nullptr; std::weak_ptr this_ptr_; }; diff --git a/tdactor/test/actors_main.cpp b/tdactor/test/actors_main.cpp index 1ccec2b3..ef08e279 100644 --- a/tdactor/test/actors_main.cpp +++ b/tdactor/test/actors_main.cpp @@ -468,6 +468,10 @@ TEST(Actors, do_after_stop) { class XContext : public ActorContext { public: + int32 get_id() const override { + return 123456789; + } + void validate() { CHECK(x == 1234); } @@ -476,6 +480,7 @@ class XContext : public ActorContext { } int x = 1234; }; + class WithContext : public Actor { public: void start_up() override {