Ver Fonte

Fix NullReferenceException in RegExp.prototype.source (#1022)

Marko Lahma há 4 anos atrás
pai
commit
c738fafa9d

+ 8 - 0
Jint.Tests/Runtime/RegExpTests.cs

@@ -1,5 +1,6 @@
 using System;
 using System;
 using System.Text.RegularExpressions;
 using System.Text.RegularExpressions;
+using Jint.Native;
 using Jint.Native.Array;
 using Jint.Native.Array;
 using Xunit;
 using Xunit;
 
 
@@ -81,5 +82,12 @@ namespace Jint.Tests.Runtime
             Assert.NotNull(engine.Evaluate("/[]/"));
             Assert.NotNull(engine.Evaluate("/[]/"));
             Assert.NotNull(engine.Evaluate("new RegExp('[]')"));
             Assert.NotNull(engine.Evaluate("new RegExp('[]')"));
         }
         }
+
+        [Fact]
+        public void ShouldNotThrowErrorOnRegExNumericNegation()
+        {
+            var engine = new Engine();
+            Assert.Equal(JsNumber.DoubleNaN, engine.Evaluate("-/[]/"));
+        }
     }
     }
 }
 }

+ 8 - 0
Jint/Native/RegExp/RegExpPrototype.cs

@@ -95,6 +95,9 @@ namespace Jint.Native.RegExp
             SetSymbols(symbols);
             SetSymbols(symbols);
         }
         }
 
 
+        /// <summary>
+        /// https://tc39.es/ecma262/#sec-get-regexp.prototype.source
+        /// </summary>
         private JsValue Source(JsValue thisObj, JsValue[] arguments)
         private JsValue Source(JsValue thisObj, JsValue[] arguments)
         {
         {
             if (ReferenceEquals(thisObj, this))
             if (ReferenceEquals(thisObj, this))
@@ -108,6 +111,11 @@ namespace Jint.Native.RegExp
                 ExceptionHelper.ThrowTypeError(_realm);
                 ExceptionHelper.ThrowTypeError(_realm);
             }
             }
 
 
+            if (r.Source is null)
+            {
+                return JsString.Empty;
+            }
+
             return r.Source.Replace("/", "\\/");
             return r.Source.Replace("/", "\\/");
         }
         }