Skip to content

str_getcsv returns null byte for unterminated quoted string #11982

Closed

Description

@apeschar

Description

The following code:

<?php
echo json_encode(str_getcsv("\""));

Resulted in this output:

["\u0000"]

But I expected this output instead:

[""]

https://3v4l.org/TI1vo

PHP Version

PHP 5.3.0 to now

Operating System

No response

Activity

  1. Girgias commented on Aug 15, 2023

    @Girgias
    Member

    This behaviour dates back to its introduction in PHP 5.3, at this point I think the ship has sailed on "sensible" behaviour for this function. I really need to go back and work onext/csv.

    But @bukka what do you think about this?

  2. bukka commented on Aug 24, 2023

    @bukka
    Member

    I have done some debugging and I think this might be actually a bug that is worth a fix. I see the point about the BC as some apps might potentially rely on that but I think we could still fix it in 8.3 potentially. Specifically this output really does not make any sense to me:

    <?php
    var_dump(str_getcsv('"","a'));
    var_dump(str_getcsv('"","'));
    

    results to

    array(2) {
      [0]=>
      string(0) ""
      [1]=>
      string(1) "a"
    }
    array(2) {
      [0]=>
      string(0) ""
      [1]=>
      string(1) ""
    }
    

    As can be seen the current logic is to ignore missing enclosing character at the end. But in the case of empty strings, it prints \0 which seems like a omission in the code to me as it twice increments bptr without checking for end.

  3. added a commit that references this issue on Aug 25, 2023
    e0dc5bd
  4. bukka commented on Aug 25, 2023

    @bukka
    Member

    The fix for this is in GH-12047 . It took me quite a bit of time to just figure out how that old (created 20 years ago) parser works. At least I have got some knowledge of that now...

  5. added a commit that references this issue on Jan 31, 2024
    aff46d7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions