Skip to content

Implementation of invalid user input exception #5

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

Merged

Conversation

NlightNFotis
Copy link

This PR contains an implementation of an erroneous user input exception, and changes the unstructured throw 0s in the cbmc/ folder into structured exceptions and preconditions/invariants.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM for the most part

throw invalid_user_input_exceptiont(
"Reason: sorry, this solver does not support incremental solving",
"--incremental-check",
"");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is quite a big improvement over the previous version that didn't tell you what option it didn't like, 👍

std::string res;
res += "\nInvalid User Input Exception\n";
res += "Option: " + option + "\n";
res += reason;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to have the "Reason: " part here, I just didn't like forcing the form of the part after the reason.
Sorry for being unclear with this

error() << "waitfor expected to have four operands" << eom;
throw 0;
}
PRECONDITION(expr.operands().size() != 4);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use DATA_INVARIANT here which is for checking whether goto programs, exprts, etc. are well-formed. Then we can also keep the error message.

Copy link
Author

@NlightNFotis NlightNFotis Jul 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I won't implement this. It's easily implementable, but the error message is just redundant IMO. It doesn't convey any information that the precondition expression doesn't convey already. It just duplicates semantic information about the behaviour of the code.

Copy link

@danpoe danpoe Jul 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm the error message had the additional information that expr is a wait-for expression (i.e., expr.id() is "waitfor"). From the condition we only see that it is some expression with four operands.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot, didn't notice that. I guess I can rewrite that a little bit as well.

@@ -143,7 +144,8 @@ void bmct::show_vcc()
{
of.open(filename);
if(!of)
throw "failed to open file "+filename;
throw invalid_user_input_exceptiont(
"Failed to open suggested file", "--outfile", "");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can drop "suggested", and append the filename to the first argument.

invalid_user_input_exceptiont(
std::string reason,
std::string option,
std::string correct_input)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have an empty string default argument for correct_input?

res += "Option: " + option + "\n";
res += "Reason: " + reason;
// Print an optional correct usage message assuming correct input parameters have been passed
correct_input.empty() ? "\n" : res += " Try: " + correct_input + "\n";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to add a newline before " Try".

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather it be kept on the same line.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a separator character then? Maybe a comma.

@@ -52,4 +58,9 @@ int main(int argc, const char **argv)
#endif

return res;
}
catch(invalid_user_input_exceptiont &e)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently if an exception is caught here the program still exits with a zero exit code. We should change this to exit with an error code.

I think we can also move the exception catching code to parse_options_baset::main() (wrapping the call to doit()). Then the tools that inherit from it don't need to repeat the exception handlers.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very interesting. It's a great suggestion, but this needs a little bit thinking about how to properly implement this (I haven't thought it through yet, might be easier than I realise)

@NlightNFotis
Copy link
Author

@hannes-steffenhagen-diffblue @danpoe Can I request a new review from both of you? Made some substantial changes on where we handle exceptions in the latest commit.

@@ -25,6 +25,10 @@ Author: Daniel Kroening, [email protected]
#include <iostream>
#endif

#include <iostream>

#include <util/exception_utils.h>
Copy link

@danpoe danpoe Jul 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think those two includes are now not needed anymore.

@@ -143,7 +144,8 @@ void bmct::show_vcc()
{
of.open(filename);
if(!of)
throw "failed to open file "+filename;
throw invalid_user_input_exceptiont(
"Failed to open file" + filename, "--outfile");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... file" -> ...file "

Copy link

@danpoe danpoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@@ -143,7 +144,8 @@ void bmct::show_vcc()
{
of.open(filename);
if(!of)
throw "failed to open file "+filename;
throw invalid_user_input_exceptiont(
"Failed to open file: " + filename, "--outfile");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about it, I don't really like this being an invalid user input exception (I suppose you can justify it being subject to user input as they control the filename, but technically whether or not we're able to open the file isn't necessarily up to the user).

Not a blocker for sure, but in general I'd have a preference for IO related exceptions to have their own place.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still looks OK to me

@chrisr-diffblue chrisr-diffblue merged commit 8c4eb38 into chrisr-diffblue:cbmc-bookworm Jul 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants