From 7578743b74258b0cdb33b1f77e538fce315b0431 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Mon, 18 Jun 2018 20:03:09 +0200 Subject: [PATCH] Try to make message-replacing more crash-resistant --- src/widgets/helper/channelview.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/widgets/helper/channelview.cpp b/src/widgets/helper/channelview.cpp index 358443ff4..7053653af 100644 --- a/src/widgets/helper/channelview.cpp +++ b/src/widgets/helper/channelview.cpp @@ -443,13 +443,21 @@ void ChannelView::setChannel(ChannelPtr newChannel) this->channelConnections_.push_back( newChannel->messageReplaced.connect([this](size_t index, MessagePtr replacement) { MessageLayoutPtr newItem(new MessageLayout(replacement)); - if (this->messages.getSnapshot()[index]->flags & MessageLayout::AlternateBackground) { + auto snapshot = this->messages.getSnapshot(); + if (index >= snapshot.getLength()) { + debug::Log("Tried to replace out of bounds message. Index: {}. Length: {}", index, + snapshot.getLength()); + return; + } + + const auto &message = snapshot[index]; + if (message->flags & MessageLayout::AlternateBackground) { newItem->flags |= MessageLayout::AlternateBackground; } this->scrollBar.replaceHighlight(index, replacement->getScrollBarHighlight()); - this->messages.replaceItem(this->messages.getSnapshot()[index], newItem); + this->messages.replaceItem(message, newItem); this->layoutMessages(); }));