Skip to content

Commit d93f76d

Browse files
Code Modernization: Correct handling of null in wp_parse_str().
This fixes `parse_str(): Passing null to parameter #1 ($string) of type string is deprecated` notices on PHP 8.1, without change in behaviour. Impact: 311 of the pre-existing tests are affected by this issue. The PHP native `parse_str()` function expects a string, however, based on the failing tests, it is clear there are functions in WordPress which passes a non-string – including `null` – value to the `wp_parse_str()` function, which would subsequently pass it onto the PHP native function without further input validation. Most notable offender is the `wp_parse_args()` function which special cases arrays and objects, but passes everything else off to `wp_parse_str()`. Several ways to fix this issue have been explored, including checking the received value with `is_string()` or `is_scalar()` before passing it off to the PHP native `parse_str()` function. In the end it was decided against these in favor of a string cast as: * `is_string()` would significantly change the behavior for anything non-string. * `is_scalar()` up to a point as well, as it does not take objects with a `__toString()` method into account. Executing a string cast on the received value before passing it on maintains the pre-existing behavior while still preventing the deprecation notice coming from PHP 8.1. Reference: [https://www.php.net/manual/en/function.parse-str.php PHP Manual: parse_str()] Follow-up to [5709]. Props jrf, hellofromTonya, lucatume, SergeyBiryukov. See #53635. git-svn-id: https://develop.svn.wordpress.org/trunk@51624 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 4d2762e commit d93f76d

File tree

2 files changed

+145
-1
lines changed

2 files changed

+145
-1
lines changed

src/wp-includes/formatting.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4964,7 +4964,7 @@ function map_deep( $value, $callback ) {
49644964
* @param array $array Variables will be stored in this array.
49654965
*/
49664966
function wp_parse_str( $string, &$array ) {
4967-
parse_str( $string, $array );
4967+
parse_str( (string) $string, $array );
49684968

49694969
/**
49704970
* Filters the array of variables derived from a parsed string.
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
/**
4+
* @group formatting
5+
* @covers ::wp_parse_str
6+
*/
7+
class Tests_Formatting_wpParseStr extends WP_UnitTestCase {
8+
9+
/**
10+
* Tests parsing of a string into variables.
11+
*
12+
* Note: While the function under test does not contain any significant logic,
13+
* these tests document the behavior and safeguard PHP cross-version compatibility.
14+
*
15+
* @dataProvider data_wp_parse_str
16+
*
17+
* @param mixed $input Value to parse.
18+
* @param array $expected Expected function output.
19+
*/
20+
public function test_wp_parse_str( $input, $expected ) {
21+
wp_parse_str( $input, $output );
22+
$this->assertSame( $expected, $output );
23+
}
24+
25+
/**
26+
* Data Provider.
27+
*
28+
* @return array
29+
*/
30+
public function data_wp_parse_str() {
31+
return array(
32+
'null' => array(
33+
'input' => null,
34+
'expected' => array(),
35+
),
36+
'boolean false' => array(
37+
'input' => false,
38+
'expected' => array(),
39+
),
40+
'boolean true' => array(
41+
'input' => true,
42+
'expected' => array(
43+
1 => '',
44+
),
45+
),
46+
'integer 0' => array(
47+
'input' => 0,
48+
'expected' => array(
49+
0 => '',
50+
),
51+
),
52+
'integer 456' => array(
53+
'input' => 456,
54+
'expected' => array(
55+
456 => '',
56+
),
57+
),
58+
'float 12.53' => array(
59+
'input' => 12.53,
60+
'expected' => array(
61+
'12_53' => '',
62+
),
63+
),
64+
'plain string' => array(
65+
'input' => 'foobar',
66+
'expected' => array(
67+
'foobar' => '',
68+
),
69+
),
70+
'query string' => array(
71+
'input' => 'x=5&_baba=dudu&',
72+
'expected' => array(
73+
'x' => '5',
74+
'_baba' => 'dudu',
75+
),
76+
),
77+
'stringable object' => array(
78+
'input' => new Fixture_Formatting_wpParseStr(),
79+
'expected' => array(
80+
'foobar' => '',
81+
),
82+
),
83+
);
84+
}
85+
86+
/**
87+
* Tests that the result array only contains the result of the string parsing
88+
* when provided with different types of input for the `$output` parameter.
89+
*
90+
* @dataProvider data_wp_parse_str_result_array_is_always_overwritten
91+
*
92+
* @param array|null $output Value for the `$output` parameter.
93+
* @param array $expected Expected function output.
94+
*/
95+
public function test_wp_parse_str_result_array_is_always_overwritten( $output, $expected ) {
96+
wp_parse_str( 'key=25&thing=text', $output );
97+
$this->assertSame( $expected, $output );
98+
}
99+
100+
/**
101+
* Data provider.
102+
*
103+
* @return array
104+
*/
105+
public function data_wp_parse_str_result_array_is_always_overwritten() {
106+
// Standard value for expected output.
107+
$expected = array(
108+
'key' => '25',
109+
'thing' => 'text',
110+
);
111+
112+
return array(
113+
'output null' => array(
114+
'output' => null,
115+
'expected' => $expected,
116+
),
117+
'output empty array' => array(
118+
'output' => array(),
119+
'expected' => $expected,
120+
),
121+
'output non empty array, no conflicting keys' => array(
122+
'output' => array(
123+
'foo' => 'bar',
124+
),
125+
'expected' => $expected,
126+
),
127+
'output non empty array, conflicting keys' => array(
128+
'output' => array(
129+
'key' => 'value',
130+
),
131+
'expected' => $expected,
132+
),
133+
);
134+
}
135+
}
136+
137+
/**
138+
* Fixture for use in the tests.
139+
*/
140+
class Fixture_Formatting_wpParseStr {
141+
public function __toString() {
142+
return 'foobar';
143+
}
144+
}

0 commit comments

Comments
 (0)