From c7b0d2967acf2430aabfda4910e77f4cf4827f55 Mon Sep 17 00:00:00 2001 From: Arno Date: Mon, 17 Jun 2024 23:47:28 +0200 Subject: Fix crash when trying to read an invalid image Helper::picSize assumed that all data passed to it is a valid image and thus crashed with an exception. Wrap it into try - catch and return QVariant() when the pic is invalid. --- helper.cpp | 10 +++++++--- newpicsdialog.cpp | 3 +++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/helper.cpp b/helper.cpp index 32dc11c..9398a06 100644 --- a/helper.cpp +++ b/helper.cpp @@ -304,9 +304,13 @@ namespace Helper { QVariant picSize(const QString &path){ Magick::Image img; - img.ping(qPrintable(path)); - QString retval = QString("%1x%2").arg(QString::number(img.columns()), QString::number(img.rows())); - return retval; + try { + img.ping(qPrintable(path)); + QString retval = QString("%1x%2").arg(QString::number(img.columns()), QString::number(img.rows())); + return retval; + } catch(...) { + return QVariant(); + } } PicData convertArchivefileToPng(PicData data){ diff --git a/newpicsdialog.cpp b/newpicsdialog.cpp index baaed8f..e7e963b 100644 --- a/newpicsdialog.cpp +++ b/newpicsdialog.cpp @@ -354,6 +354,9 @@ void NewPicFilesModel::addFile(const QString &path){ return; } QVariant picSize = Helper::picSize(fi.absoluteFilePath()); + if(picSize == QVariant()){ + return; + } insertRows(0, 1, QModelIndex()); QModelIndex inserted = index(0, 1, QModelIndex()); setData(inserted, fi.fileName(), FileNameRole); -- cgit v1.2.3-70-g09d2