Browse Source

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

Marko Lahma 3 years ago
parent
commit
c738fafa9d
2 changed files with 16 additions and 0 deletions
  1. 8 0
      Jint.Tests/Runtime/RegExpTests.cs
  2. 8 0
      Jint/Native/RegExp/RegExpPrototype.cs

+ 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("/", "\\/");
         }
         }