Skip to content

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

Closed
@yortx

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

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