microsoft / Power-Fx

Power Fx low-code programming language
MIT License
3.21k stars 327 forks source link

Fix the precheck for out of memory in Substitute #1610

Closed nguhoa closed 1 year ago

nguhoa commented 1 year ago

With the following expression, we expected precheck return error before run it and throw exception of out of memory: Len(With({one: "aaaaaaaaaaaaaaaaaa"}, Substitute(Substitute(Substitute(Substitute(Substitute(Substitute(Substitute(Substitute(Substitute(Substitute(one, "a", one), "a", one), "a", one), "a", one), "a", one), "a", one), "a", one), "a", one), "a", one), "a", one)))

Error: System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown. at System.Text.StringBuilder.ToString() at Microsoft.PowerFx.Functions.Library.SubstituteWorker(EvalVisitor eval, IRContext irContext, StringValue source, StringValue match, StringValue replacement, Int32 instanceNum) at Microsoft.PowerFx.Functions.Library.Substitute(EvalVisitor runner, EvalVisitorContext context, IRContext irContext, FormulaValue[] args) at Microsoft.PowerFx.Functions.Library.<>cDisplayClass466_01.<StandardErrorHandling>b__0(EvalVisitor runner, EvalVisitorContext context, IRContext irContext, T[] args) at Microsoft.PowerFx.Functions.Library.<>c__DisplayClass463_01.<b0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at Microsoft.PowerFx.EvalVisitor.Visit(CallNode node, EvalVisitorContext context) at Microsoft.PowerFx.EvalVisitor.Visit(CallNode node, EvalVisitorContext context) at Microsoft.PowerFx.EvalVisitor.Visit(CallNode node, EvalVisitorContext context) at Microsoft.PowerFx.EvalVisitor.Visit(CallNode node, EvalVisitorContext context) at Microsoft.PowerFx.EvalVisitor.Visit(CallNode node, EvalVisitorContext context) at Microsoft.PowerFx.EvalVisitor.Visit(LazyEvalNode node, EvalVisitorContext context) at Microsoft.PowerFx.Types.LambdaFormulaValue.EvalInRowScopeAsync(EvalVisitorContext context) at Microsoft.PowerFx.Functions.Library.With(EvalVisitor runner, EvalVisitorContext context, IRContext irContext, FormulaValue[] args) at Microsoft.PowerFx.Functions.Library.<>c__DisplayClass463_0`1.<b__0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at Microsoft.PowerFx.EvalVisitor.Visit(CallNode node, EvalVisitorContext context) at Microsoft.PowerFx.EvalVisitor.Visit(UnaryOpNode node, EvalVisitorContext context) at Microsoft.PowerFx.EvalVisitor.Visit(CallNode node, EvalVisitorContext context) at Microsoft.PowerFx.ParsedExpression.EvalAsync(CancellationToken cancellationToken, IRuntimeConfig runtimeConfig) at FxPlayground.Core.Session.TryEvalAsync(String expression, UserInfo user, CancellationToken cancel) in D:\dev\pa2\PowerFxPlayground\src\FxPlaygroundCore\Session.cs:line 503 at PowerFxService.Controllers.EvaluationController.HandleRequestAsync(String session, RequestBody body) in D:\dev\pa2\PowerFxPlayground\src\WebDemo\Controllers\EvaluationController.cs:line 159

nguhoa commented 1 year ago

Fixed in https://github.com/microsoft/Power-Fx/pull/1623