From 941ea60da799f279ab1d556e2998491f8dbaa858 Mon Sep 17 00:00:00 2001 From: "Michael L. Szulczewski" Date: Wed, 28 Sep 2022 02:21:41 -0400 Subject: [PATCH] Fix casting errors in client-cli client-cli can produce casting errors if the numbers given as command-line arguments are negative. With this commit, the code now checks if the arguments are negative, and if so, prints an error message and exits. Signed-off-by: Michael L. Szulczewski --- src/uhs/client/client-cli.cpp | 52 +++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/src/uhs/client/client-cli.cpp b/src/uhs/client/client-cli.cpp index 0801b79e0..8b7c4815f 100644 --- a/src/uhs/client/client-cli.cpp +++ b/src/uhs/client/client-cli.cpp @@ -28,8 +28,24 @@ auto mint_command(cbdc::client& client, const std::vector& args) return false; } - const auto n_outputs = std::stoull(args[5]); - const auto output_val = std::stoul(args[6]); + static constexpr auto n_outputs_arg_idx = 5; + const auto& n_outputs_arg = args[n_outputs_arg_idx]; + auto idx_first_non_whitespace = n_outputs_arg.find_first_not_of(' '); + if(n_outputs_arg[idx_first_non_whitespace] == '-') { + std::cerr << "The requsted number of UTXOs cannot be negative." + << std::endl; + return false; + } + const auto n_outputs = std::stoull(n_outputs_arg); + + static constexpr auto output_val_arg_idx = 6; + const auto& output_val_arg = args[output_val_arg_idx]; + idx_first_non_whitespace = output_val_arg.find_first_not_of(' '); + if(output_val_arg[idx_first_non_whitespace] == '-') { + std::cerr << "The UTXO values cannot be negative." << std::endl; + return false; + } + const auto output_val = std::stoul(output_val_arg); const auto mint_tx = client.mint(n_outputs, static_cast(output_val)); @@ -103,11 +119,20 @@ auto send_command(cbdc::client& client, const std::vector& args) return false; } - const auto value = std::stoul(args[5]); + static constexpr auto value_arg_idx = 5; + const auto& value_arg = args[value_arg_idx]; + const auto idx_first_non_whitespace = value_arg.find_first_not_of(' '); + if(value_arg[idx_first_non_whitespace] == '-') { + std::cerr << "The amount of currency to send cannot be negative." + << std::endl; + return false; + } + const auto value = std::stoul(value_arg); + static constexpr auto address_arg_idx = 6; auto pubkey = decode_address(args[address_arg_idx]); if(!pubkey.has_value()) { - std::cout << "Could not decode address" << std::endl; + std::cout << "Could not decode address." << std::endl; return false; } @@ -130,8 +155,23 @@ auto fan_command(cbdc::client& client, const std::vector& args) return false; } - const auto value = std::stoul(args[6]); - const auto count = std::stoul(args[5]); + static constexpr auto value_arg_idx = 6; + const auto& value_arg = args[value_arg_idx]; + auto idx_first_non_whitespace = value_arg.find_first_not_of(' '); + if(value_arg[idx_first_non_whitespace] == '-') { + std::cerr << "The value cannot be negative." << std::endl; + return false; + } + const auto value = std::stoul(value_arg); + + static constexpr auto count_arg_idx = 5; + const auto& count_arg = args[count_arg_idx]; + idx_first_non_whitespace = count_arg.find_first_not_of(' '); + if(count_arg[idx_first_non_whitespace] == '-') { + std::cerr << "The count cannot be negative." << std::endl; + return false; + } + const auto count = std::stoul(count_arg); static constexpr auto address_arg_idx = 7; auto pubkey = decode_address(args[address_arg_idx]);