Skip to content

Commit c6fea11

Browse files
committed
Lexer: fix handling of cut off json where string don't terminate
1 parent a094f1a commit c6fea11

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/Seld/JsonLint/Lexer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ public function getFullUpcomingInput()
112112
$next = $this->match;
113113
if (substr($next, 0, 1) === '"' && substr_count($next, '"') === 1) {
114114
$len = \strlen($this->input);
115-
$strEnd = min(strpos($this->input, '"', $this->offset + 1) ?: $len, strpos($this->input, "\n", $this->offset + 1) ?: $len);
115+
if ($len === $this->offset) {
116+
$strEnd = $len;
117+
} else {
118+
$strEnd = min(strpos($this->input, '"', $this->offset + 1) ?: $len, strpos($this->input, "\n", $this->offset + 1) ?: $len);
119+
}
116120
$next .= substr($this->input, $this->offset, $strEnd - $this->offset);
117121
} elseif (\strlen($next) < 20) {
118122
$next .= substr($this->input, $this->offset, 20 - \strlen($next));

tests/JsonParserTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,15 @@ public function testLongString()
269269
$json = '{"k":"' . str_repeat("a\\n",10000) . '"}';
270270
$this->assertEquals(json_decode($json), $parser->parse($json));
271271
}
272+
273+
public function testParseNoneTerminatingString()
274+
{
275+
$parser = new JsonParser();
276+
277+
try {
278+
$this->assertEquals('', $parser->parse('{"'));
279+
} catch (ParsingException $e) {
280+
$this->assertContains('Invalid string, it appears you forgot to terminate a string', $e->getMessage());
281+
}
282+
}
272283
}

0 commit comments

Comments
 (0)