Skip to content

Commit e1d36cf

Browse files
committed
Fix crash in the EditCaptionBox.
1 parent fbb2bae commit e1d36cf

File tree

2 files changed

+53
-65
lines changed

2 files changed

+53
-65
lines changed

Telegram/SourceFiles/boxes/edit_caption_box.cpp

+53-64
Original file line numberDiff line numberDiff line change
@@ -72,31 +72,21 @@ EditCaptionBox::EditCaptionBox(
7272
_isAllowedEditMedia = item->media()->allowsEditMedia();
7373
_isAlbum = !item->groupId().empty();
7474

75-
QSize dimensions;
76-
auto image = (Image*)nullptr;
77-
75+
auto dimensions = QSize();
7876
const auto media = item->media();
7977
if (const auto photo = media->photo()) {
8078
_photoMedia = photo->createMediaView();
8179
_photoMedia->wanted(PhotoSize::Large, _msgId);
82-
image = _photoMedia->image(PhotoSize::Large);
83-
if (!image) {
84-
image = _photoMedia->image(PhotoSize::Thumbnail);
85-
if (!image) {
86-
image = _photoMedia->image(PhotoSize::Small);
87-
if (!image) {
88-
image = _photoMedia->thumbnailInline();
89-
}
90-
}
91-
}
9280
dimensions = _photoMedia->size(PhotoSize::Large);
81+
if (dimensions.isEmpty()) {
82+
dimensions = QSize(1, 1);
83+
}
9384
_photo = true;
9485
} else if (const auto document = media->document()) {
9586
_documentMedia = document->createMediaView();
9687
_documentMedia->thumbnailWanted(_msgId);
97-
image = _documentMedia->thumbnail();
98-
dimensions = image
99-
? image->size()
88+
dimensions = _documentMedia->thumbnail()
89+
? _documentMedia->thumbnail()->size()
10090
: document->dimensions;
10191
if (document->isAnimation()) {
10292
_gifw = style::ConvertScale(document->dimensions.width());
@@ -107,24 +97,42 @@ EditCaptionBox::EditCaptionBox(
10797
} else {
10898
_doc = true;
10999
}
100+
} else {
101+
Unexpected("Photo or document should be set.");
110102
}
111103
const auto editData = PrepareEditText(item);
112104

113-
if (!_animated
114-
&& (dimensions.isEmpty()
115-
|| _documentMedia
116-
|| (!_photoMedia && !image))) {
117-
if (!image) {
105+
const auto computeImage = [=] {
106+
if (_documentMedia) {
107+
return _documentMedia->thumbnail();
108+
} else if (const auto large = _photoMedia->image(PhotoSize::Large)) {
109+
return large;
110+
} else if (const auto thumbnail = _photoMedia->image(
111+
PhotoSize::Thumbnail)) {
112+
return thumbnail;
113+
} else if (const auto small = _photoMedia->image(PhotoSize::Small)) {
114+
return small;
115+
} else {
116+
return _photoMedia->thumbnailInline();
117+
}
118+
};
119+
120+
if (!_animated && _documentMedia) {
121+
if (dimensions.isEmpty()) {
118122
_thumbw = 0;
123+
_thumbnailImageLoaded = true;
119124
} else {
120-
const auto tw = image->width(), th = image->height();
125+
const auto tw = dimensions.width(), th = dimensions.height();
121126
if (tw > th) {
122127
_thumbw = (tw * st::msgFileThumbSize) / th;
123128
} else {
124129
_thumbw = st::msgFileThumbSize;
125130
}
126-
_thumbnailImage = image;
127131
_refreshThumbnail = [=] {
132+
const auto image = computeImage();
133+
if (!image) {
134+
return;
135+
}
128136
const auto options = Images::Option::Smooth
129137
| Images::Option::RoundedSmall
130138
| Images::Option::RoundedTopLeft
@@ -138,7 +146,9 @@ EditCaptionBox::EditCaptionBox(
138146
options,
139147
st::msgFileThumbSize,
140148
st::msgFileThumbSize));
149+
_thumbnailImageLoaded = true;
141150
};
151+
_refreshThumbnail();
142152
}
143153

144154
if (_documentMedia) {
@@ -151,13 +161,7 @@ EditCaptionBox::EditCaptionBox(
151161
_isAudio = document->isVoiceMessage()
152162
|| document->isAudioFile();
153163
}
154-
if (_refreshThumbnail) {
155-
_refreshThumbnail();
156-
}
157164
} else {
158-
if (!image && !_photoMedia) {
159-
image = Image::BlankMedia();
160-
}
161165
auto maxW = 0, maxH = 0;
162166
const auto limitW = st::sendMediaPreviewSize;
163167
auto limitH = std::min(st::confirmMaxHeight, _gifh ? _gifh : INT_MAX);
@@ -175,35 +179,38 @@ EditCaptionBox::EditCaptionBox(
175179
maxH = limitH;
176180
}
177181
}
178-
_thumbnailImage = image;
179182
_refreshThumbnail = [=] {
183+
const auto image = computeImage();
184+
const auto use = image ? image : Image::BlankMedia().get();
180185
const auto options = Images::Option::Smooth
181186
| Images::Option::Blurred;
182-
_thumb = image->pixNoCache(
187+
_thumb = use->pixNoCache(
183188
maxW * cIntRetinaFactor(),
184189
maxH * cIntRetinaFactor(),
185190
options,
186191
maxW,
187192
maxH);
193+
_thumbnailImageLoaded = true;
188194
};
189195
prepareStreamedPreview();
190196
} else {
197+
Assert(_photoMedia != nullptr);
198+
191199
maxW = dimensions.width();
192200
maxH = dimensions.height();
193-
_thumbnailImage = image;
194201
_refreshThumbnail = [=] {
195-
const auto photo = _photoMedia
196-
? _photoMedia->image(Data::PhotoSize::Large)
197-
: nullptr;
202+
const auto image = computeImage();
203+
const auto photo = _photoMedia->image(Data::PhotoSize::Large);
198204
const auto use = photo
199205
? photo
200-
: _thumbnailImage
201-
? _thumbnailImage
206+
: image
207+
? image
202208
: Image::BlankMedia().get();
203209
const auto options = Images::Option::Smooth
204-
| ((_photoMedia && !photo)
205-
? Images::Option::Blurred
206-
: Images::Option(0));
210+
| (photo
211+
? Images::Option(0)
212+
: Images::Option::Blurred);
213+
_thumbnailImageLoaded = (photo != nullptr);
207214
_thumb = use->pixNoCache(
208215
maxW * cIntRetinaFactor(),
209216
maxH * cIntRetinaFactor(),
@@ -276,35 +283,17 @@ EditCaptionBox::EditCaptionBox(
276283
scaleThumbDown();
277284
}
278285
Assert(_animated || _photo || _doc);
286+
Assert(_thumbnailImageLoaded || _refreshThumbnail);
279287

280-
_thumbnailImageLoaded = _photoMedia
281-
? (_photoMedia->image(Data::PhotoSize::Large) != nullptr)
282-
: _thumbnailImage
283-
? true
284-
: _documentMedia
285-
? !_documentMedia->owner()->hasThumbnail()
286-
: true;
287288
if (!_thumbnailImageLoaded) {
288289
subscribe(_controller->session().downloaderTaskFinished(), [=] {
289-
if (_thumbnailImageLoaded) {
290+
if (_thumbnailImageLoaded
291+
|| (_photoMedia && !_photoMedia->image(PhotoSize::Large))
292+
|| (_documentMedia && !_documentMedia->thumbnail())) {
290293
return;
291-
} else if (!_thumbnailImage
292-
&& _photoMedia
293-
&& _photoMedia->image(PhotoSize::Large)) {
294-
_thumbnailImage = _photoMedia->image(PhotoSize::Large);
295-
} else if (!_thumbnailImage
296-
&& _documentMedia
297-
&& _documentMedia->owner()->hasThumbnail()) {
298-
_thumbnailImage = _documentMedia->thumbnail();
299-
}
300-
if (_thumbnailImage) {
301-
_thumbnailImageLoaded = !_photoMedia
302-
|| _photoMedia->image(PhotoSize::Large);
303-
if (_thumbnailImageLoaded) {
304-
_refreshThumbnail();
305-
update();
306-
}
307294
}
295+
_refreshThumbnail();
296+
update();
308297
});
309298
}
310299
_field.create(

Telegram/SourceFiles/boxes/edit_caption_box.h

-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ class EditCaptionBox
108108
FullMsgId _msgId;
109109
std::shared_ptr<Data::PhotoMedia> _photoMedia;
110110
std::shared_ptr<Data::DocumentMedia> _documentMedia;
111-
Image *_thumbnailImage = nullptr;
112111
bool _thumbnailImageLoaded = false;
113112
Fn<void()> _refreshThumbnail;
114113
bool _animated = false;

0 commit comments

Comments
 (0)