Skip to content

Commit 45facd1

Browse files
committed
fix memory leak & add test
1 parent a643ccf commit 45facd1

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

ext/standard/http_fopen_wrapper.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -922,8 +922,13 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper, char *path,
922922
if (transfer_encoding) {
923923
php_stream_filter_append(&stream->readfilters, transfer_encoding);
924924
}
925-
} else if (transfer_encoding) {
926-
php_stream_filter_free(transfer_encoding TSRMLS_CC);
925+
} else {
926+
if(response_header) {
927+
Z_DELREF_P(response_header);
928+
}
929+
if (transfer_encoding) {
930+
php_stream_filter_free(transfer_encoding TSRMLS_CC);
931+
}
927932
}
928933

929934
return stream;

ext/standard/tests/http/bug69337.phpt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
Bug #69337 (Stream context leaks when http request fails)
3+
--SKIPIF--
4+
<?php require 'server.inc'; http_server_skipif('tcp://127.0.0.1:22345'); ?>
5+
--INI--
6+
allow_url_fopen=1
7+
allow_url_include=1
8+
--FILE--
9+
<?php
10+
require 'server.inc';
11+
12+
function stream_notification_callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max)
13+
{
14+
if($notification_code == STREAM_NOTIFY_REDIRECTED) {
15+
// $http_response_header is now a string, but will be used as an array
16+
// by php_stream_url_wrap_http_ex() later on
17+
$GLOBALS['http_response_header'] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\0\0\0\0";
18+
}
19+
}
20+
21+
$ctx = stream_context_create();
22+
stream_context_set_params($ctx, array("notification" => "stream_notification_callback"));
23+
24+
$responses = array(
25+
"data://text/plain,HTTP/1.0 302 Found\r\nLocation: http://127.0.0.1:22345/try-again\r\n\r\n",
26+
"data://text/plain,HTTP/1.0 404 Not Found\r\n\r\n",
27+
);
28+
29+
$pid = http_server("tcp://127.0.0.1:22345", $responses, $output);
30+
31+
$f = file_get_contents('http://127.0.0.1:22345/', 0, $ctx);
32+
33+
http_server_kill($pid);
34+
var_dump($f);
35+
?>
36+
==DONE==
37+
--EXPECTF--
38+
string(26) "HTTP/1.0 404 Not Found
39+
40+
"
41+
==DONE==

0 commit comments

Comments
 (0)