Skip to content

Commit

Permalink
peek into file before open
Browse files Browse the repository at this point in the history
* check file open, providing nice and early error
* explicit check for perfparser (magic number QPERFSTREAM)
  opening directly without the need to fall back to file name
* explicit check for V1 perf data which isn't supported by perfparser
* explicit check for unknown file type

Fixes: KDAB#477
  • Loading branch information
GitMensch committed Oct 24, 2023
1 parent cdc66a4 commit 9cbfeba
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/parsers/perf/perfparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1583,23 +1583,35 @@ void PerfParser::startParseFile(const QString& path)
emit parsingFinished();
};

if (path.endsWith(QLatin1String(".perfparser"))) {
QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
emit parsingFailed(tr("Failed to open file %1: %2").arg(path, file.errorString()));
return;
}
QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
emit parsingFailed(tr("Failed to open file %1: %2").arg(path, file.errorString()));
return;
}

if (file.peek(11) == "QPERFSTREAM") {
d.setInput(&file);
while (!file.atEnd() && !d.stopRequested) {
if (!d.tryParse()) {
emit parsingFailed(tr("Failed to parse file"));
// TODO: provide reason
emit parsingFailed(tr("Failed to parse file %1: %2").arg(path, QStringLiteral("unknown reason"));
return;
}
}
finalize();
return;
}

if (file.peek(8) == "PERFFILE") {
emit parsingFailed(tr("Failed to parse file %1: %2").arg(path, tr("we don't support V1 perf data")));
return;
}

if (file.peek(8) != "PERFILE2") {
emit parsingFailed(tr("Failed to parse file %1: %2").arg(path, tr("File format unknown")));
return;
}

QProcess process;
process.setProcessEnvironment(perfparserEnvironment(debuginfodUrls));
process.setProcessChannelMode(QProcess::ForwardedErrorChannel);
Expand Down

0 comments on commit 9cbfeba

Please sign in to comment.