Comments (4)
It's because you're making varCount
= 2, when vars
has only one element. If you tell tinyexpr that there are two elements in vars, it'll try to access the second element, which will segfault, because the second element does not exist. If the length of vars
is 1, then you should always tell tinyexpr that it is 1.
This section of code is nonsense:
int varsCount = 0;
for (int i = 0; i < FORMULA_SIZE; i++)
{
if (formula[i] == 't')
varsCount++;
}
It should just be:
int varCount = 1;
Let me give you some debugging advice. This is why I asked for the "most minimal complete example". If you tried reducing your code down to the minimal example that still shows the error, you would have spotted the error easily. This is the "most minimal complete example," that I asked for, and it makes the error completely obvious:
te_variable vars[] = {{"t", &t, TE_VARIABLE}};
expr = te_compile("tt + 0", vars, 2, 0); //seg fault
I asset that this will work without segfault:
te_variable vars[] = {{"t", &t, TE_VARIABLE}};
expr = te_compile("tt + 0", vars, 1, 0); //no seg fault
from tinyexpr.
int main()
{
int t = 0;
te_expr *expr;
int err = 0;
te_variable vars[] = {{"t", &t, TE_VARIABLE}};
int FORMULA_SIZE = 126;
char formula[FORMULA_SIZE];
memset(formula, 0, FORMULA_SIZE);
char *initStr = "tt + 0";
strncpy(formula, initStr, strlen(initStr));
for (int i = 0; i < FORMULA_SIZE; i++)
{
if (formula[i] == '\0')
{
formula[i] = ' ';
}
}
formula[FORMULA_SIZE - 1] = 0;
while (t < 100000)
{
int varsCount = 0;
for (int i = 0; i < FORMULA_SIZE; i++)
{
if (formula[i] == 't')
varsCount++;
}
printf("vars: %d\n", varsCount);
printf("tinyExpr formula: %s\n", formula);
expr = te_compile(formula, vars, varsCount, 0);
if (expr)
{
unsigned char result = te_eval(expr);
}
else
{
printf("parse error at %d\n", err);
}
t++;
}
te_free(expr);
}
When changing the variable initStr to ""t t + 0", we got the parse error, but when ""tt + 0", segmentation fault
from tinyexpr.
I'm stating the obvious here, but it's impossible to help you if you don't post your code. Post the most minimal complete example that demonstrates your problem.
from tinyexpr.
Oh ok thank you !
from tinyexpr.
Related Issues (20)
- Feature request: add logical operators.
- Feature request : right and left shift operations HOT 4
- Look ahead Variables HOT 4
- Custom infix operators HOT 3
- How about support time expression? HOT 2
- Importing into gcc gives "error: array subscript has type 'char'" HOT 2
- The website says that this is a header-only library, but the actual code isn't HOT 2
- Extraction of tokens HOT 5
- Invalid conversion for custom function in Arduino HOT 6
- Redundant null-pointer check in `te_interp` HOT 1
- Merging logic branch with ifdefs
- codeblocks compiler issue? HOT 2
- Weird behavior when calling te_free HOT 3
- Feature request: Add define to change return value from double to long double HOT 6
- Usage with different types HOT 2
- Feature Request: Implement ternary evalutation HOT 9
- Error eval expression when initializing te_variable by individual assignment HOT 7
- Function to determine the number of variables in an expression HOT 2
- Possible memory leak in te_compile HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tinyexpr.