-
Notifications
You must be signed in to change notification settings - Fork 201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix casting errors in client-cli #188
base: trunk
Are you sure you want to change the base?
Fix casting errors in client-cli #188
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good, but you should use auto
and constexpr
Just a food-for-thought question: would it make sense to try to parse the argument as a
Perhaps that's not really any cleaner than manually finding the first non-space character and manually matching |
+1 I agree that's a better approach |
@HalosGhost Interesting suggestion! What does the first check in your pseudo-code do?
I'm used to seeing E2BIG as an error code that means "Argument list too long" (C++ ref, GNU ref). I'm happy to do the test either way, but the advantages of first converting to a |
Sorry for the confusion, the actual error-code I should have referenced was The primary benefit of this would essentially be that it offloads the actual parsing (e.g., skipping whitespace) to the standard library. Again, I'm not actually sure this is dramatically cleaner… |
Great--thanks for clarifying.
Is it a problem to terminate the program for out-of-range errors? I guess this is what you're saying above, but just to be certain, this is what happens with the current use of
Maybe the error message could be a little clearer, but it doesn't seem too bad to me. |
In the general case, no it's not a problem to terminate on those cases. However, with the alternative I offered, it's possible to require parsing twice (when the argument fits into the range of Maybe it doesn't actually matter (this is research-level code after all), and restricting the client to work within
|
Here's an implementation that I think does everything that @HalosGhost's pseudo-code does: const auto* p = n_outputs_arg.data();
char* p_end;
const auto n_outputs_sll = std::strtoll(p, &p_end, 10);
if (n_outputs_sll < 0){
std::cerr << "The requsted number of UTXOs cannot be negative."
<< std::endl;
return false;
}
const auto n_outputs = std::stoull(n_outputs_arg); I ended up modifying the pseudo-code because the first part would fail if the input argument was both out of range and negative:
For comparison, here's the version that checks for a leading ' 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); Both methods look good to me. However, I guess I favor the latter a little because it seems simpler and more readable. I'll plan to push a cleaned version of it, but if someone feels strongly about the |
855f9d2
to
0becd7d
Compare
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 <[email protected]>
0becd7d
to
941ea60
Compare
I've gone back and forth on this a couple of times, and I think the only reason I'm coming down in-favor of parsing rather than hand-matching is that there's another error case that's not handled in the code you have so far: asking for 0 UTXOs (or for UTXOs of 0-value). If we already have it as an integer, we can catch both error cases using |
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 [email protected]