mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-04-18 11:37:40 +02:00
Merge bitcoin/bitcoin#34349: util: Remove brittle and confusing sp::Popen(std::string)
fa48d42163test: Stricter unit test (MarcoFalke)fa626bd143util: Remove brittle and confusing sp::Popen(std::string) (MarcoFalke) Pull request description: The subprocess Popen call that accepts a full `std::string` has many issues: * It promotes brittle and broken code, where spaces are not properly quoted. Example: https://github.com/bitcoin/bitcoin/pull/33929#discussion_r2590523065 * The internally used `util::split` function does incorrectly split on spaces, instead of using `shlex.split`. * It is redundant and not needed, because a vector interface already exists. Fix all issues by removing it and just using the vector interface. This pull request should not change any behavior: Note that the command taken from `gArgs.GetArg("-signer", "")` is still passed through the `sp::util::split` helper, just like before. Fixing that is left for a follow-up, so that this change here is basically just a refactor. This also fixes a unit test bug as a side-effect: Fixes https://github.com/bitcoin/bitcoin/issues/32574. ACKs for top commit: janb84: cr ACKfa48d42163fjahr: Code review ACKfa48d42163hebasto: re-ACKfa48d42163. Tree-SHA512: 3d29226977c9392502f9361e2bd42b471ad03761bbf6a94ef6e545cbe4492ad5858da1ac9cc64b2791aacb9b6e6f3c3f63dbcc3a2bf45f6a13b5bc33eddf8c2b
This commit is contained in:
@@ -8,12 +8,13 @@
|
||||
|
||||
#include <tinyformat.h>
|
||||
#include <univalue.h>
|
||||
#include <util/string.h>
|
||||
|
||||
#ifdef ENABLE_EXTERNAL_SIGNER
|
||||
#include <util/subprocess.h>
|
||||
#endif // ENABLE_EXTERNAL_SIGNER
|
||||
|
||||
UniValue RunCommandParseJSON(const std::string& str_command, const std::string& str_std_in)
|
||||
UniValue RunCommandParseJSON(const std::vector<std::string>& cmd_args, const std::string& str_std_in)
|
||||
{
|
||||
#ifdef ENABLE_EXTERNAL_SIGNER
|
||||
namespace sp = subprocess;
|
||||
@@ -22,9 +23,9 @@ UniValue RunCommandParseJSON(const std::string& str_command, const std::string&
|
||||
std::istringstream stdout_stream;
|
||||
std::istringstream stderr_stream;
|
||||
|
||||
if (str_command.empty()) return UniValue::VNULL;
|
||||
if (cmd_args.empty()) return UniValue::VNULL;
|
||||
|
||||
auto c = sp::Popen(str_command, sp::input{sp::PIPE}, sp::output{sp::PIPE}, sp::error{sp::PIPE});
|
||||
auto c = sp::Popen(cmd_args, sp::input{sp::PIPE}, sp::output{sp::PIPE}, sp::error{sp::PIPE});
|
||||
if (!str_std_in.empty()) {
|
||||
c.send(str_std_in);
|
||||
}
|
||||
@@ -38,7 +39,7 @@ UniValue RunCommandParseJSON(const std::string& str_command, const std::string&
|
||||
std::getline(stderr_stream, error);
|
||||
|
||||
const int n_error = c.retcode();
|
||||
if (n_error) throw std::runtime_error(strprintf("RunCommandParseJSON error: process(%s) returned %d: %s\n", str_command, n_error, error));
|
||||
if (n_error) throw std::runtime_error(strprintf("RunCommandParseJSON error: process(%s) returned %d: %s\n", util::Join(cmd_args, " "), n_error, error));
|
||||
if (!result_json.read(result)) throw std::runtime_error("Unable to parse JSON: " + result);
|
||||
|
||||
return result_json;
|
||||
|
||||
@@ -6,16 +6,17 @@
|
||||
#define BITCOIN_COMMON_RUN_COMMAND_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class UniValue;
|
||||
|
||||
/**
|
||||
* Execute a command which returns JSON, and parse the result.
|
||||
*
|
||||
* @param str_command The command to execute, including any arguments
|
||||
* @param cmd_args The command and arguments
|
||||
* @param str_std_in string to pass to stdin
|
||||
* @return parsed JSON
|
||||
*/
|
||||
UniValue RunCommandParseJSON(const std::string& str_command, const std::string& str_std_in="");
|
||||
UniValue RunCommandParseJSON(const std::vector<std::string>& cmd_args, const std::string& str_std_in = "");
|
||||
|
||||
#endif // BITCOIN_COMMON_RUN_COMMAND_H
|
||||
|
||||
Reference in New Issue
Block a user