-
Notifications
You must be signed in to change notification settings - Fork 64
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
Syntax errors cause crashes #78
Comments
The error is for sure at |
This also crashes without any errors thrown. #include <cparse/shunting-yard.h>
int main()
{
TokenMap tm;
tm["x"] = 2;
try
{
auto result = calculator::calculate("sin(5x)", tm);
printf("%f", result.asDouble());
}
catch (...)
{
puts("Some error");
}
} |
Hello @AntonC9018, thanks for reporting and also with a minimal example with the bug, I will try to understand it and then I will get back to you =]. |
Uhm, ok this might be one of those pesky ones, because I was unable to reproduce the first example, so the steps I did were: First I created a In the #include <cparse/shunting-yard.h>
int main()
{
TokenMap tm;
tm["x"] = 5;
try
{
calculator calc;
calc.compile("5&&&x"); // I would expect this to throw an error, or set some flag I could check
printf("%f", calc.eval(tm).asDouble());
}
catch (...)
{
puts("Some error");
}
} Then I built the $ g++ -I . cparse/core-shunting-yard.o main.cpp && ./a.out
Some error Second Problem:I was able, however, to reproduce the incorrect compilation success you reported with the following code: #include <cparse/shunting-yard.h>
int main()
{
TokenMap tm;
tm["x"] = 5;
try
{
calculator calc;
calc.compile("5x"); // Using 5x instead of 5&&&x
printf("%f", calc.eval(tm).asDouble());
}
catch (...)
{
puts("Some error");
}
} It returned as if the result was the number |
Also as a side note, the #include <iostream>
// ...
std::cout << calc.eval(tm) << std::endl;
// ... Buf for debugging purposes, I am glad you used |
Ok, here's what I'm doing for the first example (this time without custom features):
// test.cpp
#include "cparse/shunting-yard.h"
int main()
{
TokenMap tm;
tm["x"] = 2;
try
{
calculator calc("5&&&x");
auto result = calc.eval(tm);
printf("%f", result.asDouble());
}
catch (...)
{
puts("Some error");
}
}
I suppose you are on Linux. Since you're on Linux, this might be a platform-specific issue. Any clues? |
Wait, this time it's even weirder. This code also crashed for me: #include "cparse/shunting-yard.h"
int main()
{
TokenMap tm;
tm["x"] = 2;
try
{
calculator calc;
calc.compile("5*x");
auto result = calc.eval(tm);
printf("%f", result.asDouble());
}
catch (...)
{
puts("Some error");
}
} Whereas |
Ok, I guess one also has to link against
"Some error" is never printed. |
Thought I'd mention the compiler version too. # g++ --version
g++ (Rev6, Built by MSYS2 project) 10.2.0 |
I've also tried to catch each type of error your code ever throws individually, to no avail. This code also results in a crash, without any prints. #include "shunting-yard.h"
#include "shunting-yard-exceptions.h"
int main()
{
TokenMap tm;
tm["x"] = 2;
try
{
calculator calc;
calc.compile("(");
auto result = calc.eval(tm);
printf("%f", result.asDouble());
}
catch (const bad_cast& exc)
{
puts("Bad cast");
}
catch (const syntax_error& exc)
{
puts("Syntax error");
}
catch (const type_error& exc)
{
puts("Type error");
}
catch (const undefined_operation& exc)
{
puts("Undefined operation");
}
catch (const std::invalid_argument& invalid_argument)
{
puts("Invalid argument");
}
catch (const std::domain_error& exc)
{
puts("Domain error");
}
catch (const std::runtime_error& exc)
{
puts("Runtime error");
}
} |
Also tried running the tests. I have not had experience with C++ tests, so I might be wrong. I have compiled the |
The last thing I tried is compiling the entire thing, without linking to libraries. Compiling and running the code from above from scratch with |
Hello @AntonC9018 sorry I disappeared, I had some things to do tonight, tomorrow I'll continue the investigation |
About the tests, it should be enough to run About the One thing you could try is to to: try
{
// ...
{
catch (const std::exception& ex)
{
std::cout << ex.what() << std::endl;
} Since all exceptions inherit from std::exception this should work unless we are talking about invalid memory access, which is always random =/. |
This is very weird. But sadly this is normal when we are working with C++ on different platforms =/. The g++ version I am using has an output very different from yours but it is also on the 10.2.0 version: $ g++ --version
g++ (GCC) 10.2.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
That said, if compiling including all cpp files is working for you this is great, but I have no idea of how to fix this so that future users don't have this problem. If you find out it would be great if you could submit a PR with a fix. So, I will focus on fixing that problem where it would not throw for the expression |
This code does print "Some error" on my MacOS:
If I remove try/catch it aborts with an unhandled exception: |
Interesting, but I think that the problem reported on stackoverflow is probably something different since he was able to fix it by recompiling everything from source instead of using the intermediary |
@AntonC9018 I have just submitted a commit that should fix the problem where it was not throwing for invalid expressions such as Regarding the error with throw, I don't know how to fix it since I can't reproduce it, so if the commit above solves the other problem I think we should close this ticket. |
Sure. |
Yeah, I figured xP, anyway thanks for reporting. |
Hello.
This simple piece of code causes a crash due to an error in the provided expression. The text "Some error" never gets printed.
Changing the expression to e.g.
5*x
makes it print the correct result.This is very unfortunate, since I expect the users of my application to be notified of an error if they input an invalid expression. Is there anything I'm doing wrong or is there any other mechanism for validating the expression?
I'm compiling with gcc on 64-bit MinGW. Everything links successfully.
Also setting the expression to e.g.
5x
does not crash the application, but doesn't throw errors either. It evaluates to x, though. However, setting it tosin(5x)
does crash it.sin(5*x)
works correctly.The crash kind of looks like a stack overflow to me. Perhaps these expressions cause infinite recursion somewhere in the code?
Now, I have defined a custom function (logarithm) and a custom operator (caret for exponentiation). Those work and I don't think they have anything to do with this problem, but it's worth mentioning anyway.
The text was updated successfully, but these errors were encountered: