php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80933 SplFileObject::DROP_NEW_LINE is broken for NUL and CR
Submitted: 2021-04-04 06:15 UTC Modified: 2021-04-07 10:08 UTC
From: gabrielpl at gmail dot com Assigned: cmb (profile)
Status: Closed Package: SPL related
PHP Version: 7.4 OS: Windows
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: gabrielpl at gmail dot com
New email:
PHP Version: OS:

 

 [2021-04-04 06:15 UTC] gabrielpl at gmail dot com
Description:
------------
When the flag DROP_NEW_LINE is enabled in SplFileObject classes, reading a line (e.g. fgets()) stops at null char.

When the flag is disabled, the read operation works as intended.

Test script:
---------------
<?php

// string with a NULL char
$line = "Lorem ipsum \0 dolor sit amet";

$temp = new SplTempFileObject();
$temp->fwrite($line);

$temp->rewind();
$read = $temp->fgets();
echo 'SplFileObject::DROP_NEW_LINE flag -> disabled' . PHP_EOL;
var_dump(strcmp($line, $read), $read);

$temp->rewind();
$temp->setFlags(SplFileObject::DROP_NEW_LINE);
$read = $temp->fgets();
echo PHP_EOL . 'SplFileObject::DROP_NEW_LINE flag -> enabled' . PHP_EOL;
var_dump(strcmp($line, $read), $read);



Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-04-04 07:04 UTC] requinix@php.net
-Status: Open +Status: Verified
 [2021-04-05 15:39 UTC] cmb@php.net
-PHP Version: 8.0.3 +PHP Version: 7.4 -Assigned To: +Assigned To: cmb
 [2021-04-05 15:39 UTC] cmb@php.net
Good catch, @requinix!
 [2021-04-05 16:35 UTC] cmb@php.net
The following pull request has been associated:

Patch Name: Fix #80933: SplFileObject::fgets() stops at NUL byte for DROP_NEW_LINE
On GitHub:  https://github.com/php/php-src/pull/6836
Patch:      https://github.com/php/php-src/pull/6836.patch
 [2021-04-07 00:34 UTC] gabrielpl at gmail dot com
Looking the original source and the patch #80933...
I think the following string will not pass the test:

// string with a carriage return char
$line = "Lorem ipsum \r dolor sit amet";

Could someone confirm?
 [2021-04-07 10:08 UTC] cmb@php.net
-Summary: When flag DROP_NEW_LINE is enabled, line read stops at null char +Summary: SplFileObject::DROP_NEW_LINE is broken for NUL and CR
 [2021-04-07 10:08 UTC] cmb@php.net
Indeed, you're right!  We need to use php_stream_locate_eol()
instead of (php_)strcspn() to properly detect the line endings.
 [2021-04-13 14:50 UTC] git@php.net
Automatic comment on behalf of cmb69
Revision: https://github.com/php/php-src/commit/976e71a2fa239cd7336bdcc56fcd7639b2e4edfd
Log: Fix #80933: SplFileObject::DROP_NEW_LINE is broken for NUL and CR
 [2021-04-13 14:50 UTC] git@php.net
-Status: Verified +Status: Closed
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Feb 02 17:01:31 2025 UTC