Skip to content

Commit bebb0b7

Browse files
committed
Merge branch 'PHP-8.0'
* PHP-8.0: Fix #80933: SplFileObject::DROP_NEW_LINE is broken for NUL and CR
2 parents 91908bc + 80f921d commit bebb0b7

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

ext/spl/spl_directory.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,8 +1896,13 @@ static int spl_filesystem_file_read(spl_filesystem_object *intern, int silent) /
18961896
intern->u.file.current_line_len = 0;
18971897
} else {
18981898
if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_DROP_NEW_LINE)) {
1899-
line_len = strcspn(buf, "\r\n");
1900-
buf[line_len] = '\0';
1899+
if (line_len > 0 && buf[line_len - 1] == '\n') {
1900+
line_len--;
1901+
if (line_len > 0 && buf[line_len - 1] == '\r') {
1902+
line_len--;
1903+
}
1904+
buf[line_len] = '\0';
1905+
}
19011906
}
19021907

19031908
intern->u.file.current_line = buf;

ext/spl/tests/bug80933.phpt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
Bug #80933 (SplFileObject::DROP_NEW_LINE is broken for NUL and CR)
3+
--FILE--
4+
<?php
5+
$lines = [
6+
"Lorem ipsum \0 dolor sit amet", // string with NUL
7+
"Lorem ipsum \r dolor sit amet", // string with CR
8+
];
9+
foreach ($lines as $line) {
10+
$temp = new SplTempFileObject();
11+
$temp->fwrite($line);
12+
13+
$temp->rewind();
14+
$read = $temp->fgets();
15+
var_dump($line === $read);
16+
17+
$temp->rewind();
18+
$temp->setFlags(SplFileObject::DROP_NEW_LINE);
19+
$read = $temp->fgets();
20+
var_dump($line === $read);
21+
}
22+
?>
23+
--EXPECT--
24+
bool(true)
25+
bool(true)
26+
bool(true)
27+
bool(true)

0 commit comments

Comments
 (0)