xref: /PHP-8.3/ext/curl/tests/bug48203_multi.phpt (revision 0bdc4b8c)
1--TEST--
2Variation of bug #48203 with curl_multi_exec (Crash when file pointers passed to curl are closed before calling curl_multi_exec)
3--EXTENSIONS--
4curl
5--SKIPIF--
6<?php
7if (curl_version()['version_number'] === 0x080a00) {
8    // https://github.com/php/php-src/issues/15997
9    die('xfail due to a libcurl bug');
10}
11?>
12--FILE--
13<?php
14include 'server.inc';
15function checkForClosedFilePointer($target_url, $curl_option, $description) {
16    $fp = fopen(__DIR__ . '/bug48203.tmp', 'w');
17
18    $ch1 = curl_init();
19    $ch2 = curl_init();
20
21    $options = array(
22        CURLOPT_RETURNTRANSFER => 1,
23        $curl_option => $fp,
24        CURLOPT_URL => $target_url,
25    );
26
27    // we also need to set CURLOPT_VERBOSE to test CURLOPT_STDERR properly
28    if (CURLOPT_STDERR == $curl_option) {
29        $options[CURLOPT_VERBOSE] = 1;
30    }
31
32    if (CURLOPT_INFILE == $curl_option) {
33        $options[CURLOPT_UPLOAD] = 1;
34    }
35
36    curl_setopt_array($ch1, $options);
37    curl_setopt_array($ch2, $options);
38
39    fclose($fp); // <-- premature close of $fp caused a crash!
40
41    $mh = curl_multi_init();
42
43    curl_multi_add_handle($mh, $ch1);
44    curl_multi_add_handle($mh, $ch2);
45
46    $active = 0;
47    do {
48        curl_multi_exec($mh, $active);
49    } while ($active > 0);
50
51    curl_multi_remove_handle($mh, $ch1);
52    curl_multi_remove_handle($mh, $ch2);
53    curl_multi_close($mh);
54
55    // Force curl to output results
56    fflush(STDERR);
57    fflush(STDOUT);
58
59    echo "Ok for $description\n";
60}
61
62$options_to_check = array(
63    "CURLOPT_STDERR", "CURLOPT_WRITEHEADER", "CURLOPT_FILE", "CURLOPT_INFILE"
64);
65
66$target_url = curl_cli_server_start();
67foreach($options_to_check as $option) {
68    checkForClosedFilePointer($target_url, constant($option), $option);
69}
70
71?>
72--CLEAN--
73<?php @unlink(__DIR__ . '/bug48203.tmp'); ?>
74--EXPECTF--
75Warning: curl_multi_add_handle(): CURLOPT_STDERR resource has gone away, resetting to stderr in %s on line %d
76%A
77Warning: curl_multi_add_handle(): CURLOPT_STDERR resource has gone away, resetting to stderr in %s on line %d
78%A
79Ok for CURLOPT_STDERR
80
81Warning: curl_multi_add_handle(): CURLOPT_WRITEHEADER resource has gone away, resetting to default in %s on line %d
82
83Warning: curl_multi_add_handle(): CURLOPT_WRITEHEADER resource has gone away, resetting to default in %s on line %d
84Ok for CURLOPT_WRITEHEADER
85
86Warning: curl_multi_add_handle(): CURLOPT_FILE resource has gone away, resetting to default in %s on line %d
87
88Warning: curl_multi_add_handle(): CURLOPT_FILE resource has gone away, resetting to default in %s on line %d
89%AOk for CURLOPT_FILE
90
91Warning: curl_multi_add_handle(): CURLOPT_INFILE resource has gone away, resetting to default in %s on line %d
92
93Warning: curl_multi_add_handle(): CURLOPT_INFILE resource has gone away, resetting to default in %s on line %d
94Ok for CURLOPT_INFILE
95