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