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