Skip to content

Commit 3c57195

Browse files
authored
Merge pull request #19 from colinodell/fix-unpaired-surrogate-detection
Throw exception for unpaired UTF-16 surrogates
2 parents bc5fccb + 72a7e74 commit 3c57195

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
88

99
### Fixed
1010
- Fixed "small" integers always being cast to strings when `JSON_BIGINT_AS_STRING` is set (#17)
11+
- Fixed exceptions not being thrown when invalid UTF-16 escape sequences are encountered in strings
1112

1213
## [2.2.1] - 2021-11-06
1314
### Fixed

src/Json5Decoder.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,15 @@ private function string()
324324

325325
if ($this->currentByte === '\\') {
326326
if ($this->peek() === 'u' && $unicodeEscaped = $this->match('/^(?:\\\\u[A-Fa-f0-9]{4})+/')) {
327-
$string .= \json_decode('"'.$unicodeEscaped.'"');
327+
try {
328+
$unicodeUnescaped = \json_decode('"' . $unicodeEscaped . '"', false, 1, JSON_THROW_ON_ERROR);
329+
if ($unicodeUnescaped === null && ($err = json_last_error_msg())) {
330+
throw new \JsonException($err);
331+
}
332+
$string .= $unicodeUnescaped;
333+
} catch (\JsonException $e) {
334+
$this->throwSyntaxError($e->getMessage());
335+
}
328336
continue;
329337
}
330338

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"lineNumber": 1,
3+
"columnNumber": 24,
4+
"message": "Single unpaired UTF-16 surrogate in unicode escape",
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"test": 1, "test\ud800": 2}

0 commit comments

Comments
 (0)