Browse Source

Fix JSON parser line number initialization for empty string (#827)

Marko Lahma 4 years ago
parent
commit
6880a994ff
2 changed files with 8 additions and 1 deletions
  1. 7 0
      Jint.Tests/Runtime/EngineTests.cs
  2. 1 1
      Jint/Native/Json/JsonParser.cs

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

@@ -1216,6 +1216,13 @@ myarr[0](0);
             ");
             ");
         }
         }
 
 
+        [Fact]
+        public void JsonParserShouldHandleEmptyString()
+        {
+            var ex = Assert.Throws<ParserException>(() => _engine.Execute("JSON.parse('');"));
+            Assert.Equal("Line 1: Unexpected end of input", ex.Message);
+       }
+
         [Fact]
         [Fact]
         [ReplaceCulture("fr-FR")]
         [ReplaceCulture("fr-FR")]
         public void ShouldBeCultureInvariant()
         public void ShouldBeCultureInvariant()

+ 1 - 1
Jint/Native/Json/JsonParser.cs

@@ -797,7 +797,7 @@ namespace Jint.Native.Json
         {
         {
             _source = code;
             _source = code;
             _index = 0;
             _index = 0;
-            _lineNumber = (_source.Length > 0) ? 1 : 0;
+            _lineNumber = 1;
             _lineStart = 0;
             _lineStart = 0;
             _length = _source.Length;
             _length = _source.Length;
             _lookahead = null;
             _lookahead = null;