diff --git a/ext/spl/spl_directory.c b/ext/spl/spl_directory.c index daed72d847089..202a815e82c77 100644 --- a/ext/spl/spl_directory.c +++ b/ext/spl/spl_directory.c @@ -2046,8 +2046,13 @@ static int spl_filesystem_file_read(spl_filesystem_object *intern, int silent) / intern->u.file.current_line_len = 0; } else { if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_DROP_NEW_LINE)) { - line_len = strcspn(buf, "\r\n"); - buf[line_len] = '\0'; + if (line_len > 0 && buf[line_len - 1] == '\n') { + line_len--; + if (line_len > 0 && buf[line_len - 1] == '\r') { + line_len--; + } + buf[line_len] = '\0'; + } } intern->u.file.current_line = buf; diff --git a/ext/spl/tests/bug80933.phpt b/ext/spl/tests/bug80933.phpt new file mode 100644 index 0000000000000..78e94aba409b7 --- /dev/null +++ b/ext/spl/tests/bug80933.phpt @@ -0,0 +1,27 @@ +--TEST-- +Bug #80933 (SplFileObject::DROP_NEW_LINE is broken for NUL and CR) +--FILE-- +fwrite($line); + + $temp->rewind(); + $read = $temp->fgets(); + var_dump($line === $read); + + $temp->rewind(); + $temp->setFlags(SplFileObject::DROP_NEW_LINE); + $read = $temp->fgets(); + var_dump($line === $read); +} +?> +--EXPECT-- +bool(true) +bool(true) +bool(true) +bool(true)