File: gh13136.phpt

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (55 lines) | stat: -rw-r--r-- 1,369 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
--TEST--
GH-13071 (Copying large files using mmap-able source streams may exhaust available memory and fail)
--FILE--
<?php

class CustomStream {
    public $context;
    protected $file;
    protected $seekable;
    public static int $writes = 0;

    public function stream_open($path, $mode, $options, &$opened_path) {
        $path = $this->trim_path($path);
        $this->file = fopen($path, $mode);
        return true;
    }

    public function stream_close() {
        fclose($this->file);
        return true;
    }

    public function stream_write($data) {
        self::$writes++;
        return fwrite($this->file, $data);
    }

    public function url_stat($path, $flags) {
        return false;
    }

    private function trim_path(string $path): string {
        return substr($path, strlen("up://"));
    }
}

file_put_contents(__DIR__ . "/gh13071.tmp", str_repeat("a", 1024 * 1024 * 8));

stream_wrapper_register("up", CustomStream::class, STREAM_IS_URL);

$old_limit = ini_get("memory_limit");
ini_set("memory_limit", memory_get_usage(true) + 5 * 1024 * 1024);
copy(__DIR__ . "/gh13071.tmp", "up://" . __DIR__ . "/gh13071.out.tmp");
ini_set("memory_limit", $old_limit);

echo "Done ", CustomStream::$writes, " writes\n";

?>
--CLEAN--
<?php
@unlink(__DIR__ . "/gh13071.tmp");
@unlink(__DIR__ . "/gh13071.out.tmp");
?>
--EXPECT--
Done 1024 writes