浏览代码

Fix possible NRE in CallStackElement construction (#708)

Sébastien Ros 5 年之前
父节点
当前提交
e43290d7e9

+ 18 - 0
Jint.Tests/Runtime/EngineTests.cs

@@ -884,6 +884,24 @@ namespace Jint.Tests.Runtime
             );
         }
 
+        [Fact]
+        public void ShouldAllowRecursionLimitWithoutReferencedName()
+        {
+            const string input = @"(function () {
+                var factorial = function(n) {
+                    if (n>1) {
+                        return n * factorial(n - 1);
+                    }
+                };
+
+                var result = factorial(38);
+            })();
+            ";
+
+            var engine = new Engine(o => o.LimitRecursion(20));
+            Assert.Throws<RecursionDepthOverflowException>(() => engine.Execute(input));
+        }
+
         [Fact]
         public void ShouldConvertDoubleToStringWithoutLosingPrecision()
         {

+ 1 - 1
Jint/Runtime/ExceptionHelper.cs

@@ -37,7 +37,7 @@ namespace Jint.Runtime
 
         public static void ThrowReferenceError(Engine engine, Reference reference)
         {
-            ThrowReferenceError(engine, (reference?.GetReferencedName()).ToString());
+            ThrowReferenceError(engine, reference?.GetReferencedName()?.ToString());
         }
 
         public static void ThrowReferenceError(Engine engine, string name)

+ 1 - 1
Jint/Runtime/Interpreter/Expressions/JintCallExpression.cs

@@ -112,7 +112,7 @@ namespace Jint.Runtime.Interpreter.Expressions
 
             if (_maxRecursionDepth >= 0)
             {
-                var stackItem = new CallStackElement(expression, func, (r?.GetReferencedName()).ToString() ?? "anonymous function");
+                var stackItem = new CallStackElement(expression, func, r?.GetReferencedName()?.ToString() ?? "anonymous function");
 
                 var recursionDepth = _engine.CallStack.Push(stackItem);