Skip to content

Commit 90705d4

Browse files
committed
Treat invalid characters in basename() consistently
Always simply ignore (pass through) them. Previously the behavior depended on where the invalid character occurred, as it messed up the state management.
1 parent d50a126 commit 90705d4

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

ext/standard/string.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,11 +1515,6 @@ PHPAPI zend_string *php_basename(const char *s, size_t len, char *suffix, size_t
15151515
int inc_len = (*s == '\0' ? 1 : php_mblen(s, len));
15161516

15171517
switch (inc_len) {
1518-
case -2:
1519-
case -1:
1520-
inc_len = 1;
1521-
php_mb_reset();
1522-
break;
15231518
case 0:
15241519
goto quit_loop;
15251520
case 1:
@@ -1553,6 +1548,11 @@ PHPAPI zend_string *php_basename(const char *s, size_t len, char *suffix, size_t
15531548
}
15541549
break;
15551550
default:
1551+
if (inc_len < 0) {
1552+
/* If character is invalid, treat it like other non-significant characters. */
1553+
inc_len = 1;
1554+
php_mb_reset();
1555+
}
15561556
if (state == 0) {
15571557
basename_start = s;
15581558
state = 1;

ext/standard/tests/strings/basename_invalid_path.phpt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ if((substr(PHP_OS, 0, 3) == "WIN"))
1313
If the filename ends in suffix this will also be cut off.
1414
*/
1515

16-
var_dump(basename(chr(-1)));
16+
setlocale(LC_CTYPE, "C");
17+
var_dump(bin2hex(basename("\xff")));
18+
var_dump(bin2hex(basename("a\xffb")));
1719

1820
echo "Done\n";
1921
--EXPECT--
20-
string(0) ""
22+
string(2) "ff"
23+
string(6) "61ff62"
2124
Done

0 commit comments

Comments
 (0)