From cfddca8f58f424060987d8e192ae81a44225f229 Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 7 Dec 2022 13:03:13 +0400 Subject: [PATCH] Fix possible crashes in toastParent expiring. --- Telegram/SourceFiles/api/api_bot.cpp | 4 +++- Telegram/SourceFiles/api/api_report.cpp | 5 +++-- .../boxes/peers/edit_participant_box.cpp | 14 +++++++------- Telegram/SourceFiles/boxes/sticker_set_box.cpp | 5 +++-- .../history/view/history_view_context_menu.cpp | 4 ++-- Telegram/SourceFiles/menu/menu_ttl.cpp | 1 - Telegram/SourceFiles/menu/menu_ttl_validator.cpp | 7 +++++-- Telegram/lib_ui | 2 +- 8 files changed, 24 insertions(+), 18 deletions(-) diff --git a/Telegram/SourceFiles/api/api_bot.cpp b/Telegram/SourceFiles/api/api_bot.cpp index f98d70118d..b6bca25211 100644 --- a/Telegram/SourceFiles/api/api_bot.cpp +++ b/Telegram/SourceFiles/api/api_bot.cpp @@ -110,7 +110,9 @@ void SendBotCallbackData( const auto showAlert = data.is_alert(); if (!message.isEmpty()) { - if (showAlert) { + if (!show->valid()) { + return; + } else if (showAlert) { show->showBox(Ui::MakeInformBox(message)); } else { if (withPassword) { diff --git a/Telegram/SourceFiles/api/api_report.cpp b/Telegram/SourceFiles/api/api_report.cpp index 3b607f3d25..663daf767d 100644 --- a/Telegram/SourceFiles/api/api_report.cpp +++ b/Telegram/SourceFiles/api/api_report.cpp @@ -44,9 +44,10 @@ void SendReport( Ui::ReportReason reason, const QString &comment, std::variant> data) { - auto done = [=] { + auto weak = Ui::MakeWeak(toastParent.get()); + auto done = crl::guard(toastParent, [=] { Ui::Toast::Show(toastParent, tr::lng_report_thanks(tr::now)); - }; + }); v::match(data, [&](v::null_t) { peer->session().api().request(MTPaccount_ReportPeer( peer->input, diff --git a/Telegram/SourceFiles/boxes/peers/edit_participant_box.cpp b/Telegram/SourceFiles/boxes/peers/edit_participant_box.cpp index 79db150c8f..baf734f4e3 100644 --- a/Telegram/SourceFiles/boxes/peers/edit_participant_box.cpp +++ b/Telegram/SourceFiles/boxes/peers/edit_participant_box.cpp @@ -622,21 +622,21 @@ void EditAdminBox::sendTransferRequestFrom( channel->inputChannel, user->inputUser, result.result - )).done([=, toastParent = _show.toastParent()](const MTPUpdates &result) { + )).done([=](const MTPUpdates &result) { api->applyUpdates(result); + if (!box && !weak) { + return; + } + Ui::Toast::Show( - toastParent, + (box ? Ui::BoxShow(box) : weak->_show).toastParent(), (channel->isBroadcast() ? tr::lng_rights_transfer_done_channel : tr::lng_rights_transfer_done_group)( tr::now, lt_user, user->shortName())); - if (box) { - Ui::BoxShow(box).hideLayer(); - } else if (weak) { - weak->_show.hideLayer(); - } + (box ? Ui::BoxShow(box) : weak->_show).hideLayer(); }).fail(crl::guard(this, [=](const MTP::Error &error) { if (weak) { _transferRequestId = 0; diff --git a/Telegram/SourceFiles/boxes/sticker_set_box.cpp b/Telegram/SourceFiles/boxes/sticker_set_box.cpp index 1d9b42d90c..fe614f8685 100644 --- a/Telegram/SourceFiles/boxes/sticker_set_box.cpp +++ b/Telegram/SourceFiles/boxes/sticker_set_box.cpp @@ -1438,6 +1438,7 @@ void StickerSetBox::Inner::install() { } void StickerSetBox::Inner::archiveStickers() { + const auto toastParent = Window::Show(_controller).toastParent(); _api.request(MTPmessages_InstallStickerSet( Data::InputStickerSet(_input), MTP_boolTrue() @@ -1445,9 +1446,9 @@ void StickerSetBox::Inner::archiveStickers() { if (result.type() == mtpc_messages_stickerSetInstallResultSuccess) { _setArchived.fire_copy(_setId); } - }).fail([toastParent = Window::Show(_controller).toastParent()] { + }).fail(crl::guard(toastParent, [=] { Ui::Toast::Show(toastParent, Lang::Hard::ServerError()); - }).send(); + })).send(); } void StickerSetBox::Inner::updateItems() { diff --git a/Telegram/SourceFiles/history/view/history_view_context_menu.cpp b/Telegram/SourceFiles/history/view/history_view_context_menu.cpp index 8bedf035ba..38ec61142b 100644 --- a/Telegram/SourceFiles/history/view/history_view_context_menu.cpp +++ b/Telegram/SourceFiles/history/view/history_view_context_menu.cpp @@ -1209,11 +1209,11 @@ void AddSaveSoundForNotifications( Api::ToggleSavedRingtone( document, item->fullId(), - [=] { + crl::guard(toastParent, [=] { Ui::Toast::Show( toastParent, tr::lng_ringtones_toast_added(tr::now)); - }, + }), true); }, &st::menuIconSoundAdd); } diff --git a/Telegram/SourceFiles/menu/menu_ttl.cpp b/Telegram/SourceFiles/menu/menu_ttl.cpp index 2d067c8723..f77294a2f2 100644 --- a/Telegram/SourceFiles/menu/menu_ttl.cpp +++ b/Telegram/SourceFiles/menu/menu_ttl.cpp @@ -202,7 +202,6 @@ void TTLBox(not_null box, Args args) { if (args.startTtl && !args.hideDisable) { box->addLeftButton(tr::lng_manage_messages_ttl_disable(), [=] { args.callback(0, [=] { box->closeBox(); }); - box->getDelegate()->hideLayer(); }); } } diff --git a/Telegram/SourceFiles/menu/menu_ttl_validator.cpp b/Telegram/SourceFiles/menu/menu_ttl_validator.cpp index 9a5283bfc5..4a0edd2e75 100644 --- a/Telegram/SourceFiles/menu/menu_ttl_validator.cpp +++ b/Telegram/SourceFiles/menu/menu_ttl_validator.cpp @@ -68,7 +68,7 @@ Args TTLValidator::createArgs() const { mtpRequestId savingRequestId = 0; }; const auto state = std::make_shared(); - auto callback = [=, toastParent = show->toastParent()]( + auto callback = [=]( TimeId period, Fn) { auto &api = peer->session().api(); @@ -79,12 +79,15 @@ Args TTLValidator::createArgs() const { api.request(state->savingRequestId).cancel(); } state->savingPeriod = period; + const auto weak = Ui::MakeWeak(show->toastParent().get()); state->savingRequestId = api.request(MTPmessages_SetHistoryTTL( peer->input, MTP_int(period) )).done([=](const MTPUpdates &result) { peer->session().api().applyUpdates(result); - ShowAutoDeleteToast(toastParent, peer); + if (const auto strong = weak.data()) { + ShowAutoDeleteToast(strong, peer); + } state->savingRequestId = 0; }).fail([=] { state->savingRequestId = 0; diff --git a/Telegram/lib_ui b/Telegram/lib_ui index d5921e742f..580e6a561f 160000 --- a/Telegram/lib_ui +++ b/Telegram/lib_ui @@ -1 +1 @@ -Subproject commit d5921e742fa9f17f65a25091a184f5a8a72ea779 +Subproject commit 580e6a561f7d252a94888b54c206e06ac0237c38