Skip to content

Commit 4843327

Browse files
committed
ext/standard: pathinfo() check flags argument validity.
close GH-17859
1 parent cbe0144 commit 4843327

File tree

5 files changed

+102
-32
lines changed

5 files changed

+102
-32
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ PHP NEWS
146146
(ndossche)
147147
. Add array size maximum to array_diff(). (ndossche)
148148
. Add enum SortDirection. (timwolla)
149+
. pathinfo() raises a ValueError with an invalid $flags argument.
150+
(David Carlier)
149151

150152
- Streams:
151153
. Added so_keepalive, tcp_keepidle, tcp_keepintvl and tcp_keepcnt stream

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ PHP 8.6 UPGRADE NOTES
131131
when not null, and on failure, gives the error code (one of the EAI_*
132132
constants).
133133

134+
- Standard:
135+
. pathinfo() now raises a ValueError when an invalid $flag argument
136+
value is passed.
137+
134138
- Zip:
135139
. ZipArchive::extractTo now raises a TypeError for the
136140
files argument if one or more of the entries is not

ext/standard/string.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,16 @@ PHP_FUNCTION(pathinfo)
15911591
Z_PARAM_LONG(opt)
15921592
ZEND_PARSE_PARAMETERS_END();
15931593

1594+
if (opt < PHP_PATHINFO_DIRNAME || opt > PHP_PATHINFO_ALL) {
1595+
zend_argument_value_error(2, "must be one of the PATHINFO_* constants");
1596+
RETURN_THROWS();
1597+
}
1598+
1599+
if (opt < PHP_PATHINFO_ALL && (opt & (opt - 1))) {
1600+
zend_argument_value_error(2, "must be only one of the PATHINFO_* constants");
1601+
RETURN_THROWS();
1602+
}
1603+
15941604
have_basename = (opt & PHP_PATHINFO_BASENAME);
15951605

15961606
array_init(&tmp);

ext/standard/tests/file/pathinfo_variation3.phpt

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,37 @@ var_dump(pathinfo($testfile, PATHINFO_BASENAME));
1515
var_dump(pathinfo($testfile, PATHINFO_FILENAME));
1616
var_dump(pathinfo($testfile, PATHINFO_EXTENSION));
1717
var_dump(pathinfo($testfile, PATHINFO_DIRNAME));
18-
var_dump(pathinfo($testfile, PATHINFO_EXTENSION|PATHINFO_FILENAME|PATHINFO_DIRNAME));
19-
var_dump(pathinfo($testfile, PATHINFO_EXTENSION|PATHINFO_FILENAME|PATHINFO_BASENAME));
20-
var_dump(pathinfo($testfile, PATHINFO_EXTENSION|PATHINFO_FILENAME));
21-
var_dump(pathinfo($testfile, PATHINFO_EXTENSION|PATHINFO_BASENAME));
22-
var_dump(pathinfo($testfile, PATHINFO_FILENAME|PATHINFO_DIRNAME));
23-
var_dump(pathinfo($testfile, PATHINFO_FILENAME|PATHINFO_BASENAME));
24-
var_dump(pathinfo($testfile, PATHINFO_DIRNAME|PATHINFO_EXTENSION));
25-
var_dump(pathinfo($testfile, PATHINFO_DIRNAME|PATHINFO_BASENAME));
2618

19+
try {
20+
pathinfo($testfile, PATHINFO_EXTENSION|PATHINFO_FILENAME|PATHINFO_DIRNAME);
21+
} catch (\ValueError $e) {
22+
echo $e->getMessage(), PHP_EOL;
23+
}
24+
try {
25+
pathinfo($testfile, PATHINFO_EXTENSION|PATHINFO_FILENAME);
26+
} catch (\ValueError $e) {
27+
echo $e->getMessage(), PHP_EOL;
28+
}
29+
try {
30+
pathinfo($testfile, PATHINFO_EXTENSION|PATHINFO_DIRNAME);
31+
} catch (\ValueError $e) {
32+
echo $e->getMessage(), PHP_EOL;
33+
}
34+
try {
35+
pathinfo($testfile, PATHINFO_FILENAME|PATHINFO_BASENAME);
36+
} catch (\ValueError $e) {
37+
echo $e->getMessage(), PHP_EOL;
38+
}
39+
try {
40+
pathinfo($testfile, PATHINFO_DIRNAME|PATHINFO_EXTENSION);
41+
} catch (\ValueError $e) {
42+
echo $e->getMessage(), PHP_EOL;
43+
}
44+
try {
45+
pathinfo($testfile, PATHINFO_DIRNAME|PATHINFO_BASENAME);
46+
} catch (\ValueError $e) {
47+
echo $e->getMessage(), PHP_EOL;
48+
}
2749

