Skip to content

Commit

Permalink
Merge pull request #79 from glaubinix/f/lexer-none-terminating-string
Browse files Browse the repository at this point in the history
Lexer: fix handling of cut off json where string don't terminate
  • Loading branch information
Seldaek committed Mar 31, 2022
2 parents a094f1a + c6fea11 commit d9a308b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Seld/JsonLint/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
11 changes: 11 additions & 0 deletions tests/JsonParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}

0 comments on commit d9a308b

Please sign in to comment.