• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IJHack / QtPass / 43741034965

15 Nov 2026 05:25PM UTC coverage: 96.321% (+0.01%) from 96.307%
43741034965

push

github

web-flow
Keep the bytes of an entry that is not UTF-8 (#2195)

* Keep the bytes of an entry that is not UTF-8

An entry whose content is not UTF-8 (a Latin-1 password, a key file
stored with pass insert -m) reached QtPass as text with U+FFFD for each
such byte, and two paths wrote that text back with success reported:
native re-encryption of a folder, and OK in the edit dialog, even with
nothing changed.

Executor now says, for the output it emits, whether the text encodes back
to the bytes the process wrote (lastOutputLossless()); Pass passes it on
as a third, defaulted argument of finishedShow(), and PasswordDialog does
not load an entry that would not, as it already does for one with too
many lines. A literal U+FFFD, as older versions left behind, encodes back
and stays editable.

Re-encryption carries the plaintext as bytes end to end: a byte variant of
the blocking run (Executor::executeBlockingRaw(), ImitatePass::
execBlockingRaw()), and the check after encrypting compares bytes, not two
strings damaged alike. It no longer appends a newline to an entry without
one, and every encrypt passes --no-textmode, so a gpg.conf textmode line
cannot drop the entry's carriage returns.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Anne Jan Brouwer <brouwer@annejan.com>

* tests: avoid 'caf' in executor test data (codespell)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Anne Jan Brouwer <brouwer@annejan.com>

* PasswordDialog: a refused answer takes back an earlier load

A later answer for the same entry that the dialog refuses (not UTF-8,
too many lines) left what an earlier one loaded in place with OK
enabled, so OK wrote it over the entry. Both refusals now reset the
loaded state and lock the editor and OK (CodeRabbit).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Anne Jan Brouwer <brouwer@annejan.com>

---------

Signed-off-by: Anne Jan Brouwer <brouwer@annejan.com>
Co-authored-by: Clau... (continued)

59 of 59 new or added lines in 5 files covered. (100.0%)

1 existing line in 1 file now uncovered.

8665 of 8996 relevant lines covered (96.32%)

605.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.56
/src/executor.cpp
1
// SPDX-FileCopyrightText: 2016 Anne Jan Brouwer
2
// SPDX-License-Identifier: GPL-3.0-or-later
3
#include "executor.h"
4
#include <QCoreApplication>
5
#include <QDeadlineTimer>
6
#include <QDir>
7
#include <QFileInfo>
8
#include <QStringDecoder>
9
#include <algorithm>
10
#include <climits>
11
#include <utility>
12

13
#include "qtpasslogging.h"
14

15
#ifdef Q_OS_UNIX
16
#include <sys/stat.h>
17
#endif
18

19
namespace {
20
/// Executor::setChildUmask(); negative for none.
21
std::atomic_int g_childUmask{-1};
22
/// How often a cancellable blocking run re-checks its cancel flag.
23
constexpr int kBlockingCancelPollMs = 100;
24
/// Grace between terminate() and kill() once a blocking run is cancelled;
25
/// gpg and git exit on SIGTERM well within this.
26
constexpr int kBlockingKillGraceMs = 1000;
27
} // namespace
28

29
Executor::Executor(QObject *parent) : QObject(parent) {
391 ✔
30
  connect(&m_process, &QProcess::finished, this, &Executor::onProcessFinished);
391 ✔
31
  connect(&m_process, &QProcess::started, this, &Executor::starting);
391 ✔
32
}
391 ✔
33

34
void Executor::detachFromTerminal(QProcess &process) {
1,704 ✔
35
#if defined(Q_OS_UNIX) && QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
36
  // A child inherits QtPass's controlling terminal when QtPass was started
37
  // from one. git (HTTPS credentials) and ssh (key passphrase, host key) then
38
  // prompt on that /dev/tty, and as the queue runs one command at a time,
39
  // every decrypt and save waits behind a prompt nobody sees: QtPass looks
40
  // frozen. In a session of its own the child has no terminal, so git and
41
  // ssh use SSH_ASKPASS/GIT_ASKPASS when set and otherwise fail with an
42
  // error QtPass shows. GIT_TERMINAL_PROMPT=0 is not set on top: without a
43
  // tty git has nothing to prompt on, and some credential helpers read it as
44
  // "no interaction at all" and would skip their own dialogs.
45
  //
46
  // gpg is unaffected: pinentry is started by gpg-agent, which finds the
47
  // terminal through GPG_TTY (set by the user's shell) or the tty on gpg's
48
  // stdin, here always a pipe. pass is a shell script running that gpg and
49
  // git, so the same holds for it.
50
  //
51
  // Signal dispositions are left alone (no ResetSignalHandlers): exec()
52
  // already resets caught signals, and QtPass sets no SIG_IGN of its own, so
53
  // that flag would only undo what QtPass's own parent chose (nohup, say).
54
  // Terminal signals (Ctrl+C, the SIGHUP of a closed terminal) now reach
55
  // QtPass but not the child; the children are short-lived and, with no
56
  // terminal, cannot sit waiting for input.
57
  QProcess::UnixProcessParameters params = process.unixProcessParameters();
1,704 ✔
58
  params.flags |= QProcess::UnixProcessFlag::CreateNewSession;
59
  process.setUnixProcessParameters(params);
1,704 ✔
60
#else
61
  Q_UNUSED(process);
62
#endif
63
}
1,704 ✔
64

65
void Executor::setChildUmask(int mask) { g_childUmask.store(mask); }
5 ✔
66

67
auto Executor::childUmask() -> int { return g_childUmask.load(); }
31 ✔
68

69
void Executor::startProcess(QProcess &process, const QString &app,
1,699 ✔
70
                            const QStringList &args) {
71
  detachFromTerminal(process);
1,699 ✔
72
#ifdef Q_OS_UNIX
73
  // In the child, between fork and exec: umask() is async-signal-safe.
74
  if (const int mask = g_childUmask.load(); mask >= 0) {
1,699 ✔
75
    process.setChildProcessModifier(
1 ✔
76
        [mask] { ::umask(static_cast<mode_t>(mask)); });
1 ✔
77
  }
78
#endif
79
  if (const auto wsl = parseWslCommand(app)) {
1,699 ✔
80
    process.start(wsl->launcher, wsl->argv(args));
18 ✔
81
  } else {
82
    process.start(resolveExecutable(app), args);
3,380 ✔
83
  }
84
}
1,699 ✔
85

86
auto Executor::resolveExecutable(const QString &app) -> QString {
1,696 ✔
87
  if (app.isEmpty() || QDir::isAbsolutePath(app)) {
3,391 ✔
88
    return app;
89
  }
90
  const QDir appDir(QCoreApplication::applicationDirPath());
62 ✔
91
  QStringList candidates{QDir::cleanPath(appDir.absoluteFilePath(app))};
248 ✔
92
#ifdef Q_OS_WIN
93
  candidates << QDir::cleanPath(
94
      appDir.absoluteFilePath(app + QStringLiteral(".exe")));
95
#endif
96
  for (const QString &candidate : std::as_const(candidates)) {
121 ✔
97
    // A stray non-executable file of that name must not mask PATH.
98
    const QFileInfo info(candidate);
62 ✔
99
    if (info.isFile() && info.isExecutable()) {
62 ✔
100
      return candidate;
101
    }
102
  }
62 ✔
103
  return app;
104
}
124 ✔
105

106
auto Executor::WslCommand::argv(const QStringList &args) const -> QStringList {
32 ✔
107
  return options + QStringList{QStringLiteral("--exec"), command} + args;
128 ✔
108
}
109

110
auto Executor::WslCommand::with(const QString &program) const -> WslCommand {
9 ✔
111
  return {launcher, options, program};
9 ✔
112
}
113

114
auto Executor::parseWslCommand(const QString &app)
3,329 ✔
115
    -> std::optional<WslCommand> {
116
  const QStringList parts = QProcess::splitCommand(app);
3,329 ✔
117
  if (parts.size() < 2) {
3,329 ✔
118
    return std::nullopt;
3,287 ✔
119
  }
120
  // The file name after the last separator of either kind: a Windows path
121
  // has to be recognised as such wherever the parser runs.
122
  const QString &launcher = parts.first();
123
  const qsizetype separator = std::max(launcher.lastIndexOf(QLatin1Char('/')),
42 ✔
124
                                       launcher.lastIndexOf(QLatin1Char('\\')));
42 ✔
125
  const QString launcherName = launcher.mid(separator + 1);
42 ✔
126
  const bool isWsl =
127
      launcherName.compare(QLatin1String("wsl"), Qt::CaseInsensitive) == 0 ||
55 ✔
128
      launcherName.compare(QLatin1String("wsl.exe"), Qt::CaseInsensitive) == 0;
13 ✔
129
  if (!isWsl) {
130
    return std::nullopt;
3 ✔
131
  }
132
  WslCommand wsl;
39 ✔
133
#ifdef Q_OS_WIN
134
  // A bare wsl or wsl.exe in any case is looked up on PATH as `wsl`; a path
135
  // stays as is.
136
  wsl.launcher = separator < 0 ? QStringLiteral("wsl") : launcher;
137
#else
138
  // QtPass running inside WSL reaches wsl.exe through interop under exactly
139
  // that name; nothing says `wsl` resolves too, so start what was written.
140
  wsl.launcher = launcher;
39 ✔
141
#endif
142
  // wsl.exe options that take a value; anything else starting with `-` is a
143
  // flag. A user-written -e/--exec is dropped, argv() always adds one.
144
  static const QStringList valued{
145
      QStringLiteral("-d"),   QStringLiteral("--distribution"),
2 ✔
146
      QStringLiteral("-u"),   QStringLiteral("--user"),
2 ✔
147
      QStringLiteral("--cd"), QStringLiteral("--shell-type")};
53 ✔
148
  qsizetype i = 1;
149
  for (; i < parts.size() && parts.at(i).startsWith(QLatin1Char('-')); ++i) {
56 ✔
150
    const QString &option = parts.at(i);
151
    if (option == QLatin1String("-e") || option == QLatin1String("--exec")) {
18 ✔
152
      continue;
7 ✔
153
    }
154
    if (valued.contains(option)) {
11 ✔
155
      if (i + 1 >= parts.size()) {
11 ✔
156
        return std::nullopt;
1 ✔
157
      }
158
      wsl.options << option << parts.at(++i);
159
    } else {
160
      wsl.options << option;
161
    }
162
  }
163
  // Exactly one program. More is a shell command line (`sh -c "..."`),
164
  // which is what --exec exists to keep out.
165
  if (i != parts.size() - 1) {
38 ✔
166
    return std::nullopt;
6 ✔
167
  }
168
  wsl.command = parts.at(i);
32 ✔
169
  return wsl;
170
}
39 ✔
171

172
auto Executor::wslExecArgs(const QString &command, const QStringList &args)
2 ✔
173
    -> QStringList {
174
  return WslCommand{QStringLiteral("wsl"), {}, command}.argv(args);
2 ✔
175
}
176

177
auto Executor::translatePathForWsl(const QString &path, const QString &exe)
1,220 ✔
178
    -> QString {
179
  QString normalizedPath = QDir::cleanPath(path);
1,220 ✔
180
  const auto wsl = parseWslCommand(exe);
1,220 ✔
181
  if (!wsl) {
1,220 ✔
182
    return normalizedPath;
1,219 ✔
183
  }
184
  QString wslPath;
1 ✔
185
  const auto wslpath = wsl->with(QStringLiteral("wslpath"));
2 ✔
186
  const int rc = executeBlocking(wslpath.launcher,
1 ✔
187
                                 wslpath.argv({normalizedPath}), &wslPath);
3 ✔
188
  const QString translated = wslPath.trimmed();
189
  return (rc == 0 && !translated.isEmpty()) ? translated : normalizedPath;
1 ✔
190
}
1 ✔
191

192
void Executor::executeNext() {
583 ✔
193
  if (running || m_execQueue.isEmpty()) {
583 ✔
194
    return;
305 ✔
195
  }
196
  // A copy, and marked running, before anyone hears of it: a slot on
197
  // dequeuing() that queues more cannot invalidate it or start it a second
198
  // time, and one that calls cancelNext() cannot drop it.
199
  const ExecQueueItem i = m_execQueue.head();
288 ✔
200
  running = true;
288 ✔
201
  emit dequeuing(i.id, true);
288 ✔
202

203
  // An empty executable never emits finished(); dropping it wedged the queue
204
  // (#1682). Fail it like a failed-to-start process so the queue drains.
205
  if (i.app.isEmpty()) {
288 ✔
206
    qCDebug(lcQtPass) << "No executable set for:" << i.id;
5 ✔
207
    const int failedId = i.id;
5 ✔
208
    running = false;
5 ✔
209
    m_execQueue.dequeue();
5 ✔
210
    ++m_pendingErrors;
5 ✔
211
    QMetaObject::invokeMethod(
5 ✔
212
        this,
213
        [this, failedId]() {
5 ✔
214
          emit error(failedId, -1, QString(),
10 ✔
215
                     tr("No executable configured for this command"));
5 ✔
216
          --m_pendingErrors;
5 ✔
217
          postDrainedIfIdle();
5 ✔
218
        },
5 ✔
219
        Qt::QueuedConnection);
220
    executeNext();
5 ✔
221
    return;
222
  }
223

224
  // Always set it: an item without a directory runs in the application's
225
  // cwd, not wherever the previous item happened to run.
226
  m_process.setWorkingDirectory(i.workingDir);
283 ✔
227
  startProcess(m_process, i.app, i.args);
283 ✔
228

229
  // A failed start never emits finished(), so `running` would stall the queue
230
  // and waiters (the keygen dialog) would hang. The emit is queued so it does
231
  // not re-enter KeygenDialog::done(), still on the stack; -1 goes through
232
  // Pass::finished's non-zero error gate.
233
  if (!m_process.waitForStarted(-1)) {
283 ✔
234
    qCDebug(lcQtPass) << "Process failed to start:" << i.id << " " << i.app;
5 ✔
235
    const int failedId = i.id;
5 ✔
236
    const QString failedApp = i.app;
237
    m_process.closeWriteChannel();
5 ✔
238
    running = false;
5 ✔
239
    m_execQueue.dequeue();
5 ✔
240
    ++m_pendingErrors;
5 ✔
241
    QMetaObject::invokeMethod(
5 ✔
242
        this,
243
        [this, failedId, failedApp]() {
10 ✔
244
          emit error(failedId, -1, QString(),
15 ✔
245
                     tr("Failed to start %1").arg(failedApp));
10 ✔
246
          --m_pendingErrors;
5 ✔
247
          postDrainedIfIdle();
5 ✔
248
        },
5 ✔
249
        Qt::QueuedConnection);
250
    executeNext();
5 ✔
251
    return;
252
  }
253

254
  if (!i.input.isEmpty()) {
278 ✔
255
    QByteArray data = i.input.toUtf8();
256
    if (m_process.write(data) != data.length()) {
78 ✔
257
      qCDebug(lcQtPass) << "Not all data written to process:" << i.id << " "
×
258
                        << i.app;
×
259
    }
260
  }
261
  m_process.closeWriteChannel();
278 ✔
262
}
288 ✔
263

264
void Executor::execute(int id, const QString &app, const QStringList &args,
83 ✔
265
                       bool readStdout, bool readStderr) {
266
  execute(id, QString(), app, args, QString(), readStdout, readStderr);
166 ✔
267
}
83 ✔
268

269
void Executor::execute(int id, const QString &workDir, const QString &app,
2 ✔
270
                       const QStringList &args, bool readStdout,
271
                       bool readStderr) {
272
  execute(id, workDir, app, args, QString(), readStdout, readStderr);
2 ✔
273
}
2 ✔
274

275
void Executor::execute(int id, const QString &app, const QStringList &args,
1 ✔
276
                       QString input, bool readStdout, bool readStderr) {
277
  execute(id, QString(), app, args, std::move(input), readStdout, readStderr);
2 ✔
278
}
1 ✔
279

280
void Executor::execute(int id, const QString &workDir, const QString &app,
296 ✔
281
                       const QStringList &args, QString input, bool readStdout,
282
                       bool readStderr) {
283
  // Queue even an empty executable: executeNext() reports it (#1682).
284
  // startProcess resolves the executable, as on the blocking path.
285
  m_execQueue.push_back(
296 ✔
286
      {id, app, args, std::move(input), readStdout, readStderr, workDir});
287
  executeNext();
296 ✔
288
}
888 ✔
289

290
// UTF-8 first; on a decoding error fall back to the system encoding.
291
// lossless, when given, says whether the text encodes back to exactly these
292
// bytes: what writing it to gpg or pass (toUtf8(), as every writer here
293
// does) would store. Not for bytes that are not UTF-8 (each became U+FFFD,
294
// or on Windows a code-page character), nor for a leading byte order mark,
295
// which the decoder drops without an error. A literal U+FFFD, as older
296
// QtPass versions left in such entries, does encode back.
297
static auto decodeAssumingUtf8(const QByteArray &in, bool *lossless = nullptr)
1,753 ✔
298
    -> QString {
299
  // Stateless: a truncated trailing byte becomes a replacement character
300
  // instead of being held back for a continuation that never comes.
301
  auto converter =
1,753 ✔
302
      QStringDecoder(QStringDecoder::Utf8, QStringDecoder::Flag::Stateless);
303
  QString out = converter(in);
1,753 ✔
304
  if (converter.hasError()) {
1,753 ✔
305
    auto fallback =
4 ✔
306
        QStringDecoder(QStringDecoder::System, QStringDecoder::Flag::Stateless);
307
    out = fallback(in);
8 ✔
308
  }
309
  if (lossless != nullptr) {
1,753 ✔
310
    *lossless = out.toUtf8() == in;
226 ✔
311
  }
312
  return out;
1,753 ✔
313
}
314

315
// Returns an error code rather than throwing, matching QtPass's error
316
// handling elsewhere.
317
namespace {
318

319
/**
320
 * @brief Stop a running @p process from the thread that owns it:
321
 * terminate(), then kill() if it is still running after a grace period,
322
 * then at most one more grace period for it to exit.
323
 * A console gpg ignores terminate() on Windows (WM_CLOSE), hence the kill.
324
 */
325
void stopBlocking(QProcess &process) {
14 ✔
326
  process.terminate();
14 ✔
327
  if (process.waitForFinished(kBlockingKillGraceMs)) {
14 ✔
328
    return;
329
  }
330
  process.kill();
3 ✔
331
  // SIGKILL cannot be ignored, but a child in uninterruptible sleep (a hung
332
  // NFS or FUSE mount) dies only when the kernel lets it. Wait one more grace
333
  // period, not forever: the run is reported as failed either way, and a
334
  // blocking run then lasts at most its timeout plus twice the grace.
335
  if (!process.waitForFinished(kBlockingKillGraceMs)) {
3 ✔
336
    qCWarning(lcQtPass) << "Process still running after kill():"
×
337
                        << process.program();
×
338
  }
339
}
340

341
/**
342
 * @brief What is left of @p deadline, as QProcess's wait functions take it:
343
 * -1 for none.
344
 */
345
auto msecsLeft(const QDeadlineTimer &deadline) -> int {
2,390 ✔
346
  if (deadline.isForever()) {
2,390 ✔
347
    return -1;
348
  }
349
  return static_cast<int>(
350
      std::clamp<qint64>(deadline.remainingTime(), 0, INT_MAX));
56 ✔
351
}
352

353
/**
354
 * @brief Wait for @p process to finish, stopping it when @p cancel is set
355
 * or @p deadline passes.
356
 *
357
 * Polls the flag; every QProcess call, terminate()/kill() included, stays on
358
 * this thread, so another thread can never act on an exited process (or a
359
 * pid the OS has since reused).
360
 * @return false when it was cancelled or ran out of time.
361
 */
362
auto waitOrCancel(QProcess &process, const std::atomic_bool *cancel,
1,360 ✔
363
                  const QDeadlineTimer &deadline) -> bool {
364
  if (cancel == nullptr) {
1,360 ✔
365
    // waitForFinished() can also fail (a poll error) with the child still
366
    // running; that is not completion either. Stop it rather than report
367
    // it done.
368
    if (!process.waitForFinished(msecsLeft(deadline)) &&
974 ✔
369
        process.state() != QProcess::NotRunning) {
3 ✔
370
      stopBlocking(process);
3 ✔
371
      return false;
3 ✔
372
    }
373
    return true;
968 ✔
374
  }
375
  while (!process.waitForFinished(
822 ✔
376
      deadline.isForever()
377
          ? kBlockingCancelPollMs
378
          : qMin(kBlockingCancelPollMs, msecsLeft(deadline)))) {
3 ✔
379
    if (process.state() == QProcess::NotRunning) {
33 ✔
380
      return true;
381
    }
382
    if (cancel->load() || deadline.hasExpired()) {
44 ✔
383
      stopBlocking(process);
11 ✔
384
      return false;
11 ✔
385
    }
386
  }
387
  return true;
388
}
389

390
} // namespace
391

392
auto Executor::runBlocking(QProcess &process, const QString &app,
1,227 ✔
393
                           const QStringList &args, const QString &input,
394
                           QString *process_out, QString *process_err,
395
                           const std::atomic_bool *cancel, int timeoutMs)
396
    -> int {
397
  QByteArray out;
1,227 ✔
398
  const int rc = runBlockingRaw(process, app, args, input.toUtf8(),
2,454 ✔
399
                                process_out != nullptr ? &out : nullptr,
400
                                process_err, cancel, timeoutMs);
401
  if (process_out != nullptr) {
1,227 ✔
402
    *process_out = decodeAssumingUtf8(out);
2,240 ✔
403
  }
404
  return rc;
1,227 ✔
405
}
406

407
auto Executor::runBlockingRaw(QProcess &process, const QString &app,
1,424 ✔
408
                              const QStringList &args, const QByteArray &input,
409
                              QByteArray *process_out, QString *process_err,
410
                              const std::atomic_bool *cancel, int timeoutMs)
411
    -> int {
412
  if (cancel != nullptr && cancel->load())
1,424 ✔
413
    return -1;
414
  const QDeadlineTimer deadline = timeoutMs < 0
415
                                      ? QDeadlineTimer(QDeadlineTimer::Forever)
1,416 ✔
416
                                      : QDeadlineTimer(timeoutMs);
1,416 ✔
417
  startProcess(process, app, args);
1,416 ✔
418
  if (!process.waitForStarted(msecsLeft(deadline))) {
1,416 ✔
419
    qCDebug(lcQtPass) << "Process failed to start:" << app;
56 ✔
420
    if (process.state() != QProcess::NotRunning) {
56 ✔
421
      stopBlocking(process);
×
422
    }
423
    return -1;
56 ✔
424
  }
425
  if (!input.isEmpty()) {
1,360 ✔
426
    if (process.write(input) != input.length()) {
198 ✔
UNCOV
427
      qCDebug(lcQtPass) << "Not all input written:" << app;
×
428
    }
429
  }
430
  // Always close stdin so a child blocking on EOF doesn't hang.
431
  process.closeWriteChannel();
1,360 ✔
432
  if (!waitOrCancel(process, cancel, deadline)) {
1,360 ✔
433
    return -1;
434
  }
435
  // Read before judging the exit: a process that crashed may have said why,
436
  // as the queued path (onProcessFinished) keeps it too.
437
  if (process_out != nullptr) {
1,346 ✔
438
    *process_out = process.readAllStandardOutput();
2,386 ✔
439
  }
440
  if (process_err != nullptr) {
1,346 ✔
441
    *process_err = decodeAssumingUtf8(process.readAllStandardError());
398 ✔
442
  }
443
  return process.exitStatus() == QProcess::NormalExit ? process.exitCode() : -1;
1,346 ✔
444
}
445

446
auto Executor::executeBlockingRaw(QProcess &process, const QString &app,
197 ✔
447
                                  const QStringList &args,
448
                                  const QByteArray &input,
449
                                  QByteArray *process_out, QString *process_err,
450
                                  const std::atomic_bool *cancel, int timeoutMs)
451
    -> int {
452
  return runBlockingRaw(process, app, args, input, process_out, process_err,
197 ✔
453
                        cancel, timeoutMs);
197 ✔
454
}
455

456
auto Executor::executeBlocking(const QString &app, const QStringList &args,
242 ✔
457
                               const QString &input, QString *process_out,
458
                               QString *process_err) -> int {
459
  QProcess internal;
242 ✔
460
  return runBlocking(internal, app, args, input, process_out, process_err);
484 ✔
461
}
242 ✔
462

463
auto Executor::executeBlocking(QProcess &process, const QString &app,
949 ✔
464
                               const QStringList &args, const QString &input,
465
                               QString *process_out, QString *process_err,
466
                               const std::atomic_bool *cancel, int timeoutMs)
467
    -> int {
468
  return runBlocking(process, app, args, input, process_out, process_err,
949 ✔
469
                     cancel, timeoutMs);
949 ✔
470
}
471

472
auto Executor::executeBlocking(const QString &app, const QStringList &args,
206 ✔
473
                               QString *process_out, QString *process_err)
474
    -> int {
475
  return executeBlocking(app, args, QString(), process_out, process_err);
412 ✔
476
}
477

478
auto Executor::executeBlocking(const QProcessEnvironment &env,
36 ✔
479
                               const QString &app, const QStringList &args,
480
                               QString *process_out, QString *process_err)
481
    -> int {
482
  QProcess process;
36 ✔
483
  process.setProcessEnvironment(env);
36 ✔
484
  return runBlocking(process, app, args, QString(), process_out, process_err);
72 ✔
485
}
36 ✔
486

487
void Executor::setEnvironment(const QProcessEnvironment &env) {
557 ✔
488
  m_process.setProcessEnvironment(env);
557 ✔
489
}
557 ✔
490

491
auto Executor::environment() const -> QProcessEnvironment {
84 ✔
492
  return m_process.processEnvironment();
84 ✔
493
}
494

495
// Also -1 while the head is already running: that one is not cancelled.
496
auto Executor::cancelNext() -> int {
12 ✔
497
  if (running || m_execQueue.isEmpty()) {
12 ✔
498
    return -1;
499
  }
500
  const int id = m_execQueue.dequeue().id;
8 ✔
501
  // As in executeNext(): while the signal is out, a slot's cancelNext()
502
  // drops nothing and its execute() only queues. Nothing is started here
503
  // afterwards: the caller (ImitatePass dropping the rest of a failed
504
  // transaction from its completion handler) may cancel again, and the
505
  // queue moves on when that handler returns (onProcessFinished()).
506
  running = true;
8 ✔
507
  emit dequeuing(id, false);
8 ✔
508
  running = false;
8 ✔
509
  return id;
8 ✔
510
}
511

512
void Executor::onProcessFinished(int exitCode,
277 ✔
513
                                 QProcess::ExitStatus exitStatus) {
514
  ExecQueueItem i = m_execQueue.dequeue();
515
  running = false;
277 ✔
516
  auto [output, err] = collectOutput(i, exitCode);
277 ✔
517
  if (exitStatus == QProcess::NormalExit) {
277 ✔
518
    if (exitCode != 0) {
274 ✔
519
      qCDebug(lcQtPass) << i.app << "exited with" << exitCode << err;
40 ✔
520
    }
521
    emit finished(i.id, exitCode, output, err);
274 ✔
522
    m_outputLossless = true;
274 ✔
523
  } else {
524
    // A signal-killed process usually leaves stderr empty; without this the
525
    // password pane would show nothing at all for the failure.
526
    if (err.trimmed().isEmpty()) {
3 ✔
527
      err = tr("%1 crashed or was killed").arg(i.app);
6 ✔
528
    }
529
    // Qt leaves exitCode undefined after a CrashExit (the signal number on
530
    // Unix), and Pass::finished gates on it: 0 would pass as success and 1
531
    // as grep's "no matches". Report -1, like the failed-to-start path.
532
    emit error(i.id, -1, output, err);
3 ✔
533
    m_outputLossless = true;
3 ✔
534
  }
535
  executeNext();
277 ✔
536
  postDrainedIfIdle();
277 ✔
537
}
277 ✔
538

539
void Executor::postDrainedIfIdle() {
287 ✔
540
  // One check at a time, run after what is emitted now; it fires only once
541
  // nothing runs or waits and every posted error() of a failed start is
542
  // out (the last of those posts the check again).
543
  if (m_drainCheckPosted) {
287 ✔
544
    return;
545
  }
546
  m_drainCheckPosted = true;
283 ✔
547
  QMetaObject::invokeMethod(
283 ✔
548
      this,
549
      [this]() {
283 ✔
550
        m_drainCheckPosted = false;
227 ✔
551
        if (isIdle() && m_pendingErrors == 0) {
116 ✔
552
          emit drained();
116 ✔
553
        }
554
      },
227 ✔
555
      Qt::QueuedConnection);
556
}
557

558
auto Executor::collectOutput(const ExecQueueItem &item, int exitCode)
277 ✔
559
    -> std::pair<QString, QString> {
560
  QString output;
277 ✔
561
  QString err;
277 ✔
562
  m_outputLossless = true;
277 ✔
563
  if (item.readStdout) {
277 ✔
564
    output = decodeAssumingUtf8(m_process.readAllStandardOutput(),
452 ✔
565
                                &m_outputLossless);
566
  }
567
  if (item.readStderr || exitCode != 0) {
277 ✔
568
    err = decodeAssumingUtf8(m_process.readAllStandardError());
416 ✔
569
  }
570
  return {output, err};
277 ✔
571
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc