xref: /PHP-8.0/ext/curl/tests/bug54798.phpt (revision f8d79582)
1--TEST--
2Bug #54798 (Segfault when CURLOPT_STDERR file pointer is closed before calling curl_exec)
3--SKIPIF--
4<?php
5include 'skipif.inc';
6?>
7--FILE--
8<?php
9
10function checkForClosedFilePointer($host, $curl_option, $description) {
11    $fp = fopen(__DIR__ . '/bug54798.tmp', 'w+');
12
13    $ch = curl_init();
14
15    // we also need CURLOPT_VERBOSE to be set to test CURLOPT_STDERR properly
16    if (CURLOPT_STDERR == $curl_option) {
17        curl_setopt($ch, CURLOPT_VERBOSE, 1);
18    }
19
20    if (CURLOPT_INFILE == $curl_option) {
21        curl_setopt($ch, CURLOPT_UPLOAD, 1);
22    }
23
24    curl_setopt($ch, $curl_option, $fp);
25
26    curl_setopt($ch, CURLOPT_URL, $host);
27    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
28
29    fclose($fp); // <-- premature close of $fp caused a crash!
30
31    curl_exec($ch);
32
33    curl_close($ch);
34
35    echo "Ok for $description\n";
36}
37
38$options_to_check = array(
39    "CURLOPT_STDERR",
40    "CURLOPT_WRITEHEADER",
41    "CURLOPT_FILE",
42    "CURLOPT_INFILE"
43);
44
45include 'server.inc';
46$host = curl_cli_server_start();
47foreach($options_to_check as $option) {
48    checkForClosedFilePointer($host, constant($option), $option);
49}
50
51?>
52--CLEAN--
53<?php @unlink(__DIR__ . '/bug54798.tmp'); ?>
54--EXPECTF--
55%AOk for CURLOPT_STDERR
56
57%AOk for CURLOPT_WRITEHEADER
58
59%AHello World!
60Hello World!Ok for CURLOPT_FILE
61
62%AOk for CURLOPT_INFILE
63