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

IJHack / QtPass / 43482519769

13 Nov 2026 06:25PM UTC coverage: 68.466% (+0.05%) from 68.419%
43482519769

push

github

web-flow
coverage: let lcov ignore inconsistent data on system headers (#2138)

lcov 2.x on the CI runner (gcc 15 headers) fails the coverage job with
"lcov: ERROR: (inconsistent) ...: function ... is not hit but line ... has
a non-zero hit count" on files outside the project (e.g. /usr/include/c++/15
and Qt headers), aborting lcov_prepare before coverage of src/ is produced.

The captures already pass "--ignore-errors unused"; add "inconsistent" the
same way. Only the consistency check on data filtered out anyway (-e src/*)
is bypassed; src/ coverage is unchanged. Backport of #2136.

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

5276 of 7706 relevant lines covered (68.47%)

72.38 hits per line

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

97.64
/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 <QDir>
6
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
7
#include <QTextCodec>
8
#endif
9
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
10
#include <QStringDecoder>
11
#endif
12
#include <utility>
13

14
#ifdef QT_DEBUG
15
#include "debughelper.h"
16
#endif
17

18
/**
19
 * @brief Executor::Executor executes external applications
20
 * @param parent
21
 */
22
Executor::Executor(QObject *parent) : QObject(parent) {
95 ✔
23
  connect(&m_process,
95 ✔
24
          static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(
25
              &QProcess::finished),
26
          this,
27
          static_cast<void (Executor::*)(int, QProcess::ExitStatus)>(
95 ✔
28
              &Executor::finished));
29
  connect(&m_process, &QProcess::started, this, &Executor::starting);
95 ✔
30
}
95 ✔
31

32
/**
33
 * @brief Executor::detachFromTerminal makes @p process start its child
34
 * without a controlling terminal.
35
 * @param process Process to configure; call it before start().
36
 */
37
void Executor::detachFromTerminal(QProcess &process) {
213 ✔
38
#if defined(Q_OS_UNIX) && QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
39
  // A child inherits QtPass's controlling terminal when QtPass was started
40
  // from one. git (HTTPS credentials) and ssh (key passphrase, host key) then
41
  // prompt on that /dev/tty, and as the queue runs one command at a time,
42
  // every decrypt and save waits behind a prompt nobody sees: QtPass looks
43
  // frozen. In a session of its own the child has no terminal, so git and
44
  // ssh use SSH_ASKPASS/GIT_ASKPASS when set and otherwise fail with an
45
  // error QtPass shows. GIT_TERMINAL_PROMPT=0 is not set on top: without a
46
  // tty git has nothing to prompt on, and some credential helpers read it as
47
  // "no interaction at all" and would skip their own dialogs.
48
  //
49
  // gpg is unaffected: pinentry is started by gpg-agent, which finds the
50
  // terminal through GPG_TTY (set by the user's shell) or the tty on gpg's
51
  // stdin, here always a pipe. pass is a shell script running that gpg and
52
  // git, so the same holds for it.
53
  //
54
  // Qt before 6.7 has no such flag; builds against it keep the old
55
  // behaviour.
56
  QProcess::UnixProcessParameters params = process.unixProcessParameters();
213 ✔
57
  params.flags |= QProcess::UnixProcessFlag::CreateNewSession;
58
  process.setUnixProcessParameters(params);
213 ✔
59
#else
60
  Q_UNUSED(process);
61
#endif
62
}
213 ✔
63

64
/**
65
 * @brief Executor::startProcess starts the internal process, handling WSL
66
 * prefixes.
67
 * @param app Executable path (may start with "wsl ").
68
 * @param args Arguments to pass to the executable.
69
 */
70
void Executor::startProcess(const QString &app, const QStringList &args) {
81 ✔
71
  detachFromTerminal(m_process);
81 ✔
72
  if (app.startsWith("wsl ")) {
162 ✔
73
    m_process.start("wsl", wslExecArgs(app.mid(4), args));
3 ✔
74
  } else {
75
    m_process.start(app, args);
80 ✔
76
  }
77
}
81 ✔
78

79
/**
80
 * @brief Executor::startProcessBlocking starts a given process, handling WSL
81
 * prefixes.
82
 * @param internal QProcess reference to start.
83
 * @param app Executable path (may start with "wsl ").
84
 * @param args Arguments to pass to the executable.
85
 */
86
void Executor::startProcessBlocking(QProcess &internal, const QString &app,
131 ✔
87
                                    const QStringList &args) {
88
  detachFromTerminal(internal);
131 ✔
89
  if (app.startsWith("wsl ")) {
262 ✔
90
    internal.start("wsl", wslExecArgs(app.mid(4), args));
3 ✔
91
  } else {
92
    internal.start(app, args);
130 ✔
93
  }
94
}
131 ✔
95

96
/**
97
 * @brief Executor::wslExecArgs builds the wsl.exe argv for @p command.
98
 *
99
 * `wsl <command> <args>` hands the joined command line to the default Linux
100
 * shell, which word-splits and expands `$()` in every argument. `--exec`
101
 * makes WSL launch the binary directly so arguments arrive verbatim.
102
 * @param command Linux command to run.
103
 * @param args Arguments for @p command.
104
 * @return `--exec`, @p command, then @p args.
105
 */
106
auto Executor::wslExecArgs(const QString &command, const QStringList &args)
4 ✔
107
    -> QStringList {
108
  QStringList wslArgs = args;
109
  wslArgs.prepend(command);
110
  wslArgs.prepend(QStringLiteral("--exec"));
4 ✔
111
  return wslArgs;
4 ✔
112
}
113

114
/**
115
 * @brief Executor::executeNext consumes executable tasks from the queue
116
 */
117
void Executor::executeNext() {
162 ✔
118
  if (running || m_execQueue.isEmpty()) {
163 ✔
119
    return;
120
  }
121
  const execQueueItem &i = m_execQueue.head();
122

123
  // An empty executable can never produce a finished() signal. Silently
124
  // dropping it used to wedge the queue: the command stayed at the head until
125
  // the next completion, whose signal only caused the dequeue; a second
126
  // completion then stalled everything (#1682). Fail it through the same
127
  // deferred path as a failed-to-start process so the queue keeps draining.
128
  if (i.app.isEmpty()) {
82 ✔
129
#ifdef QT_DEBUG
130
    dbg() << "No executable set for:" << i.id;
131
#endif
132
    // Capture before dequeue() invalidates the head reference.
133
    const int failedId = i.id;
1 ✔
134
    m_execQueue.dequeue();
1 ✔
135
    QMetaObject::invokeMethod(
1 ✔
136
        this,
137
        [this, failedId]() {
1 ✔
138
          emit error(failedId, -1, QString(),
2 ✔
139
                     tr("No executable configured for this command"));
1 ✔
140
        },
1 ✔
141
        Qt::QueuedConnection);
142
    executeNext();
143
    return;
1 ✔
144
  }
145

146
  running = true;
81 ✔
147
  if (!i.workingDir.isEmpty()) {
81 ✔
148
    m_process.setWorkingDirectory(i.workingDir);
65 ✔
149
  }
150
  startProcess(i.app, i.args);
81 ✔
151

152
  // Confirm the process actually started, regardless of whether it takes stdin.
153
  // A process that fails to start emits errorOccurred(FailedToStart) but never
154
  // finished(), so without this check `running` would stay true forever and
155
  // stall the whole queue (and, for stdin commands, the input would be dropped
156
  // silently). Surface the failure so callers waiting on a finished/error
157
  // signal (e.g. the GPG keygen dialog) do not hang. Defer the emit via a
158
  // queued call so we do not re-enter a caller still on the stack —
159
  // KeygenDialog::done() drives key generation synchronously — which mirrors
160
  // the normal asynchronous QProcess::finished path. A -1 exit code routes
161
  // through Pass::finished's non-zero error gate.
162
  if (!m_process.waitForStarted(-1)) {
81 ✔
163
#ifdef QT_DEBUG
164
    dbg() << "Process failed to start:" << i.id << " " << i.app;
165
#endif
166
    // Capture before dequeue() invalidates the head reference.
167
    const int failedId = i.id;
2 ✔
168
    const QString failedApp = i.app;
169
    m_process.closeWriteChannel();
2 ✔
170
    running = false;
2 ✔
171
    m_execQueue.dequeue();
2 ✔
172
    QMetaObject::invokeMethod(
2 ✔
173
        this,
174
        [this, failedId, failedApp]() {
4 ✔
175
          emit error(failedId, -1, QString(),
6 ✔
176
                     tr("Failed to start %1").arg(failedApp));
4 ✔
177
        },
2 ✔
178
        Qt::QueuedConnection);
179
    executeNext();
2 ✔
180
    return;
181
  }
182

183
  if (!i.input.isEmpty()) {
79 ✔
184
    QByteArray data = i.input.toUtf8();
38 ✔
185
    if (m_process.write(data) != data.length()) {
38 ✔
186
#ifdef QT_DEBUG
187
      dbg() << "Not all data written to process:" << i.id << " " << i.app;
188
#endif
189
    }
190
  }
191
  m_process.closeWriteChannel();
79 ✔
192
}
193

194
/**
195
 * @brief Executor::execute execute an app
196
 * @param id
197
 * @param app
198
 * @param args
199
 * @param readStdout
200
 * @param readStderr
201
 */
202
void Executor::execute(int id, const QString &app, const QStringList &args,
16 ✔
203
                       bool readStdout, bool readStderr) {
204
  execute(id, QString(), app, args, QString(), readStdout, readStderr);
32 ✔
205
}
16 ✔
206

207
/**
208
 * @brief Executor::execute executes an app from a workDir
209
 * @param id
210
 * @param workDir
211
 * @param app
212
 * @param args
213
 * @param readStdout
214
 * @param readStderr
215
 */
216
void Executor::execute(int id, const QString &workDir, const QString &app,
1 ✔
217
                       const QStringList &args, bool readStdout,
218
                       bool readStderr) {
219
  execute(id, workDir, app, args, QString(), readStdout, readStderr);
1 ✔
220
}
1 ✔
221

222
/**
223
 * @brief Executor::execute an app, takes input and presents it as stdin
224
 * @param id
225
 * @param app
226
 * @param args
227
 * @param input
228
 * @param readStdout
229
 * @param readStderr
230
 */
231
void Executor::execute(int id, const QString &app, const QStringList &args,
1 ✔
232
                       QString input, bool readStdout, bool readStderr) {
233
  execute(id, QString(), app, args, std::move(input), readStdout, readStderr);
2 ✔
234
}
1 ✔
235

236
/**
237
 * @brief Executor::execute  executes an app from a workDir, takes input and
238
 * presents it as stdin
239
 * @param id
240
 * @param workDir
241
 * @param app
242
 * @param args
243
 * @param input
244
 * @param readStdout
245
 * @param readStderr
246
 */
247
void Executor::execute(int id, const QString &workDir, const QString &app,
82 ✔
248
                       const QStringList &args, QString input, bool readStdout,
249
                       bool readStderr) {
250
  // An empty executable (e.g. git not configured yet) used to be dropped
251
  // here. That left its command permanently at the head of the queue: no
252
  // process ever runs, no finished()/error() is emitted, and every later
253
  // completion signal is swallowed for the rest of the session (#1682).
254
  // ExecuteNext() now surfaces it as an error instead, so keep queueing it.
255
  QString appPath = app;
256
  if (!appPath.isEmpty() && !appPath.startsWith("wsl ")) {
163 ✔
257
    appPath =
258
        QDir(QCoreApplication::applicationDirPath()).absoluteFilePath(app);
240 ✔
259
  }
260
  m_execQueue.push_back(
82 ✔
261
      {id, appPath, args, std::move(input), readStdout, readStderr, workDir});
262
  executeNext();
82 ✔
263
}
246 ✔
264

265
/**
266
 * @brief decodes the input into a string assuming UTF-8 encoding.
267
 * If this fails (which is likely if it is not actually UTF-8)
268
 * it will then fall back to Qt's decoding function, which
269
 * will try based on BOM and if that fails fall back to local encoding.
270
 * This should not be needed in Qt6
271
 *
272
 * @param in input data
273
 * @return Input bytes decoded to string
274
 */
275
static auto decodeAssumingUtf8(const QByteArray &in) -> QString {
294 ✔
276
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
277
  QTextCodec *codec = QTextCodec::codecForName("UTF-8");
278
  QTextCodec::ConverterState state;
279
  QString out = codec->toUnicode(in.constData(), in.size(), &state);
280
  if (state.invalidChars == 0) {
281
    return out;
282
  }
283
  codec = QTextCodec::codecForUtfText(in);
284
  return codec->toUnicode(in);
285
#else
286
  auto converter = QStringDecoder(QStringDecoder::Utf8);
294 ✔
287
  QString out = converter(in);
294 ✔
288
  if (!converter.hasError()) {
294 ✔
289
    return out;
294 ✔
290
  }
291
  // Fallback if UTF-8 decoding failed - try system encoding
292
  auto fallback = QStringDecoder(QStringDecoder::System);
×
293
  return fallback(in);
×
294
#endif
295
}
296

297
/**
298
 * @brief Executor::executeBlocking blocking version of the executor,
299
 * takes input and presents it as stdin
300
 * @param app
301
 * @param args
302
 * @param input
303
 * @param process_out
304
 * @param process_err
305
 * @return
306
 *
307
 * Note: Returning error code instead of throwing to maintain compatibility
308
 * with the existing error handling pattern used throughout QtPass.
309
 */
310
auto Executor::runBlocking(QProcess &process, const QString &app,
131 ✔
311
                           const QStringList &args, const QString &input,
312
                           QString *process_out, QString *process_err) -> int {
313
  startProcessBlocking(process, app, args);
131 ✔
314
  if (!process.waitForStarted(-1)) {
131 ✔
315
#ifdef QT_DEBUG
316
    dbg() << "Process failed to start:" << app;
317
#endif
318
    return -1;
319
  }
320
  if (!input.isEmpty()) {
128 ✔
321
    QByteArray data = input.toUtf8();
322
    if (process.write(data) != data.length()) {
16 ✔
323
#ifdef QT_DEBUG
324
      dbg() << "Not all input written:" << app;
325
#endif
326
    }
327
  }
328
  // Always close stdin so a child blocking on EOF doesn't hang when no
329
  // input is written (these are one-shot blocking runs that never stream).
330
  process.closeWriteChannel();
128 ✔
331
  process.waitForFinished(-1);
128 ✔
332
  if (process.exitStatus() != QProcess::NormalExit) {
128 ✔
333
    // Process failed to start or crashed; return -1 to indicate error.
334
    // The calling code checks for non-zero exit codes for error handling.
335
    return -1;
336
  }
337
  if (process_out != nullptr) {
128 ✔
338
    *process_out = decodeAssumingUtf8(process.readAllStandardOutput());
212 ✔
339
  }
340
  if (process_err != nullptr) {
128 ✔
341
    *process_err = decodeAssumingUtf8(process.readAllStandardError());
90 ✔
342
  }
343
  return process.exitCode();
128 ✔
344
}
345

346
auto Executor::executeBlocking(const QString &app, const QStringList &args,
115 ✔
347
                               const QString &input, QString *process_out,
348
                               QString *process_err) -> int {
349
  QProcess internal;
115 ✔
350
  return runBlocking(internal, app, args, input, process_out, process_err);
230 ✔
351
}
115 ✔
352

353
/**
354
 * @brief Executor::executeBlocking blocking version of the executor
355
 * @param app
356
 * @param args
357
 * @param process_out
358
 * @param process_err
359
 * @return
360
 */
361
auto Executor::executeBlocking(const QString &app, const QStringList &args,
80 ✔
362
                               QString *process_out, QString *process_err)
363
    -> int {
364
  return executeBlocking(app, args, QString(), process_out, process_err);
160 ✔
365
}
366

367
/**
368
 * @brief Executor::executeBlocking blocking version with custom environment
369
 * @param env Environment variables to set
370
 * @param app Executable path
371
 * @param args Arguments
372
 * @param process_out Standard output
373
 * @param process_err Standard error
374
 * @return Exit code
375
 */
376
auto Executor::executeBlocking(const QProcessEnvironment &env,
16 ✔
377
                               const QString &app, const QStringList &args,
378
                               QString *process_out, QString *process_err)
379
    -> int {
380
  QProcess process;
16 ✔
381
  process.setProcessEnvironment(env);
16 ✔
382
  return runBlocking(process, app, args, QString(), process_out, process_err);
32 ✔
383
}
16 ✔
384

385
/**
386
 * @brief Executor::setEnvironment set environment variables
387
 * for executor processes
388
 * @param env
389
 */
390
void Executor::setEnvironment(const QProcessEnvironment &env) {
72 ✔
391
  m_process.setProcessEnvironment(env);
72 ✔
392
}
72 ✔
393

394
auto Executor::environment() const -> QProcessEnvironment {
19 ✔
395
  return m_process.processEnvironment();
19 ✔
396
}
397

398
/**
399
 * @brief Executor::cancelNext  cancels execution of first process in queue
400
 *                              if it's not already running
401
 *
402
 * @return  id of the cancelled process or -1 on error
403
 */
404
auto Executor::cancelNext() -> int {
3 ✔
405
  if (running || m_execQueue.isEmpty()) {
3 ✔
406
    return -1; // Return -1 to indicate no process was cancelled
407
               // (queue empty or currently executing).
408
  }
409
  return m_execQueue.dequeue().id;
×
410
}
411

412
/**
413
 * @brief Executor::finished called when an executed process finishes
414
 * @param exitCode
415
 * @param exitStatus
416
 */
417
void Executor::finished(int exitCode, QProcess::ExitStatus exitStatus) {
78 ✔
418
  execQueueItem i = m_execQueue.dequeue();
419
  running = false;
78 ✔
420
  if (exitStatus == QProcess::NormalExit) {
78 ✔
421
    QString output;
76 ✔
422
    QString err;
76 ✔
423
    if (i.readStdout) {
76 ✔
424
      output = decodeAssumingUtf8(m_process.readAllStandardOutput());
148 ✔
425
    }
426
    if (i.readStderr || exitCode != 0) {
76 ✔
427
      err = decodeAssumingUtf8(m_process.readAllStandardError());
132 ✔
428
      if (exitCode != 0) {
429
#ifdef QT_DEBUG
430
        dbg() << exitCode << err;
431
#endif
432
      }
433
    }
434
    emit finished(i.id, exitCode, output, err);
76 ✔
435
  } else {
436
    QString output;
2 ✔
437
    QString err;
2 ✔
438
    if (i.readStdout) {
2 ✔
439
      output = decodeAssumingUtf8(m_process.readAllStandardOutput());
2 ✔
440
    }
441
    if (i.readStderr || exitCode != 0) {
2 ✔
442
      err = decodeAssumingUtf8(m_process.readAllStandardError());
4 ✔
443
    }
444
    emit error(i.id, exitCode, output, err);
2 ✔
445
  }
446
  executeNext();
78 ✔
447
}
78 ✔
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