Skip to content

Commit 9ac350f

Browse files
committed
Check the return value of QFile and QTemporary file
This silences some compiler warnings. But it is also good to know if these files can not be opened.
1 parent 29934d4 commit 9ac350f

File tree

7 files changed

+95
-37
lines changed

7 files changed

+95
-37
lines changed

src/docks/encodedock.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,15 +1408,22 @@ MeltJob *EncodeDock::createMeltJob(Mlt::Producer *service,
14081408

14091409
// get temp filename
14101410
auto tmp = new QTemporaryFile{Util::writableTemporaryFile(target)};
1411-
tmp->open();
1411+
if (!tmp->open()) {
1412+
LOG_ERROR() << "Failed to create temporary file" << tmp->fileName();
1413+
delete tmp;
1414+
return nullptr;
1415+
}
14121416
QString fileName = tmp->fileName();
14131417
auto isProxy = ui->previewScaleCheckBox->isChecked() && Settings.proxyEnabled();
14141418
MLT.saveXML(fileName, service, false /* without relative paths */, tmp, isProxy);
14151419
tmp->close();
14161420

14171421
// parse xml
14181422
QFile f1(fileName);
1419-
f1.open(QIODevice::ReadOnly);
1423+
if (!f1.open(QIODevice::ReadOnly)) {
1424+
LOG_ERROR() << "Failed to open XML file for reading" << fileName;
1425+
return nullptr;
1426+
}
14201427
QXmlStreamReader xmlReader(&f1);
14211428
QDomDocument dom(fileName);
14221429
dom.setContent(&xmlReader, false);
@@ -2875,7 +2882,10 @@ bool EncodeDock::checkForMissingFiles()
28752882
= QStringLiteral("%1.XXXXXX").arg(QCoreApplication::applicationName());
28762883
tmp.reset(new QTemporaryFile(info.dir().filePath(templateFileName)));
28772884
}
2878-
tmp->open();
2885+
if (!tmp->open()) {
2886+
LOG_ERROR() << "Encode: Unable to create temporary file for checking missing files";
2887+
return false;
2888+
}
28792889
QString fileName = tmp->fileName();
28802890
auto isProxy = ui->previewScaleCheckBox->isChecked() && Settings.proxyEnabled();
28812891
MLT.saveXML(fileName, service, false /* without relative paths */, tmp.get(), isProxy);

src/docks/findanalysisfilterparser.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ class FindAnalysisFilterParser : public Mlt::Parser
6060

