From c6fea1151ea9150c8bab9b5bc9277255c473515f Mon Sep 17 00:00:00 2001 From: Stephan Vock Date: Wed, 30 Mar 2022 14:23:12 +0100 Subject: [PATCH] Lexer: fix handling of cut off json where string don't terminate --- src/Seld/JsonLint/Lexer.php | 6 +++++- tests/JsonParserTest.php | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Seld/JsonLint/Lexer.php b/src/Seld/JsonLint/Lexer.php index 455f0f7..d66de63 100644 --- a/src/Seld/JsonLint/Lexer.php +++ b/src/Seld/JsonLint/Lexer.php @@ -112,7 +112,11 @@ public function getFullUpcomingInput() $next = $this->match; if (substr($next, 0, 1) === '"' && substr_count($next, '"') === 1) { $len = \strlen($this->input); - $strEnd = min(strpos($this->input, '"', $this->offset + 1) ?: $len, strpos($this->input, "\n", $this->offset + 1) ?: $len); + if ($len === $this->offset) { + $strEnd = $len; + } else { + $strEnd = min(strpos($this->input, '"', $this->offset + 1) ?: $len, strpos($this->input, "\n", $this->offset + 1) ?: $len); + } $next .= substr($this->input, $this->offset, $strEnd - $this->offset); } elseif (\strlen($next) < 20) { $next .= substr($this->input, $this->offset, 20 - \strlen($next)); diff --git a/tests/JsonParserTest.php b/tests/JsonParserTest.php index 5f3296f..6389f20 100644 --- a/tests/JsonParserTest.php +++ b/tests/JsonParserTest.php @@ -269,4 +269,15 @@ public function testLongString() $json = '{"k":"' . str_repeat("a\\n",10000) . '"}'; $this->assertEquals(json_decode($json), $parser->parse($json)); } + + public function testParseNoneTerminatingString() + { + $parser = new JsonParser(); + + try { + $this->assertEquals('', $parser->parse('{"')); + } catch (ParsingException $e) { + $this->assertContains('Invalid string, it appears you forgot to terminate a string', $e->getMessage()); + } + } }