2850
?>
2951
--EXPECTF--
@@ -62,11 +84,9 @@ string(6) "inet.h"
6284
string(4) "inet"
6385
string(1) "h"
6486
string(17) "/usr/include/arpa"
65-
string(17) "/usr/include/arpa"
66-
string(6) "inet.h"
67-
string(1) "h"
68-
string(6) "inet.h"
69-
string(17) "/usr/include/arpa"
70-
string(6) "inet.h"
71-
string(17) "/usr/include/arpa"
72-
string(17) "/usr/include/arpa"
87+
pathinfo(): Argument #2 ($flags) must be only one of the PATHINFO_* constants
88+
pathinfo(): Argument #2 ($flags) must be only one of the PATHINFO_* constants
89+
pathinfo(): Argument #2 ($flags) must be only one of the PATHINFO_* constants
90+
pathinfo(): Argument #2 ($flags) must be only one of the PATHINFO_* constants
91+
pathinfo(): Argument #2 ($flags) must be only one of the PATHINFO_* constants
92+
pathinfo(): Argument #2 ($flags) must be only one of the PATHINFO_* constants

ext/standard/tests/strings/pathinfo.phpt

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,48 @@ var_dump(pathinfo(__FILE__, PATHINFO_BASENAME));
1414
var_dump(pathinfo(__FILE__, PATHINFO_FILENAME));
1515
var_dump(pathinfo(__FILE__, PATHINFO_EXTENSION));
1616
var_dump(pathinfo(__FILE__, PATHINFO_DIRNAME));
17-
var_dump(pathinfo(__FILE__, PATHINFO_EXTENSION|PATHINFO_FILENAME|PATHINFO_DIRNAME));
18-
var_dump(pathinfo(__FILE__, PATHINFO_EXTENSION|PATHINFO_FILENAME|PATHINFO_BASENAME));
19-
var_dump(pathinfo(__FILE__, PATHINFO_EXTENSION|PATHINFO_FILENAME));
20-
var_dump(pathinfo(__FILE__, PATHINFO_EXTENSION|PATHINFO_BASENAME));
21-
var_dump(pathinfo(__FILE__, PATHINFO_FILENAME|PATHINFO_DIRNAME));
22-
var_dump(pathinfo(__FILE__, PATHINFO_FILENAME|PATHINFO_BASENAME));
23-
var_dump(pathinfo(__FILE__, PATHINFO_DIRNAME|PATHINFO_EXTENSION));
24-
var_dump(pathinfo(__FILE__, PATHINFO_DIRNAME|PATHINFO_BASENAME));
17+
18+
try {
19+
pathinfo(__FILE__, PATHINFO_EXTENSION|PATHINFO_FILENAME|PATHINFO_DIRNAME);
20+
} catch (\ValueError $e) {
21+
echo $e->getMessage(), PHP_EOL;
22+
}
23+
try {
24+
pathinfo(__FILE__, PATHINFO_EXTENSION|PATHINFO_FILENAME);
25+
} catch (\ValueError $e) {
26+
echo $e->getMessage(), PHP_EOL;
27+
}
28+
try {
29+
pathinfo(__FILE__, PATHINFO_EXTENSION|PATHINFO_DIRNAME);
30+
} catch (\ValueError $e) {
31+
echo $e->getMessage(), PHP_EOL;
32+
}
33+
try {
34+
pathinfo(__FILE__, PATHINFO_FILENAME|PATHINFO_BASENAME);
35+
} catch (\ValueError $e) {
36+
echo $e->getMessage(), PHP_EOL;
37+
}
38+
try {
39+
pathinfo(__FILE__, PATHINFO_DIRNAME|PATHINFO_EXTENSION);
40+
} catch (\ValueError $e) {
41+
echo $e->getMessage(), PHP_EOL;
42+
}
43+
try {
44+
pathinfo(__FILE__, PATHINFO_DIRNAME|PATHINFO_BASENAME);
45+
} catch (\ValueError $e) {
46+
echo $e->getMessage(), PHP_EOL;
47+
}
48+
49+
try {
50+
pathinfo(__FILE__, PATHINFO_DIRNAME-1);
51+
} catch (\ValueError $e) {
52+
echo $e->getMessage(), PHP_EOL;
53+
}
54+
try {
55+
pathinfo(__FILE__, PATHINFO_ALL+1);
56+
} catch (\ValueError $e) {
57+
echo $e->getMessage(), PHP_EOL;
58+
}
2559

2660
echo "Done\n";
2761
?>
@@ -94,12 +128,12 @@ string(12) "pathinfo.php"
94128
string(8) "pathinfo"
95129
string(3) "php"
96130
string(%d) "%s%estrings"
97-
string(%d) "%s%estrings"
98-
string(12) "pathinfo.php"
99-
string(3) "php"
100-
string(12) "pathinfo.php"
101-
string(%d) "%s%estrings"
102-
string(12) "pathinfo.php"
103-
string(%d) "%s%estrings"
104-
string(%d) "%s%estrings"
131+
pathinfo(): Argument #2 ($flags) must be only one of the PATHINFO_* constants
132+
pathinfo(): Argument #2 ($flags) must be only one of the PATHINFO_* constants
133+
pathinfo(): Argument #2 ($flags) must be only one of the PATHINFO_* constants
134+
pathinfo(): Argument #2 ($flags) must be only one of the PATHINFO_* constants
135+
pathinfo(): Argument #2 ($flags) must be only one of the PATHINFO_* constants
136+
pathinfo(): Argument #2 ($flags) must be only one of the PATHINFO_* constants
137+
pathinfo(): Argument #2 ($flags) must be one of the PATHINFO_* constants
138+
pathinfo(): Argument #2 ($flags) must be one of the PATHINFO_* constants
105139
Done

0 commit comments

Comments
 (0)