From 63149745edf573b5f0fbaaba6e9512cc8db99988 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Poizl?= Date: Sun, 12 Apr 2026 10:19:40 +0200 Subject: [PATCH] Handle return value of QFile::open --- src/about.cpp | 16 +++++++++------- src/httpdownloader.cpp | 16 ++++++++++------ src/httpdownloader.h | 4 ++-- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/about.cpp b/src/about.cpp index fa96a58..5db0eea 100644 --- a/src/about.cpp +++ b/src/about.cpp @@ -15,11 +15,13 @@ About::About(QWidget *parent) : QDialog(parent) QLabel *label = new QLabel(this); QFile tenmonText(":/about/tenmon"); - tenmonText.open(QIODevice::ReadOnly); - QByteArray text = tenmonText.readAll(); - text.replace("@GITVERSION@", GITVERSION); - label->setText(text); - label->setOpenExternalLinks(true); + if(tenmonText.open(QIODevice::ReadOnly)) + { + QByteArray text = tenmonText.readAll(); + text.replace("@GITVERSION@", GITVERSION); + label->setText(text); + label->setOpenExternalLinks(true); + } QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok); connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); @@ -41,8 +43,8 @@ HelpDialog::HelpDialog(QWidget *parent) : QDialog(parent) layout->addWidget(helpText); QFile tenmonText(":/help"); - tenmonText.open(QIODevice::ReadOnly); - helpText->setHtml(tenmonText.readAll()); + if(tenmonText.open(QIODevice::ReadOnly)) + helpText->setHtml(tenmonText.readAll()); } QString getVersion() diff --git a/src/httpdownloader.cpp b/src/httpdownloader.cpp index 7d1b800..2aa9ba2 100644 --- a/src/httpdownloader.cpp +++ b/src/httpdownloader.cpp @@ -345,18 +345,22 @@ Download::Download(QNetworkReply *reply, const QString indexPath, QObject *paren filename.remove(QRegularExpression("\\.zst$")); _fw.setFileName(indexPath + "/" + filename); - _fw.open(QIODevice::WriteOnly | QIODevice::Truncate); - if(_fw.isOpen()) + if(_fw.open(QIODevice::WriteOnly | QIODevice::Truncate)) { qDebug() << "open file" << _fw.fileName(); + _dstream = ZSTD_createDStream(); + } + else + { + qWarning() << "Failed to open file" << _fw.fileName(); + abort(); } - - _dstream = ZSTD_createDStream(); } Download::~Download() { - ZSTD_freeDStream(_dstream); + if(_dstream) + ZSTD_freeDStream(_dstream); } void Download::abort() @@ -402,7 +406,7 @@ void Download::finished() void Download::decompress(QByteArray &data) { - if(data.isEmpty())return; + if(data.isEmpty() || _dstream == nullptr)return; _hash.addData(data); diff --git a/src/httpdownloader.h b/src/httpdownloader.h index 6c58eda..d283b15 100644 --- a/src/httpdownloader.h +++ b/src/httpdownloader.h @@ -11,8 +11,8 @@ class Download : public QObject { Q_OBJECT - QNetworkReply *_reply; - ZSTD_DStream *_dstream; + QNetworkReply *_reply = nullptr; + ZSTD_DStream *_dstream = nullptr; QFile _fw; QCryptographicHash _hash; public: