Skip to content

Commit

Permalink
[DAPHNE-daphne-eu#773] Undetected invalid script args
Browse files Browse the repository at this point in the history
- Fixed invalid script arguments that try to evaulate an expression
- Fixed invalid script arguments that were not literals
- Added testcases for single script arguments
- Added testcases where Daphne fails in case of an invalid script arguments
  • Loading branch information
ldirry authored and corepointer committed Aug 9, 2024
1 parent e6cd72a commit dc61221
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
40 changes: 27 additions & 13 deletions src/parser/daphnedsl/DaphneDSLVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -794,27 +794,41 @@ antlrcpp::Any DaphneDSLVisitor::visitArgExpr(DaphneDSLGrammarParser::ArgExprCont
"argument " + arg + " referenced, but not provided as a command line argument"
);

bool hasMinus;
std::string litStr;
if(!it->second.empty() && it->second[0] == '-') {
std::string argValue = it->second;
bool hasMinus = false;
if (!argValue.empty() && argValue[0] == '-') {
hasMinus = true;
litStr = it->second.substr(1);
}
else {
hasMinus = false;
litStr = it->second;
argValue = argValue.substr(1);
}

// Parse the string that was passed as the value for this argument on the
// command line as a DaphneDSL literal.
// Parse the argument value as a literal
// TODO: fix for string literals when " are not escaped or not present
std::istringstream stream(litStr);
std::istringstream stream(argValue);
antlr4::ANTLRInputStream input(stream);
input.name = "argument"; // TODO Does this make sense?
input.name = "argument";
DaphneDSLGrammarLexer lexer(&input);
antlr4::CommonTokenStream tokens(&lexer);
DaphneDSLGrammarParser parser(&tokens);
DaphneDSLGrammarParser::LiteralContext * literalCtx = parser.literal();

CancelingErrorListener errorListener;
lexer.removeErrorListeners();
lexer.addErrorListener(&errorListener);
parser.removeErrorListeners();
parser.addErrorListener(&errorListener);

DaphneDSLGrammarParser::LiteralContext * literalCtx = nullptr;
try {
literalCtx = parser.literal();
if (tokens.LA(1) != antlr4::Token::EOF) {
// Ensure entire input is consumed; if not, it's not a valid literal
throw std::runtime_error("Extra input after literal");
}
}
catch (std::exception & e) {
throw ErrorHandler::compilerError(utils.getLoc(ctx->start), "DSLVisitor",
"Invalid literal value for argument '" + arg + "': " + argValue
);
}

mlir::Value lit = visitLiteral(literalCtx);
if(!hasMinus)
Expand Down
20 changes: 15 additions & 5 deletions test/api/cli/scriptargs/ScriptArgsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ TEST_CASE("Print single script argument", TAG_SCRIPTARGS) {
compareDaphneToStr("-123.45\n" , scriptPath.c_str(), "--args", "foo=-123.45");
compareDaphneToStr("1\n" , scriptPath.c_str(), "--args", "foo=true");
compareDaphneToStr("hello world\n", scriptPath.c_str(), "--args", "foo=\"hello world\"");
compareDaphneToStr("nan\n", scriptPath.c_str(), "--args", "foo=nan");
compareDaphneToStr("inf\n", scriptPath.c_str(), "--args", "foo=inf");
compareDaphneToStr("-inf\n", scriptPath.c_str(), "--args", "foo=-inf");
compareDaphneToStr("12.34\n", scriptPath.c_str(), "--args", "foo=12.34f");
compareDaphneToStr("120000\n", scriptPath.c_str(), "--args", "foo=1.2e5f");
compareDaphneToStr("1e-14\n", scriptPath.c_str(), "--args", "foo=1E-14");
compareDaphneToStr("1\n", scriptPath.c_str(), "--args", "foo=true");
compareDaphneToStr("0\n", scriptPath.c_str(), "--args", "foo=false");
}

TEST_CASE("Missing script argument", TAG_SCRIPTARGS) {
Expand Down Expand Up @@ -89,13 +97,15 @@ TEST_CASE("Ways of specifying script arguments", TAG_SCRIPTARGS) {
CHECK(err.str() == "");
}

// TODO While DAPHNE does not evaluate the expressions provided as script arguments,
// these invalid script arguments are not properly detected, either. DAPHNE succeeds
// with unexpected results (see #773).
#if 0

TEST_CASE("Don't support general expressions as script arguments", TAG_SCRIPTARGS) {
const std::string scriptPath = dirPath + "printSingleArg.daphne";
checkDaphneFails(scriptPath.c_str(), "--args", "foo=10+10");
checkDaphneFails(scriptPath.c_str(), "--args", "foo=sin(1.23)");
}
#endif

TEST_CASE("Don't support literal mixtures ", TAG_SCRIPTARGS) {
const std::string scriptPath = dirPath + "printSingleArg.daphne";
checkDaphneFails(scriptPath.c_str(), "--args", "foo=10xyz23");
checkDaphneFails(scriptPath.c_str(), "--args", "foo=-0.0mo");
}

0 comments on commit dc61221

Please sign in to comment.