diff --git a/ext/ftp/ftp.c b/ext/ftp/ftp.c index 09367097cdb5d..9253edc558990 100644 --- a/ext/ftp/ftp.c +++ b/ext/ftp/ftp.c @@ -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; } diff --git a/ext/ftp/tests/gh10562.phpt b/ext/ftp/tests/gh10562.phpt new file mode 100644 index 0000000000000..c2a4357521b48 --- /dev/null +++ b/ext/ftp/tests/gh10562.phpt @@ -0,0 +1,40 @@ +--TEST-- +GH-10562 (Memory leak with consecutive ftp_nb_fget) +--EXTENSIONS-- +ftp +pcntl +--FILE-- + +--CLEAN-- + +--EXPECT-- +bool(true) +bool(true) +BINARYFooBar +For sale: baby shoes, never worn.