KovtunV / NoStringEvaluating

Fast low memory consuming mathematical evaluation without endless string parsing! Parses string formula once and uses its object sequence in each evaluation. Moreover, provides user defined functions and variables.
MIT License
28 stars 10 forks source link

Evaluator.Parse() throws unexpected exceptions #14

Closed mschessler closed 3 months ago

mschessler commented 8 months ago

Evaluator.Parse() throws unexpected exceptions on malformed input.

Example 1:

var facade = NoStringEvaluator.CreateFacade();
var evaluator = facade.Evaluator;
evaluator.Calc("0(!(");

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index') at System.Collections.Generic.List1.get_Item(Int32 index) at NoStringEvaluating.Functions.Logic.NotAliasFunction.Execute(List1 args, ValueFactory factory) at NoStringEvaluating.NoStringEvaluator.CalcFunction(FunctionWrapperNode functionWrapper, VariablesSource variables, ValueKeeperContainer valueKeeperContainer) at NoStringEvaluating.NoStringEvaluator.CalcInternal(List1 nodes, VariablesSource variables, ValueKeeperContainer valueKeeperContainer) at NoStringEvaluating.NoStringEvaluator.OnCalcAggregated(List1 nodes, VariablesSource variables) at NoStringEvaluating.NoStringEvaluator.Calc(String formula)

Example 2: evaluator.Calc("(");

System.InvalidOperationException: Stack empty. at System.Collections.Generic.Stack1.ThrowForEmptyStack() at System.Collections.Generic.Stack1.Pop() at NoStringEvaluating.NoStringEvaluator.CalcInternal(List1 nodes, VariablesSource variables, ValueKeeperContainer valueKeeperContainer) at NoStringEvaluating.NoStringEvaluator.OnCalcAggregated(List1 nodes, VariablesSource variables) at NoStringEvaluating.NoStringEvaluator.Calc(String formula)

KovtunV commented 8 months ago

This is expected behavior. I did it to not to decrease performance because of function checking. Alternatively I created another service to validate formula input.

Example 1: image

Example 2: image

mschessler commented 8 months ago

Hey, thanks for the fast answer. I was not aware of the check syntax function. The problem unfortunately still remains. New inputs that pass the check function but still crash:

";;" -> System.InvalidOperationException

"-(!())" -> System.ArgumentOutOfRangeException

KovtunV commented 8 months ago

Thank you!

I will fix it in the next version.

mschessler commented 8 months ago

Not sure how interested you are, I'm just having some fun playing around with C# Fuzzing.

So here is another one:

"LeN(2)" -> System.NullReferenceException

If you want to I will report all findings in a few days, but I understand if you say that these bugs are not that relevant.

KovtunV commented 8 months ago

If you share the results, I will be grateful)

On the other hand, I'm not sure should I validate functions or not. For example, "UPPER()" will throw as well, I left it to show an error immediately. I mean if exception throws user can review formula immediately, but if I will return some value, even my wrapper around null, user can realize that later