Coverage Report

Created: 2025-06-10 13:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/bitcoin/src/common/run_command.cpp
Line
Count
Source
1
// Copyright (c) 2022 The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#include <bitcoin-build-config.h> // IWYU pragma: keep
6
7
#include <common/run_command.h>
8
9
#include <tinyformat.h>
10
#include <univalue.h>
11
12
#ifdef ENABLE_EXTERNAL_SIGNER
13
#include <util/subprocess.h>
14
#endif // ENABLE_EXTERNAL_SIGNER
15
16
UniValue RunCommandParseJSON(const std::string& str_command, const std::string& str_std_in)
17
0
{
18
0
#ifdef ENABLE_EXTERNAL_SIGNER
19
0
    namespace sp = subprocess;
20
21
0
    UniValue result_json;
22
0
    std::istringstream stdout_stream;
23
0
    std::istringstream stderr_stream;
24
25
0
    if (str_command.empty()) return UniValue::VNULL;
  Branch (25:9): [True: 0, False: 0]
26
27
0
    auto c = sp::Popen(str_command, sp::input{sp::PIPE}, sp::output{sp::PIPE}, sp::error{sp::PIPE}, sp::close_fds{true});
28
0
    if (!str_std_in.empty()) {
  Branch (28:9): [True: 0, False: 0]
29
0
        c.send(str_std_in);
30
0
    }
31
0
    auto [out_res, err_res] = c.communicate();
32
0
    stdout_stream.str(std::string{out_res.buf.begin(), out_res.buf.end()});
33
0
    stderr_stream.str(std::string{err_res.buf.begin(), err_res.buf.end()});
34
35
0
    std::string result;
36
0
    std::string error;
37
0
    std::getline(stdout_stream, result);
38
0
    std::getline(stderr_stream, error);
39
40
0
    const int n_error = c.retcode();
41
0
    if (n_error) throw std::runtime_error(strprintf("RunCommandParseJSON error: process(%s) returned %d: %s\n", str_command, n_error, error));
  Branch (41:9): [True: 0, False: 0]
42
0
    if (!result_json.read(result)) throw std::runtime_error("Unable to parse JSON: " + result);
  Branch (42:9): [True: 0, False: 0]
43
44
0
    return result_json;
45
#else
46
    throw std::runtime_error("Compiled without external signing support (required for external signing).");
47
#endif // ENABLE_EXTERNAL_SIGNER
48
0
}