From a968e112e8c040b5b3193fefbca91f88ceb7217f Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 30 May 2019 18:09:44 +0300 Subject: [PATCH] Fix crash on invalid lottie file selection. --- Telegram/SourceFiles/data/data_document.cpp | 3 --- .../SourceFiles/lottie/lottie_animation.cpp | 27 +++++-------------- .../SourceFiles/lottie/lottie_animation.h | 1 - 3 files changed, 6 insertions(+), 25 deletions(-) diff --git a/Telegram/SourceFiles/data/data_document.cpp b/Telegram/SourceFiles/data/data_document.cpp index 29299188c..2bbbd6c70 100644 --- a/Telegram/SourceFiles/data/data_document.cpp +++ b/Telegram/SourceFiles/data/data_document.cpp @@ -308,9 +308,6 @@ void DocumentOpenClickHandler::Open( if (QImageReader(path).canRead()) { Core::App().showDocument(data, context); return; - //} else if (Lottie::ValidateFile(path)) { - // Core::App().showDocument(data, context); - // return; } } LaunchWithWarning(location.name(), context); diff --git a/Telegram/SourceFiles/lottie/lottie_animation.cpp b/Telegram/SourceFiles/lottie/lottie_animation.cpp index d9bfc0785..cee1bf7bb 100644 --- a/Telegram/SourceFiles/lottie/lottie_animation.cpp +++ b/Telegram/SourceFiles/lottie/lottie_animation.cpp @@ -54,28 +54,13 @@ QByteArray UnpackGzip(const QByteArray &bytes) { } // namespace -bool ValidateFile(const QString &path) { - if (!path.endsWith(qstr(".json"), Qt::CaseInsensitive) - && !path.endsWith(qstr(".tgs"), Qt::CaseInsensitive)) { - return false; - } - return true; -} - std::unique_ptr FromFile(const QString &path) { - if (!path.endsWith(qstr(".json"), Qt::CaseInsensitive) - && !path.endsWith(qstr(".tgs"), Qt::CaseInsensitive)) { - return nullptr; - } - auto f = QFile(path); - if (!f.open(QIODevice::ReadOnly)) { - return nullptr; - } - const auto content = f.readAll(); - if (content.isEmpty()) { - return nullptr; - } - return FromData(std::move(content)); + return FromData([&] { + auto f = QFile(path); + return (f.size() <= kMaxFileSize && f.open(QIODevice::ReadOnly)) + ? f.readAll() + : QByteArray(); + }()); } std::unique_ptr FromData(const QByteArray &data) { diff --git a/Telegram/SourceFiles/lottie/lottie_animation.h b/Telegram/SourceFiles/lottie/lottie_animation.h index 812d2c5e6..3e01c2b22 100644 --- a/Telegram/SourceFiles/lottie/lottie_animation.h +++ b/Telegram/SourceFiles/lottie/lottie_animation.h @@ -31,7 +31,6 @@ class Animation; class SharedState; class FrameRenderer; -bool ValidateFile(const QString &path); std::unique_ptr FromFile(const QString &path); std::unique_ptr FromData(const QByteArray &data);