Skip to content

Commit

Permalink
only start programs in user's path
Browse files Browse the repository at this point in the history
don't use QProcess with just program name
first search the right program in the user's path
  • Loading branch information
christoph-cullmann committed Jan 20, 2022
1 parent c0fd913 commit 804e494
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
29 changes: 17 additions & 12 deletions src/document/katedocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
#include <QMimeDatabase>
#include <QProcess>
#include <QRegularExpression>
#include <QStandardPaths>
#include <QTemporaryFile>
#include <QTextCodec>
#include <QTextStream>
Expand Down Expand Up @@ -5054,18 +5055,22 @@ void KTextEditor::DocumentPrivate::slotDelayedHandleModOnHd()
// skip that, if document is modified!
// only do that, if the file is still there, else reload makes no sense!
if (m_modOnHd && !isModified() && QFile::exists(url().toLocalFile())) {
QProcess git;
const QStringList args{QStringLiteral("cat-file"), QStringLiteral("-e"), QString::fromUtf8(oldDigest)};
git.start(QStringLiteral("git"), args);
if (git.waitForStarted()) {
git.closeWriteChannel();
if (git.waitForFinished()) {
if (git.exitCode() == 0) {
// this hash exists still in git => just reload
m_modOnHd = false;
m_modOnHdReason = OnDiskUnmodified;
m_prevModOnHdReason = OnDiskUnmodified;
documentReload();
// we only want to use git from PATH, cache this
static const QString fullGitPath = QStandardPaths::findExecutable(QStringLiteral("git"));
if (!fullGitPath.isEmpty()) {
QProcess git;
const QStringList args{QStringLiteral("cat-file"), QStringLiteral("-e"), QString::fromUtf8(oldDigest)};
git.start(fullGitPath, args);
if (git.waitForStarted()) {
git.closeWriteChannel();
if (git.waitForFinished()) {
if (git.exitCode() == 0) {
// this hash exists still in git => just reload
m_modOnHd = false;
m_modOnHdReason = OnDiskUnmodified;
m_prevModOnHdReason = OnDiskUnmodified;
documentReload();
}
}
}
}
Expand Down
17 changes: 13 additions & 4 deletions src/swapfile/kateswapdiffcreator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <KMessageBox>

#include <QDir>
#include <QStandardPaths>
#include <QTextCodec>

// BEGIN SwapDiffCreator
Expand Down Expand Up @@ -85,17 +86,25 @@ void SwapDiffCreator::viewDiff()
connect(&m_proc, &QProcess::readyRead, this, &SwapDiffCreator::slotDataAvailable, Qt::UniqueConnection);
connect(&m_proc, &QProcess::finished, this, &SwapDiffCreator::slotDiffFinished, Qt::UniqueConnection);

// try to start diff process, if we can't be started be done with error
m_proc.start(QStringLiteral("diff"), QStringList() << QStringLiteral("-u") << m_originalFile.fileName() << m_recoveredFile.fileName());
if (!m_proc.waitForStarted()) {
// use diff from PATH only => inform if not found at all
const QString fullDiffPath = QStandardPaths::findExecutable(QStringLiteral("diff"));
if (fullDiffPath.isEmpty()) {
KMessageBox::sorry(nullptr,
i18n("The diff command could not be started. Please make sure that "
i18n("The diff command could not be found. Please make sure that "
"diff(1) is installed and in your PATH."),
i18n("Error Creating Diff"));
deleteLater();
return;
}

// try to start the diff program, might fail, too
m_proc.start(fullDiffPath, QStringList() << QStringLiteral("-u") << m_originalFile.fileName() << m_recoveredFile.fileName());
if (!m_proc.waitForStarted()) {
KMessageBox::sorry(nullptr, i18n("The diff command '%1' could not be started.").arg(fullDiffPath), i18n("Error Creating Diff"));
deleteLater();
return;
}

// process is up and running, we can write data to it
QTextStream ts(&m_proc);
int lineCount = recoverDoc.lines();
Expand Down

0 comments on commit 804e494

Please sign in to comment.