add a maximum RAM usage for images

This commit is contained in:
fourtf 2021-06-24 22:54:36 +02:00
parent 97b9bfb2bd
commit ed6ef0b98e
2 changed files with 13 additions and 0 deletions

View file

@ -405,6 +405,16 @@ void Image::actuallyLoad()
QBuffer buffer(const_cast<QByteArray *>(&data)); QBuffer buffer(const_cast<QByteArray *>(&data));
buffer.open(QIODevice::ReadOnly); buffer.open(QIODevice::ReadOnly);
QImageReader reader(&buffer); QImageReader reader(&buffer);
if (reader.size().width() * reader.size().height() *
reader.imageCount() * 4 >
Image::maxBytesRam)
{
qCDebug(chatterinoImage) << "image too large in RAM";
return Failure;
}
auto parsed = detail::readFrames(reader, shared->url()); auto parsed = detail::readFrames(reader, shared->url());
postToThread(makeConvertCallback(parsed, [weak](auto frames) { postToThread(makeConvertCallback(parsed, [weak](auto frames) {

View file

@ -50,6 +50,9 @@ using ImagePtr = std::shared_ptr<Image>;
class Image : public std::enable_shared_from_this<Image>, boost::noncopyable class Image : public std::enable_shared_from_this<Image>, boost::noncopyable
{ {
public: public:
// Maximum amount of RAM used by the image in bytes.
static constexpr int maxBytesRam = 20 * 1024 * 1024;
~Image(); ~Image();
static ImagePtr fromUrl(const Url &url, qreal scale = 1); static ImagePtr fromUrl(const Url &url, qreal scale = 1);