Skip to content

Commit 635c42c

Browse files
committed
Code Quality: Correct by-ref types in UTF-8 compat functions.
In `_wp_scan_utf8()` the `$has_noncharacters` reference was accumulated with the bitwise `|=` operator, which turned the boolean into an integer as soon as a sequence able to encode a noncharacter was scanned, contrary to its documented type. It is now accumulated with a logical OR, so callers always receive a boolean. New `@param-out` tags document that `$has_noncharacters` and `$found_code_points` are never null on return, even though both accept null when passed in. This empties the `parameterByRef.unusedType` baseline, so it is removed. New tests cover how `_wp_scan_utf8()` counts code points across multibyte sequences and grapheme clusters, how it reports the maximal subparts of invalid byte sequences, and how it detects noncharacters. Developed in #14025. Follow-up to r61000, r63022. Props huzaifaalmesbah, dmsnell, lancewillett, westonruter, rcwpdev. See #66739. git-svn-id: https://develop.svn.wordpress.org/trunk@65771 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 2da24ad commit 635c42c

4 files changed

Lines changed: 158 additions & 37 deletions

File tree

‎src/wp-includes/compat-utf8.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* @param int|null $max_bytes Stop scanning after this many bytes have been seen.
4343
* @param int|null $max_code_points Stop scanning after this many code points have been seen.
4444
* @param bool|null $has_noncharacters Set to indicate if scanned string contained noncharacters.
45+
* @param-out bool $has_noncharacters
4546
* @return int How many code points were successfully scanned.
4647
*/
4748
function _wp_scan_utf8( string $bytes, int &$at, int &$invalid_length, ?int $max_bytes = null, ?int $max_code_points = null, ?bool &$has_noncharacters = null ): int {
@@ -151,7 +152,7 @@ function _wp_scan_utf8( string $bytes, int &$at, int &$invalid_length, ?int $max
151152

152153
// Covers the range U+FDD0–U+FDEF, U+FFFE, U+FFFF.
153154
if ( 0xEF === $b1 ) {
154-
$has_noncharacters |= (
155+
$has_noncharacters = $has_noncharacters || (
155156
( 0xB7 === $b2 && $b3 >= 0x90 && $b3 <= 0xAF ) ||
156157
( 0xBF === $b2 && ( 0xBE === $b3 || 0xBF === $b3 ) )
157158
);
@@ -176,7 +177,7 @@ function _wp_scan_utf8( string $bytes, int &$at, int &$invalid_length, ?int $max
176177
$i += 3;
177178

178179
// Covers U+1FFFE, U+1FFFF, U+2FFFE, U+2FFFF, …, U+10FFFE, U+10FFFF.
179-
$has_noncharacters |= (
180+
$has_noncharacters = $has_noncharacters || (
180181
( 0x0F === ( $b2 & 0x0F ) ) &&
181182
0xBF === $b3 &&
182183
( 0xBE === $b4 || 0xBF === $b4 )
@@ -376,6 +377,7 @@ function _wp_utf8_codepoint_count( string $text, ?int $byte_offset = 0, ?int $ma
376377
* @param ?int $found_code_points Optional. Will be set to number of found code points in
377378
* span, as this might be smaller than the maximum count if
378379
* the string is not long enough.
380+
* @param-out int $found_code_points
379381
* @return int Number of bytes spanned by the code points.
380382
*/
381383
function _wp_utf8_codepoint_span( string $text, int $byte_offset, int $max_code_points, ?int &$found_code_points = 0 ): int {

‎tests/phpstan/baselines/parameterByRef.type.neon‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ parameters:
3838
identifier: parameterByRef.type
3939
count: 1
4040
path: ../../../src/wp-includes/class-wp-token-map.php
41-
-
42-
message: '#^Parameter &\$has_noncharacters by\-ref type of function _wp_scan_utf8\(\) expects bool\|null, int given\.$#'
43-
identifier: parameterByRef.type
44-
count: 2
45-
path: ../../../src/wp-includes/compat-utf8.php
4641
-
4742
message: '#^Parameter &\$result by\-ref type of function _page_traverse_name\(\) expects array\<string\>, array given\.$#'
4843
identifier: parameterByRef.type

‎tests/phpstan/baselines/parameterByRef.unusedType.neon‎

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
/**
3+
* Unit tests covering _wp_scan_utf8().
4+
*
5+
* @package WordPress
6+
* @subpackage Charset
7+
*
8+
* @group compat
9+
*
10+
* @covers ::_wp_scan_utf8
11+
*/
12+
class Tests_Compat_wpScanUtf8 extends WP_UnitTestCase {
13+
14+
/**
15+
* Ensures that valid UTF-8 is scanned to the end, counting every code point.
16+
*
17+
* @ticket 65817
18+
*
19+
* @dataProvider data_valid_utf8
20+
*
21+
* @param string $text Valid UTF-8 text without noncharacters.
22+
*/
23+
public function test_counts_code_points_in_valid_utf8( string $text ) {
24+
$at = 0;
25+
$invalid_length = 0;
26+
$has_noncharacters = null;
27+
28+
$count = _wp_scan_utf8( $text, $at, $invalid_length, null, null, $has_noncharacters );
29+
30+
$this->assertSame( mb_strlen( $text, 'UTF-8' ), $count, 'Should have counted every code point.' );
31+
$this->assertSame( strlen( $text ), $at, 'Should have scanned to the end of the text.' );
32+
$this->assertSame( 0, $invalid_length, 'Should not have found any invalid bytes.' );
33+
$this->assertFalse( $has_noncharacters, 'Should not have found any noncharacters.' );
34+
}
35+
36+
/**
37+
* Data provider for {@see self::test_counts_code_points_in_valid_utf8()}.
38+
*
39+
* @return array<non-falsy-string, array{ 0: string }>
40+
*/
41+
public static function data_valid_utf8(): array {
42+
return array(
43+
'Empty string' => array( '' ),
44+
'US-ASCII' => array( 'Hello, World!' ),
45+
'Two-byte sequences' => array( "Pi\u{00F1}a \u{00E9}t\u{00E9}" ),
46+
'Three-byte sequences' => array( "\u{65E5}\u{672C}\u{8A9E}" ),
47+
'Four-byte sequences' => array( "\u{1F600}\u{1F170}\u{10348}" ),
48+
'Mixed sequence lengths' => array( "a\u{00F1}\u{65E5}\u{1F600}z" ),
49+
'Boundaries between lengths' => array( "\u{007F}\u{0080}\u{07FF}\u{0800}\u{FFFD}\u{10000}\u{10FFFD}" ),
50+
'Combining character' => array( "e\u{0301}" ),
51+
'Hangul syllable as conjoining jamo' => array( "\u{1112}\u{1161}\u{11AB}" ),
52+
'Devanagari grapheme clusters' => array( "\u{0928}\u{092E}\u{0938}\u{094D}\u{0924}\u{0947}" ),
53+
'Regional indicator pair (flag)' => array( "\u{1F1E7}\u{1F1E9}" ),
54+
'Emoji with skin tone modifier' => array( "\u{1F44D}\u{1F3FD}" ),
55+
'Keycap sequence' => array( "1\u{FE0F}\u{20E3}" ),
56+
'ZWJ sequence' => array( "\u{1F469}\u{200D}\u{1F4BB}" ),
57+
'ZWJ family sequence' => array( "\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F467}\u{200D}\u{1F466}" ),
58+
'ZWJ sequence with VS-16' => array( "\u{1F3F3}\u{FE0F}\u{200D}\u{1F308}" ),
59+
);
60+
}
61+
62+
/**
63+
* Ensures that each maximal subpart of an invalid byte sequence is reported
64+
* as its own invalid span, so that it can be replaced by a single U+FFFD.
65+
*
66+
* @ticket 65817
67+
*
68+
* @dataProvider data_invalid_utf8
69+
*
70+
* @param string $text Text containing invalid UTF-8 bytes.
71+
* @param list<positive-int> $expected_spans Byte length of each invalid span, in order.
72+
* @param positive-int $expected_code_points Code points when each invalid span counts as one.
73+
*/
74+
public function test_finds_maximal_subparts_of_invalid_bytes( string $text, array $expected_spans, int $expected_code_points ) {
75+
$end = strlen( $text );
76+
$at = 0;
77+
$invalid_length = 0;
78+
$invalid_spans = array();
79+
$code_points = 0;
80+
81+
while ( $at < $end ) {
82+
$code_points += _wp_scan_utf8( $text, $at, $invalid_length );
83+
84+
if ( 0 === $invalid_length ) {
85+
break;
86+
}
87+
88+
$invalid_spans[] = $invalid_length;
89+
++$code_points;
90+
$at += $invalid_length;
91+
}
92+
93+
$this->assertSame( $expected_spans, $invalid_spans, 'Should have found the maximal subpart of each invalid span.' );
94+
$this->assertSame( $expected_code_points, $code_points, 'Should have counted one code point for each invalid span.' );
95+
$this->assertSame( $end, $at, 'Should have scanned to the end of the text.' );
96+
}
97+
98+
/**
99+
* Data provider for {@see self::test_finds_maximal_subparts_of_invalid_bytes()}.
100+
*
101+
* @return array<non-falsy-string, array{ 0: string, 1: list<positive-int>, 2: positive-int }>
102+
*/
103+
public static function data_invalid_utf8(): array {
104+
return array(
105+
'Lone continuation byte' => array( "a\x80b", array( 1 ), 3 ),
106+
'Run of continuation bytes' => array( "a\x80\xBF\x80b", array( 1, 1, 1 ), 5 ),
107+
'Truncated two-byte sequence' => array( "a\xC3b", array( 1 ), 3 ),
108+
'Truncated three-byte sequence' => array( "a\xE2\x82b", array( 2 ), 3 ),
109+
'Truncated four-byte sequence' => array( "a\xF0\x9F\x98b", array( 3 ), 3 ),
110+
'Truncated sequence at the end' => array( "abc\xF0\x9F\x98", array( 3 ), 4 ),
111+
'Overlong two-byte sequence' => array( "a\xC0\xAFb", array( 1, 1 ), 4 ),
112+
'Overlong three-byte sequence' => array( "a\xE0\x80\xAFb", array( 1, 1, 1 ), 5 ),
113+
'UTF-16 surrogate half' => array( "a\xED\xA0\x80b", array( 1, 1, 1 ), 5 ),
114+
'Code point above U+10FFFF' => array( "a\xF4\x90\x80\x80b", array( 1, 1, 1, 1 ), 6 ),
115+
'Byte that never appears in UTF-8' => array( "a\xFFb", array( 1 ), 3 ),
116+
'Windows-1252 encoded text' => array( "Pi\xF1a", array( 1 ), 4 ),
117+
'Truncated sequences back to back' => array( "\xE2\x82\xF0\x9F\x98", array( 2, 3 ), 2 ),
118+
'Invalid span between valid multibyte' => array( "\u{1F600}\xE2\x82\u{65E5}", array( 2 ), 3 ),
119+
);
120+
}
121+
122+
/**
123+
* Tests scanning UTF-8 string containing noncharacters.
124+
*
125+
* @ticket 65817
126+
*
127+
* @dataProvider data_strings_with_noncharacters
128+
*
129+
* @param string $text UTF-8 string containing a noncharacter.
130+
*/
131+
public function test_scan_string_with_noncharacters( string $text ) {
132+
$at = 0;
133+
$invalid_length = 0;
134+
$has_noncharacters = null;
135+
136+
_wp_scan_utf8( $text, $at, $invalid_length, null, null, $has_noncharacters );
137+
138+
$this->assertTrue( $has_noncharacters );
139+
}
140+
141+
/**
142+
* Data provider for {@see self::test_scan_string_with_noncharacters()}.
143+
*
144+
* @return array<non-falsy-string, array{ 0: string }>
145+
*/
146+
public static function data_strings_with_noncharacters(): array {
147+
return array(
148+
'U+FFFF (3-byte noncharacter)' => array( "test \xEF\xBF\xBF end" ),
149+
'U+FFFE (3-byte noncharacter)' => array( "test \xEF\xBF\xBE end" ),
150+
'U+FDD0 (3-byte noncharacter)' => array( "test \xEF\xB7\x90 end" ),
151+
'U+1FFFF (4-byte noncharacter)' => array( "test \xF0\x9F\xBF\xBF end" ),
152+
);
153+
}
154+
}

0 commit comments

Comments
 (0)