6161
// Touch file to prevent overwriting the same file
6262
QFile file(filename);
63-
file.open(QIODevice::WriteOnly);
64-
file.resize(0);
65-
file.close();
63+
if (file.open(QIODevice::WriteOnly)) {
64+
file.resize(0);
65+
file.close();
66+
}
6667
}
6768
} else {
6869
m_filters << Mlt::Filter(*filter);

src/docks/subtitlesdock.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,10 @@ void SubtitlesDock::importSubtitles()
639639
// Convert the subtitles to SRT using FFMpeg
640640
QString tmpLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/";
641641
QScopedPointer<QTemporaryFile> tmp(Util::writableTemporaryFile(tmpLocation, "XXXXXX.srt"));
642-
tmp->open();
642+
if (!tmp->open()) {
643+
LOG_ERROR() << "Failed to create temporary file" << tmp->fileName();
644+
return;
645+
}
643646
QString tmpFileName = tmp->fileName();
644647
tmp->close();
645648
QProcess proc;

src/jobs/encodejob.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ void EncodeJob::onVideoQualityTriggered()
9999

100100
// Get temp file for the new XML.
101101
QScopedPointer<QTemporaryFile> tmp(Util::writableTemporaryFile(reportPath));
102-
tmp->open();
102+
if (!tmp->open()) {
103+
LOG_ERROR() << "Failed to create temporary file" << tmp->fileName();
104+
return;
105+
}
103106

104107
// Generate the XML for the comparison.
105108
Mlt::Tractor tractor(MLT.profile());
@@ -116,7 +119,10 @@ void EncodeJob::onVideoQualityTriggered()
116119

117120
// Add consumer element to XML.
118121
QFile f1(tmp->fileName());
119-
f1.open(QIODevice::ReadOnly);
122+
if (!f1.open(QIODevice::ReadOnly)) {
123+
LOG_ERROR() << "Failed to open XML file for reading" << tmp->fileName();
124+
return;
125+
}
120126
QDomDocument dom(tmp->fileName());
121127
dom.setContent(&f1);
122128
f1.close();
@@ -288,7 +294,10 @@ void EncodeJob::onFinished(int exitCode, QProcess::ExitStatus exitStatus)
288294
appendToLog(QStringLiteral("Failed with exit code %1\n").arg(exitCode));
289295
bool isParallel = false;
290296
// Parse the XML.
291-
m_xml->open();
297+
if (!m_xml->open()) {
298+
LOG_ERROR() << "Failed to open XML file for reading" << m_xml->fileName();
299+
return;
300+
}
292301
QDomDocument dom(xmlPath());
293302
dom.setContent(m_xml.data());
294303
m_xml->close();
@@ -307,7 +316,10 @@ void EncodeJob::onFinished(int exitCode, QProcess::ExitStatus exitStatus)
307316
QString message(tr("Export job failed; trying again without Parallel processing."));
308317
MAIN.showStatusMessage(message);
309318
appendToLog(message.append("\n"));
310-
m_xml->open();
319+
if (!m_xml->open()) {
320+
LOG_ERROR() << "Failed to open XML file for reading" << m_xml->fileName();
321+
return;
322+
}
311323
QTextStream textStream(m_xml.data());
312324
dom.save(textStream, 2);
313325
m_xml->close();

src/mainwindow.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4999,18 +4999,20 @@ void MainWindow::on_actionApplicationLog_triggered()
49994999
TextViewerDialog dialog(this);
50005000
QDir dir = Settings.appDataLocation();
50015001
QFile logFile(dir.filePath("shotcut-log.txt"));
5002-
logFile.open(QIODevice::ReadOnly | QIODevice::Text);
5003-
dialog.setText(logFile.readAll());
5004-
logFile.close();
5002+
if (logFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
5003+
dialog.setText(logFile.readAll());
5004+
logFile.close();
5005+
}
50055006
dialog.setWindowTitle(tr("Application Log"));
50065007
const auto previousLogName = dir.filePath("shotcut-log.bak");
50075008
if (QFile::exists(previousLogName)) {
50085009
auto button = dialog.buttonBox()->addButton(tr("Previous"), QDialogButtonBox::ActionRole);
50095010
connect(button, &QAbstractButton::clicked, this, [&]() {
50105011
QFile logFile(previousLogName);
5011-
logFile.open(QIODevice::ReadOnly | QIODevice::Text);
5012-
dialog.setText(logFile.readAll());
5013-
logFile.close();
5012+
if (logFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
5013+
dialog.setText(logFile.readAll());
5014+
logFile.close();
5015+
}
50145016
button->setEnabled(false);
50155017
});
50165018
}
@@ -5150,9 +5152,10 @@ void MainWindow::on_actionExportEDL_triggered()
51505152
if (!result.isError()) {
51515153
// Save the result with the export file name.
51525154
QFile f(saveFileName);
5153-
f.open(QIODevice::WriteOnly | QIODevice::Text);
5154-
f.write(result.toString().toUtf8());
5155-
f.close();
5155+
if (f.open(QIODevice::WriteOnly | QIODevice::Text)) {
5156+
f.write(result.toString().toUtf8());
5157+
f.close();
5158+
}
51565159
}
51575160
}
51585161
if (result.isError()) {
@@ -6039,7 +6042,9 @@ void MainWindow::on_actionUseProxy_triggered(bool checked)
60396042
if (MLT.producer()) {
60406043
QDir dir(m_currentFile.isEmpty() ? QDir::tempPath() : QFileInfo(m_currentFile).dir());
60416044
QScopedPointer<QTemporaryFile> tmp(new QTemporaryFile(dir.filePath("shotcut-XXXXXX.mlt")));
6042-
tmp->open();
6045+
if (!tmp->open()) {
6046+
return;
6047+
}
60436048
tmp->close();
60446049
QString fileName = tmp->fileName();
60456050
tmp->remove();
@@ -6374,9 +6379,10 @@ void MainWindow::on_actionExportChapters_triggered()
63746379
if (!result.isError()) {
63756380
// Save the result with the export file name.
63766381
QFile f(saveFileName);
6377-
f.open(QIODevice::WriteOnly | QIODevice::Text);
6378-
f.write(result.toString().toUtf8());
6379-
f.close();
6382+
if (f.open(QIODevice::WriteOnly | QIODevice::Text)) {
6383+
f.write(result.toString().toUtf8());
6384+
f.close();
6385+
}
63806386
}
63816387
}
63826388
if (result.isError()) {

src/qmltypes/qmlfile.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,15 @@ void QmlFile::copyFromFile(QString source)
114114

115115
QFile inFile(source);
116116
QFile outfile(m_url.toString());
117-
inFile.open(QFile::ReadOnly);
118-
outfile.open(QFile::WriteOnly);
117+
if (!inFile.open(QFile::ReadOnly)) {
118+
LOG_ERROR() << "Failed to open source file for reading" << source;
119+
return;
120+
}
121+
122+
if (!outfile.open(QFile::WriteOnly)) {
123+
LOG_ERROR() << "Failed to open destination file for writing" << m_url.toString();
124+
return;
125+
}
119126
outfile.write(inFile.readAll());
120127
outfile.close();
121128
}

src/qmltypes/qmlfilter.cpp

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,10 @@ void QmlFilter::analyze(bool isAudio, bool deferJob)
433433
// get temp file for input xml
434434
QString filename(mltFilter.get("filename"));
435435
QScopedPointer<QTemporaryFile> tmp(Util::writableTemporaryFile(filename));
436-
tmp->open();
436+
if (!tmp->open()) {
437+
LOG_ERROR() << "Failed to create temporary file" << tmp->fileName();
438+
return;
439+
}
437440

438441
mltFilter.clear("results");
439442
int disable = mltFilter.get_int("disable");
@@ -482,12 +485,18 @@ void QmlFilter::analyze(bool isAudio, bool deferJob)
482485

483486
// get temp filename for output xml
484487
QScopedPointer<QTemporaryFile> tmpTarget(Util::writableTemporaryFile(filename));
485-
tmpTarget->open();
488+
if (!tmpTarget->open()) {
489+
LOG_ERROR() << "Failed to create temporary file" << tmpTarget->fileName();
490+
return;
491+
}
486492
tmpTarget->close();
487493

488494
// parse xml
489495
QFile f1(tmp->fileName());
490-
f1.open(QIODevice::ReadOnly);
496+
if (!f1.open(QIODevice::ReadOnly)) {
497+
LOG_ERROR() << "Failed to open temporary file for reading" << tmp->fileName();
498+
return;
499+
}
491500
QDomDocument dom(tmp->fileName());
492501
dom.setContent(&f1);
493502
f1.close();
@@ -522,8 +531,9 @@ void QmlFilter::analyze(bool isAudio, bool deferJob)
522531
if (!filename.isEmpty() && !QFile::exists(filename)) {
523532
job->setProperty("filename", filename);
524533
QFile file(filename);
525-
file.open(QFile::WriteOnly);
526-
file.write("");
534+
if (file.open(QFile::WriteOnly)) {
535+
file.write("");
536+
}
527537
}
528538
if (deferJob) {
529539
QTimer::singleShot(0, this, [=]() { JOBS.add(job); });
@@ -1093,7 +1103,10 @@ void AnalyzeDelegate::updateJob(EncodeJob *job, const QString &results)
10931103

10941104
// parse the xml
10951105
QFile file(job->xmlPath());
1096-
file.open(QIODevice::ReadOnly);
1106+
if (!file.open(QIODevice::ReadOnly)) {
1107+
LOG_ERROR() << "Failed to open job XML for reading" << job->xmlPath();
1108+
return;
1109+
}
10971110
QDomDocument dom(job->xmlPath());
10981111
dom.setContent(&file);
10991112
file.close();
@@ -1136,10 +1149,13 @@ void AnalyzeDelegate::updateJob(EncodeJob *job, const QString &results)
11361149

11371150
if (isUpdated) {
11381151
// Save the new XML.
1139-
file.open(QIODevice::WriteOnly);
1140-
QTextStream textStream(&file);
1141-
dom.save(textStream, 2);
1142-
file.close();
1152+
if (file.open(QIODevice::WriteOnly)) {
1153+
QTextStream textStream(&file);
1154+
dom.save(textStream, 2);
1155+
file.close();
1156+
} else {
1157+
LOG_ERROR() << "Failed to open job XML for writing" << job->xmlPath();
1158+
}
11431159
}
11441160
}
11451161

@@ -1192,7 +1208,10 @@ QString AnalyzeDelegate::resultsFromXml(const QString &fileName)
11921208
{
11931209
// parse the xml
11941210
QFile file(fileName);
1195-
file.open(QIODevice::ReadOnly);
1211+
if (!file.open(QIODevice::ReadOnly)) {
1212+
LOG_ERROR() << "Failed to open job XML for reading" << fileName;
1213+
return QString();
1214+
}
11961215
QDomDocument dom(fileName);
11971216
dom.setContent(&file);
11981217
file.close();

0 commit comments

Comments
 (0)