Skip to content

Commit eacfbd9

Browse files
committed
Merge branch 'PHP-8.2'
* PHP-8.2: Fix GH-10562: Memory leak and invalid state with consecutive ftp_nb_fget
2 parents 72a163a + 4dcb5af commit eacfbd9

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

ext/ftp/ftp.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,6 +2063,15 @@ ftp_nb_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t
20632063
return PHP_FTP_FAILED;
20642064
}
20652065

2066+
if (ftp->data != NULL) {
2067+
/* If there is a transfer in action, abort it.
2068+
* If we don't, we get an invalid state and memory leaks when the new connection gets opened. */
2069+
data_close(ftp, ftp->data);
2070+
if (!ftp_getresp(ftp) || (ftp->resp != 226 && ftp->resp != 250)) {
2071+
goto bail;
2072+
}
2073+
}
2074+
20662075
if (!ftp_type(ftp, type)) {
20672076
goto bail;
20682077
}

ext/ftp/tests/gh10562.phpt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
GH-10562 (Memory leak with consecutive ftp_nb_fget)
3+
--EXTENSIONS--
4+
ftp
5+
pcntl
6+
--FILE--
7+
<?php
8+
require 'server.inc';
9+
10+
$ftp = ftp_connect('127.0.0.1', $port);
11+
if (!$ftp) die("Couldn't connect to the server");
12+
13+
var_dump(ftp_login($ftp, 'anonymous', 'IEUser@'));
14+
15+
$local_file = __DIR__ . DIRECTORY_SEPARATOR . "gh10562.txt";
16+
$fout = fopen($local_file, "w");
17+
18+
// This requests more data, but we don't do the loop to fetch it.
19+
$ret = ftp_nb_fget($ftp, $fout, "fget", FTP_BINARY, 0);
20+
var_dump($ret == FTP_MOREDATA);
21+
22+
// This aborts the previous request, fetches the whole "a story" file.
23+
$ret = ftp_nb_fget($ftp, $fout, "a story", FTP_BINARY, 0);
24+
while ($ret == FTP_MOREDATA) {
25+
$ret = ftp_nb_continue($ftp);
26+
}
27+
28+
fclose($fout);
29+
30+
echo file_get_contents($local_file), "\n";
31+
?>
32+
--CLEAN--
33+
<?php
34+
@unlink(__DIR__ . DIRECTORY_SEPARATOR . "gh10562.txt");
35+
?>
36+
--EXPECT--
37+
bool(true)
38+
bool(true)
39+
BINARYFooBar
40+
For sale: baby shoes, never worn.

0 commit comments

Comments
 (0)