php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66588 SplFileObject::fgetcsv incorrectly returns a row on premature EOF
Submitted: 2014-01-27 16:15 UTC Modified: 2021-07-06 11:57 UTC
From: jameshfisher at gmail dot com Assigned:
Status: Closed Package: SPL related
PHP Version: 5.5.8 OS: Ubuntu 12.04.4 LTS
Private report: No CVE-ID: None
 [2014-01-27 16:15 UTC] jameshfisher at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/splfileobject.fgetcsv
---



Test script:
---------------
Assume the file `invalid.csv` contains:

    blah,"blah

Then run the script below:

    #!/usr/bin/env php5
    <?php

    $s = new \SplFileObject('invalid.csv');

    $r = NULL;

    while (!$s->eof())
    {
      var_dump($s->fgetcsv(',','"'));
    }


Expected result:
----------------
An error, signalled by FALSE, as described in the documentation for the method:

    $ ./test.php 
    bool(false)


Actual result:
--------------
The cell in the file that does not have a closing quotation mark is treated as if EOF is the closing quotation mark. This is not compliant with the RFC: https://tools.ietf.org/html/rfc4180

    $ ./test.php 
    array(2) {
      [0]=>
      string(4) "blah"
      [1]=>
      string(5) "blah
    "
    }


Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-03-28 19:50 UTC] levim@php.net
-Status: Open +Status: Verified
 [2020-04-09 08:11 UTC] cmb@php.net
-Type: Bug +Type: Documentation Problem
 [2020-04-09 08:11 UTC] cmb@php.net
This behavior is a deliberate design decision[1], so I don't think
this qualifies as bug; it's rather some form of error resilience.
Of course, that behavior should be documented.

If you, or anybody else, wants to have that behavior changed,
please provide a pull request[2], or start discussion on the
internals mailing list[3]; this bug tracker doesn't seem to be
appropriate for this kind of feature request.

[1] <https://github.com/php/php-src/blob/php-7.3.15/ext/standard/file.c#L2184-L2187>
[2] <https://github.com/php/php-src/pulls>
[3] <https://www.php.net/mailing-lists.php#internals>
 [2021-07-06 11:01 UTC] cmb@php.net
-Type: Documentation Problem +Type: Bug
 [2021-07-06 11:01 UTC] cmb@php.net
> This behavior is a deliberate design decision[1], […]

No, the behavior actually makes no sense.
 [2021-07-06 11:57 UTC] cmb@php.net
To clarify: this is about the different behavior with and without
trailing newline (<https://3v4l.org/VHrCH>); both should behave
the same, and applying Postel's law, neither should return FALSE.
 [2021-09-30 09:33 UTC] cmb@php.net
The following pull request has been associated:

Patch Name: Fix #66588: SplFileObject::fgetcsv incorrectly returns a row on premature EOF.
On GitHub:  https://github.com/php/php-src/pull/7535
Patch:      https://github.com/php/php-src/pull/7535.patch
 [2021-10-01 14:21 UTC] git@php.net
Automatic comment on behalf of Flashwade1990 (author) and nikic (committer)
Revision: https://github.com/php/php-src/commit/52e7e0f521f6002bb84a20e4be5269617afa071d
Log: Fix bug #66588: SplFileObject::fgetcsv incorrectly returns a row on premature EOF
 [2021-10-01 14:21 UTC] git@php.net
-Status: Verified +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 02:01:28 2024 UTC