Giter VIP home page Giter VIP logo

Comments (4)

codeplea avatar codeplea commented on August 19, 2024 2

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.

Sercurio avatar Sercurio commented on August 19, 2024 1
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.

codeplea avatar codeplea commented on August 19, 2024

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.

Sercurio avatar Sercurio commented on August 19, 2024

Oh ok thank you !

from tinyexpr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.