Skip to content

[8.2] stream_copy_to_stream doesn't work anymore with resource opened in a append mode #9779

Closed

Description

@Slamdunk

Description

The following code:

<?php
file_put_contents("./source", "bar");
file_put_contents("./dest", "foo");
$sourceHandle = fopen("./source", "r");
$destHandle = fopen("./dest", "a");
stream_copy_to_stream($sourceHandle, $destHandle);
fclose($sourceHandle);
fclose($destHandle);
var_dump(file_get_contents("./dest"));

Resulted in this output:

string(3) "foo"

But I expected this output instead:

string(6) "foobar"

PHP Version

PHP 8.2.0RC3 (cli) (built: Sep 29 2022 22:31:18) (NTS)

Operating System

Ubuntu 22.04.1

Activity

  1. cmb69 commented on Oct 19, 2022

    @cmb69
    Member

    I cannot reproduce this, neither with PHP 8.2.0RC3 nor with current PHP-8.2 HEAD; this is on Windows, though.

    What are the actual file contents? Is it just bar?

  2. Slamdunk commented on Oct 19, 2022

    @Slamdunk
    Author

    What are the actual file contents? Is it just bar?

    Sorry, I've updated the bug report to be more descriptive in the expected vs actual output: it's foo but should be foobar

    I cannot reproduce this, neither with PHP 8.2.0RC3 nor with current PHP-8.2 HEAD; this is on Windows, though.

    We had this issue in shivammathur/setup-php@v2 Github Action on PHP 8.2.0-dev (2e6b317fe30cd28fc73cf1c08ca472f6e6c534b3):
    https://github.com/openspout/openspout/actions/runs/3279348682/jobs/5398785191#step:3:12
    As well as on my computer:

    $ lsb_release -a
    No LSB modules are available.
    Distributor ID: Ubuntu
    Description:    Ubuntu 22.04.1 LTS
    Release:        22.04
    Codename:       jammy
    
    $ uname -a
    Linux OptiPlex-7060 5.15.0-50-generic #56-Ubuntu SMP Tue Sep 20 13:23:26 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
    
    $ php8.2 -v
    PHP 8.2.0RC3 (cli) (built: Sep 29 2022 22:31:18) (NTS)
    Copyright (c) The PHP Group
    Zend Engine v4.2.0RC3, Copyright (c) Zend Technologies
        with Zend OPcache v8.2.0RC3, Copyright (c), by Zend Technologies
    
    $ dpkg -l|grep php8.2
    ii  php8.2-bcmath                            8.2.0~rc3-1+ubuntu22.04.1+deb.sury.org+2               amd64        Bcmath module for PHP
    ii  php8.2-cli                               8.2.0~rc3-1+ubuntu22.04.1+deb.sury.org+2               amd64        command-line interpreter for the PHP scripting language
    ii  php8.2-common                            8.2.0~rc3-1+ubuntu22.04.1+deb.sury.org+2               amd64        documentation, examples and common module for PHP
    ii  php8.2-curl                              8.2.0~rc3-1+ubuntu22.04.1+deb.sury.org+2               amd64        CURL module for PHP
    ii  php8.2-gd                                8.2.0~rc3-1+ubuntu22.04.1+deb.sury.org+2               amd64        GD module for PHP
    ii  php8.2-imap                              8.2.0~rc3-1+ubuntu22.04.1+deb.sury.org+2               amd64        IMAP module for PHP
    ii  php8.2-intl                              8.2.0~rc3-1+ubuntu22.04.1+deb.sury.org+2               amd64        Internationalisation module for PHP
    ii  php8.2-mbstring                          8.2.0~rc3-1+ubuntu22.04.1+deb.sury.org+2               amd64        MBSTRING module for PHP
    ii  php8.2-mysql                             8.2.0~rc3-1+ubuntu22.04.1+deb.sury.org+2               amd64        MySQL module for PHP
    ii  php8.2-opcache                           8.2.0~rc3-1+ubuntu22.04.1+deb.sury.org+2               amd64        Zend OpCache module for PHP
    ii  php8.2-readline                          8.2.0~rc3-1+ubuntu22.04.1+deb.sury.org+2               amd64        readline module for PHP
    ii  php8.2-soap                              8.2.0~rc3-1+ubuntu22.04.1+deb.sury.org+2               amd64        SOAP module for PHP
    ii  php8.2-sqlite3                           8.2.0~rc3-1+ubuntu22.04.1+deb.sury.org+2               amd64        SQLite3 module for PHP
    ii  php8.2-xml                               8.2.0~rc3-1+ubuntu22.04.1+deb.sury.org+2               amd64        DOM, SimpleXML, XML, and XSL module for PHP
    ii  php8.2-zip                               8.2.0~rc3-1+ubuntu22.04.1+deb.sury.org+2               amd64        Zip module for PHP
    
    $ cat /etc/apt/sources.list.d/ondrej-ubuntu-php-jammy.list 
    deb https://ppa.launchpadcontent.net/ondrej/php/ubuntu/ jammy main
    
  3. jhdxr commented on Oct 19, 2022

    @jhdxr
    Member

    I cannot reproduce this as well. Considering permission issue?

    Env: MBP M1

  4. Slamdunk commented on Oct 19, 2022

    @Slamdunk
    Author

    Considering permission issue?

    Unlikely:

    $ cd $(mktemp -d)
    
    $ cat <<'EOF' > test.php
    > <?php
    file_put_contents("./source", "bar");
    file_put_contents("./dest", "foo");
    $sourceHandle = fopen("./source", "r");
    $destHandle = fopen("./dest", "a");
    stream_copy_to_stream($sourceHandle, $destHandle);
    fclose($sourceHandle);
    fclose($destHandle);
    var_dump(file_get_contents("./dest"));
    > EOF
    
    $ php8.0 test.php 
    string(6) "foobar"
    
    $ php8.1 test.php 
    string(6) "foobar"
    
    $ php8.2 test.php 
    string(3) "foo"
  5. cmb69 commented on Oct 19, 2022

    @cmb69
    Member

    Please check if that is a duplicate of #9653 (unlikely but maybe it is).

  6. Slamdunk commented on Oct 19, 2022

    @Slamdunk
    Author

    Please check if that is a duplicate of #9653 (unlikely but maybe it is).

    Nope:

    $ cat <<'EOF' > test.php
    <?php
    file_put_contents("test.txt", "foobar");
    var_dump(copy("test.txt", "testcopy.txt"));
    var_dump(file_get_contents("testcopy.txt"));
    EOF
    
    $ php8.0 test.php 
    bool(true)
    string(6) "foobar"
    
    $ php8.1 test.php 
    bool(true)
    string(6) "foobar"
    
    $ php8.2 test.php 
    bool(true)
    string(6) "foobar"
  7. Slamdunk commented on Oct 19, 2022

    @Slamdunk
    Author

    BTW, the error also happens in the official Docker images:

    $ docker run -it --rm -w /test -v $PWD:/test php:8.2-rc php -v
    PHP 8.2.0RC4 (cli) (built: Oct 13 2022 21:22:32) (NTS)
    Copyright (c) The PHP Group
    Zend Engine v4.2.0RC4, Copyright (c) Zend Technologies
    
    $ docker run -it --rm -w /test -v $PWD:/test php:8.2-rc php test.php
    string(3) "foo"
  8. Girgias commented on Oct 20, 2022

    @Girgias
    Member

    BTW, the error also happens in the official Docker images:

    $ docker run -it --rm -w /test -v $PWD:/test php:8.2-rc php -v
    PHP 8.2.0RC4 (cli) (built: Oct 13 2022 21:22:32) (NTS)
    Copyright (c) The PHP Group
    Zend Engine v4.2.0RC4, Copyright (c) Zend Technologies
    
    $ docker run -it --rm -w /test -v $PWD:/test php:8.2-rc php test.php
    string(3) "foo"
    

    FYI we do not provide official Docker images, Docker just makes them.

  9. jhdxr commented on Oct 21, 2022

    @jhdxr
    Member

    I cannot reproduce it on my rocky linux 8, but I do reproduce it with the docker image given above.

    I doubt it's related to #8413

    copy_file_range may not work well with O_APPEND. ref

    EBADF The O_APPEND flag is set for the open file description
    (see open(2)) referred to by the file descriptor fd_out.

    As I don't have ubuntu on hand, maybe someone working on ubuntu can look into this?

  10. kocoten1992 commented on Oct 21, 2022

    @kocoten1992

    image

    can confirm this.

    P/s: I'm using php ppa of https://launchpad.net/~ondrej/+archive/ubuntu/php

    No LSB modules are available.
    Distributor ID:	Ubuntu
    Description:	Ubuntu 22.04.1 LTS
    Release:	22.04
    Codename:	jammy
    
  11. bukka commented on Oct 22, 2022

    @bukka
    Member

    Fix in #9808

  12. bukka commented on Oct 23, 2022

    @bukka
    Member

    Fixed by b732d80

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