From 10e34aa9962fa99cb9fe2df4df557463a7f73ce2 Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Fri, 26 May 2017 15:20:11 +0300
Subject: [PATCH] Fix a crash in round videos with a message text.

---
 .../history/history_media_types.cpp           | 19 ++++++++++++-------
 .../SourceFiles/history/history_media_types.h |  4 +---
 .../SourceFiles/media/media_clip_reader.cpp   |  3 +++
 3 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/Telegram/SourceFiles/history/history_media_types.cpp b/Telegram/SourceFiles/history/history_media_types.cpp
index 3c851157d..2a2c391ec 100644
--- a/Telegram/SourceFiles/history/history_media_types.cpp
+++ b/Telegram/SourceFiles/history/history_media_types.cpp
@@ -1941,9 +1941,10 @@ void HistoryGif::draw(Painter &p, const QRect &r, TextSelection selection, TimeM
 	}
 
 	auto usex = 0, usew = width;
-	auto via = (!isRound || isChildMedia) ? nullptr : _parent->Get<HistoryMessageVia>();
-	auto reply = (!isRound || isChildMedia) ? nullptr : _parent->Get<HistoryMessageReply>();
-	auto forwarded = (!isRound || isChildMedia) ? nullptr : _parent->Get<HistoryMessageForwarded>();
+	auto separateRoundVideo = isSeparateRoundVideo();
+	auto via = separateRoundVideo ? _parent->Get<HistoryMessageVia>() : nullptr;
+	auto reply = separateRoundVideo ? _parent->Get<HistoryMessageReply>() : nullptr;
+	auto forwarded = separateRoundVideo ? _parent->Get<HistoryMessageForwarded>() : nullptr;
 	if (via || reply || forwarded) {
 		usew = _maxw - additionalWidth(via, reply, forwarded);
 		if (isPost) {
@@ -2180,11 +2181,11 @@ HistoryTextState HistoryGif::getState(int x, int y, HistoryStateRequest request)
 	}
 	auto out = _parent->out(), isPost = _parent->isPost();
 	auto isChildMedia = (_parent->getMedia() != this);
-	auto isRound = _data->isRoundVideo();
 	auto usew = width, usex = 0;
-	auto via = (!isRound || isChildMedia) ? nullptr : _parent->Get<HistoryMessageVia>();
-	auto reply = (!isRound || isChildMedia) ? nullptr : _parent->Get<HistoryMessageReply>();
-	auto forwarded = (!isRound || isChildMedia) ? nullptr : _parent->Get<HistoryMessageForwarded>();
+	auto separateRoundVideo = isSeparateRoundVideo();
+	auto via = separateRoundVideo ? _parent->Get<HistoryMessageVia>() : nullptr;
+	auto reply = separateRoundVideo ? _parent->Get<HistoryMessageReply>() : nullptr;
+	auto forwarded = separateRoundVideo ? _parent->Get<HistoryMessageForwarded>() : nullptr;
 	if (via || reply || forwarded) {
 		usew = _maxw - additionalWidth(via, reply, forwarded);
 		if (isPost) {
@@ -2306,6 +2307,10 @@ QString HistoryGif::mediaTypeString() const {
 	return _data->isRoundVideo() ? lang(lng_in_dlg_video_message) : qsl("GIF");
 }
 
+bool HistoryGif::isSeparateRoundVideo() const {
+	return _data->isRoundVideo() && (_parent->getMedia() == this) && !_parent->hasBubble();
+}
+
 void HistoryGif::setStatusSize(int32 newSize) const {
 	if (_data->isRoundVideo()) {
 		_statusSize = newSize;
diff --git a/Telegram/SourceFiles/history/history_media_types.h b/Telegram/SourceFiles/history/history_media_types.h
index 770b7b2e5..2f1c94b0a 100644
--- a/Telegram/SourceFiles/history/history_media_types.h
+++ b/Telegram/SourceFiles/history/history_media_types.h
@@ -583,9 +583,7 @@ private:
 		return additionalWidth(_parent->Get<HistoryMessageVia>(), _parent->Get<HistoryMessageReply>(), _parent->Get<HistoryMessageForwarded>());
 	}
 	QString mediaTypeString() const;
-	bool isSeparateRoundVideo() const {
-		return _data->isRoundVideo() && (_parent->getMedia() == this);
-	}
+	bool isSeparateRoundVideo() const;
 
 	gsl::not_null<DocumentData*> _data;
 	ClickHandlerPtr _openInMediaviewLink;
diff --git a/Telegram/SourceFiles/media/media_clip_reader.cpp b/Telegram/SourceFiles/media/media_clip_reader.cpp
index 33ffe74e3..2c15e5e46 100644
--- a/Telegram/SourceFiles/media/media_clip_reader.cpp
+++ b/Telegram/SourceFiles/media/media_clip_reader.cpp
@@ -223,6 +223,9 @@ void Reader::start(int32 framew, int32 frameh, int32 outerw, int32 outerh, Image
 }
 
 QPixmap Reader::current(int32 framew, int32 frameh, int32 outerw, int32 outerh, ImageRoundRadius radius, ImageRoundCorners corners, TimeMs ms) {
+	Expects(outerw > 0);
+	Expects(outerh > 0);
+
 	auto frame = frameToShow();
 	t_assert(frame != nullptr);