Skip to content

Integer overflow on stream_notification_callback byte_max parameter with files bigger than 2GB #15034

Closed

Description

@yortx

Description

Downloading a file from an http url using fopen, and trying to keep track of download progress using the stream_notification_callback returns a negative value in the $bytes_max parameter if the file to download is bigger than 2GB. Looks like an integer overflow.

The $bytes_transferred parameter, on the other hand, reports the size transferred correctly.

The following code:

<?php
        $params = ['notification' => function(
            int $notification_code,
            int $severity,
            ?string $message,
            int $message_code,
            int $bytes_transferred,
            int $bytes_max
        ) {
            fwrite(STDERR, "Downloaded {$bytes_transferred} out of {$bytes_max}\n");
        }];
        $contextResource = \stream_context_create([], $params);

        $url = "https://laotzu.ftp.acc.umu.se/cdimage/archive/12.5.0/amd64/iso-dvd/debian-12.5.0-amd64-DVD-1.iso";
        $resource = \fopen((string) $url, 'r', false, $contextResource);
        while(!feof($resource)) {
            fread($resource, 1024*1024);
        }

Resulted in this output:

Downloaded 0 out of -301989888
Downloaded 7937 out of -301989888
Downloaded 15937 out of -301989888
Downloaded 23937 out of -301989888
...
Downloaded 3992977408 out of -301989888

But I expected this output instead:

Downloaded 0 out of 3992977408
Downloaded 7956 out of 3992977408
...
Downloaded 3992977408 out of 3992977408

My system is amd 64bits. The problem only happens when the file to download is bigger than 2GB, as it is with the iso image I used in the example (which is near 4GB in size).

PHP Version

PHP 8.2.20

Operating System

GNU/Debian 12.6

Activity

  1. ndossche commented on Jul 19, 2024

    @ndossche
    Member

    I'll have a look

  2. added 3 commits that reference this issue on Jul 19, 2024
    f81d7a8
    cfcc2a3
    c26d1a3
  3. added a commit that references this issue on Jul 21, 2024
    af830d8
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