Add safe checks around use of QImageReader (#3736)

Co-authored-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
kornes 2022-05-28 11:48:31 +00:00 committed by GitHub
parent 135f914b38
commit efcfb19187
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 12 deletions

View file

@ -138,14 +138,6 @@ namespace detail {
{
QVector<Frame<QImage>> frames;
if (reader.imageCount() == 0)
{
qCDebug(chatterinoImage)
<< "Error while reading image" << url.string << ": '"
<< reader.errorString() << "'";
return frames;
}
QImage image;
for (int index = 0; index < reader.imageCount(); ++index)
{
@ -413,8 +405,30 @@ void Image::actuallyLoad()
buffer.open(QIODevice::ReadOnly);
QImageReader reader(&buffer);
if (!reader.canRead())
{
qCDebug(chatterinoImage)
<< "Error: image cant be read " << shared->url().string;
return Failure;
}
const auto size = reader.size();
if (size.isEmpty())
{
return Failure;
}
// returns 1 for non-animated formats
if (reader.imageCount() <= 0)
{
qCDebug(chatterinoImage)
<< "Error: image has less than 1 frame "
<< shared->url().string << ": " << reader.errorString();
return Failure;
}
// use "double" to prevent int overflows
if (double(reader.size().width()) * double(reader.size().height()) *
if (double(size.width()) * double(size.height()) *
double(reader.imageCount()) * 4.0 >
double(Image::maxBytesRam))
{

View file

@ -14,7 +14,6 @@
#include "util/FormatTime.hpp"
#include <QDateTime>
#include <QImageReader>
namespace chatterino {

View file

@ -204,8 +204,18 @@ void TwitchBadges::loadEmoteImage(const QString &name, ImagePtr image,
buffer.open(QIODevice::ReadOnly);
QImageReader reader(&buffer);
QImage image;
if (reader.imageCount() == 0 || !reader.read(&image))
if (!reader.canRead() || reader.size().isEmpty())
{
return Failure;
}
QImage image = reader.read();
if (image.isNull())
{
return Failure;
}
if (reader.imageCount() <= 0)
{
return Failure;
}