Skip to content

Fix GH-10562: Memory leak and invalid state with consecutive ftp_nb_fget #11606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ext/ftp/ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2063,6 +2063,15 @@ ftp_nb_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t
return PHP_FTP_FAILED;
}

if (ftp->data != NULL) {
/* If there is a transfer in action, abort it.
* If we don't, we get an invalid state and memory leaks when the new connection gets opened. */
data_close(ftp, ftp->data);
if (!ftp_getresp(ftp) || (ftp->resp != 226 && ftp->resp != 250)) {
goto bail;
}
}

if (!ftp_type(ftp, type)) {
goto bail;
}
Expand Down
40 changes: 40 additions & 0 deletions ext/ftp/tests/gh10562.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
GH-10562 (Memory leak with consecutive ftp_nb_fget)
--EXTENSIONS--
ftp
pcntl
--FILE--
<?php
require 'server.inc';

$ftp = ftp_connect('127.0.0.1', $port);
if (!$ftp) die("Couldn't connect to the server");

var_dump(ftp_login($ftp, 'anonymous', 'IEUser@'));

$local_file = __DIR__ . DIRECTORY_SEPARATOR . "gh10562.txt";
$fout = fopen($local_file, "w");

// This requests more data, but we don't do the loop to fetch it.
$ret = ftp_nb_fget($ftp, $fout, "fget", FTP_BINARY, 0);
var_dump($ret == FTP_MOREDATA);

// This aborts the previous request, fetches the whole "a story" file.
$ret = ftp_nb_fget($ftp, $fout, "a story", FTP_BINARY, 0);
while ($ret == FTP_MOREDATA) {
$ret = ftp_nb_continue($ftp);
}

fclose($fout);

echo file_get_contents($local_file), "\n";
?>
--CLEAN--
<?php
@unlink(__DIR__ . DIRECTORY_SEPARATOR . "gh10562.txt");
?>
--EXPECT--
bool(true)
bool(true)
BINARYFooBar
For sale: baby shoes, never worn.