Skip to content

Commit 835c8a5

Browse files
jakevoytkovladar
authored andcommitted
Modifies the chr() check to properly handle \u0000 codes in the range… (#554)
* Modifies the chr() check to properly handle \u0000 codes in the range [127, 255] in the lexer This caused the lexer to output invalid UTF-8 for input like 'pok\u00E9mon'. The output for the é would be the decimal byte 233, which would indicate that it should be a 4 byte unicode sequence, but the following bytes didn't have the leading 10 prefix, so the sequence was invalid UTF-8.
1 parent 9864820 commit 835c8a5

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Add schema validation: Input Objects must not contain non-nullable circular references (#492)
99
- Added retrieving query complexity once query has been completed (#316)
1010
- Allow input types to be passed in from variables using \stdClass instead of associative arrays (#535)
11+
- Fixes parsing of string literals of the form \u0000 for code points in the range [128, 255] inclusive
1112

1213
#### v0.13.5
1314
- Fix coroutine executor when using with promise (#486)

src/Utils/Utils.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,6 @@ public static function printSafe($var)
442442
*/
443443
public static function chr($ord, $encoding = 'UTF-8')
444444
{
445-
if ($ord <= 255) {
446-
return chr($ord);
447-
}
448445
if ($encoding === 'UCS-4BE') {
449446
return pack('N', $ord);
450447
}

tests/UtilsTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use InvalidArgumentException;
99
use PHPUnit\Framework\TestCase;
1010
use stdClass;
11+
use function mb_check_encoding;
1112

1213
class UtilsTest extends TestCase
1314
{
@@ -20,4 +21,39 @@ public function testAssignThrowsExceptionOnMissingRequiredKey() : void
2021
$this->expectExceptionMessage('Key requiredKey is expected to be set and not to be null');
2122
Utils::assign($object, [], ['requiredKey']);
2223
}
24+
25+
/**
26+
* @param int $input
27+
* @param string $expected
28+
*
29+
* @dataProvider chrUtf8DataProvider
30+
*/
31+
public function testChrUtf8Generation($input, $expected) : void
32+
{
33+
$result = Utils::chr($input);
34+
self::assertTrue(mb_check_encoding($result, 'UTF-8'));
35+
self::assertEquals($expected, $result);
36+
}
37+
38+
public function chrUtf8DataProvider()
39+
{
40+
return [
41+
'alphabet' => [
42+
'input' => 0x0061,
43+
'expected' => 'a',
44+
],
45+
'numeric' => [
46+
'input' => 0x0030,
47+
'expected' => '0',
48+
],
49+
'between 128 and 256' => [
50+
'input' => 0x00E9,
51+
'expected' => 'é',
52+
],
53+
'emoji' => [
54+
'input' => 0x231A,
55+
'expected' => '',
56+
],
57+
];
58+
}
2359
}

0 commit comments

Comments
 (0)