Cleanup internal data_close API

This always returns NULL. Also passing in data is not necessary as it is
always equal to ftp->data.
This commit is contained in:
Niels Dossche 2023-12-02 14:28:46 +01:00
parent f601963d4f
commit 35cf7abec7

View File

@ -94,8 +94,8 @@ static databuf_t* ftp_getdata(ftpbuf_t *ftp);
/* accepts the data connection, returns updated data buffer */ /* accepts the data connection, returns updated data buffer */
static databuf_t* data_accept(databuf_t *data, ftpbuf_t *ftp); static databuf_t* data_accept(databuf_t *data, ftpbuf_t *ftp);
/* closes the data connection, returns NULL */ /* closes the data connection, no-op if already closed */
static databuf_t* data_close(ftpbuf_t *ftp, databuf_t *data); static void data_close(ftpbuf_t *ftp);
/* generic file lister */ /* generic file lister */
static char** ftp_genlist(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, const char *path, const size_t path_len); static char** ftp_genlist(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, const char *path, const size_t path_len);
@ -170,9 +170,7 @@ ftp_close(ftpbuf_t *ftp)
if (ftp->last_ssl_session) { if (ftp->last_ssl_session) {
SSL_SESSION_free(ftp->last_ssl_session); SSL_SESSION_free(ftp->last_ssl_session);
} }
if (ftp->data) { data_close(ftp);
data_close(ftp, ftp->data);
}
if (ftp->stream && ftp->closestream) { if (ftp->stream && ftp->closestream) {
php_stream_close(ftp->stream); php_stream_close(ftp->stream);
} }
@ -963,7 +961,7 @@ ftp_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t pat
} }
} }
ftp->data = data = data_close(ftp, data); data_close(ftp);
if (!ftp_getresp(ftp) || (ftp->resp != 226 && ftp->resp != 250)) { if (!ftp_getresp(ftp) || (ftp->resp != 226 && ftp->resp != 250)) {
goto bail; goto bail;
@ -971,7 +969,7 @@ ftp_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t pat
return 1; return 1;
bail: bail:
ftp->data = data_close(ftp, data); data_close(ftp);
return 0; return 0;
} }
/* }}} */ /* }}} */
@ -1045,14 +1043,14 @@ ftp_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *inst
if (size && my_send(ftp, data->fd, data->buf, size) != size) { if (size && my_send(ftp, data->fd, data->buf, size) != size) {
goto bail; goto bail;
} }
ftp->data = data = data_close(ftp, data); data_close(ftp);
if (!ftp_getresp(ftp) || (ftp->resp != 226 && ftp->resp != 250 && ftp->resp != 200)) { if (!ftp_getresp(ftp) || (ftp->resp != 226 && ftp->resp != 250 && ftp->resp != 200)) {
goto bail; goto bail;
} }
return 1; return 1;
bail: bail:
ftp->data = data_close(ftp, data); data_close(ftp);
return 0; return 0;
} }
/* }}} */ /* }}} */
@ -1112,14 +1110,14 @@ ftp_append(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *i
if (size && my_send(ftp, data->fd, data->buf, size) != size) { if (size && my_send(ftp, data->fd, data->buf, size) != size) {
goto bail; goto bail;
} }
ftp->data = data = data_close(ftp, data); data_close(ftp);
if (!ftp_getresp(ftp) || (ftp->resp != 226 && ftp->resp != 250 && ftp->resp != 200)) { if (!ftp_getresp(ftp) || (ftp->resp != 226 && ftp->resp != 250 && ftp->resp != 200)) {
goto bail; goto bail;
} }
return 1; return 1;
bail: bail:
ftp->data = data_close(ftp, data); data_close(ftp);
return 0; return 0;
} }
/* }}} */ /* }}} */
@ -1938,11 +1936,12 @@ static void ftp_ssl_shutdown(ftpbuf_t *ftp, php_socket_t fd, SSL *ssl_handle) {
/* }}} */ /* }}} */
/* {{{ data_close */ /* {{{ data_close */
databuf_t* void data_close(ftpbuf_t *ftp)
data_close(ftpbuf_t *ftp, databuf_t *data)
{ {
ZEND_ASSERT(ftp != NULL);
databuf_t *data = ftp->data;
if (data == NULL) { if (data == NULL) {
return NULL; return;
} }
if (data->listener != -1) { if (data->listener != -1) {
#ifdef HAVE_FTP_SSL #ifdef HAVE_FTP_SSL
@ -1964,11 +1963,8 @@ data_close(ftpbuf_t *ftp, databuf_t *data)
#endif #endif
closesocket(data->fd); closesocket(data->fd);
} }
if (ftp) { ftp->data = NULL;
ftp->data = NULL;
}
efree(data); efree(data);
return NULL;
} }
/* }}} */ /* }}} */
@ -2010,7 +2006,7 @@ ftp_genlist(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, const char *pa
/* some servers don't open a ftp-data connection if the directory is empty */ /* some servers don't open a ftp-data connection if the directory is empty */
if (ftp->resp == 226) { if (ftp->resp == 226) {
ftp->data = data_close(ftp, data); data_close(ftp);
php_stream_close(tmpstream); php_stream_close(tmpstream);
return ecalloc(1, sizeof(char*)); return ecalloc(1, sizeof(char*));
} }
@ -2038,7 +2034,7 @@ ftp_genlist(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, const char *pa
} }
} }
ftp->data = data_close(ftp, data); data_close(ftp);
php_stream_rewind(tmpstream); php_stream_rewind(tmpstream);
@ -2068,7 +2064,7 @@ ftp_genlist(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, const char *pa
return ret; return ret;
bail: bail:
ftp->data = data_close(ftp, data); data_close(ftp);
php_stream_close(tmpstream); php_stream_close(tmpstream);
if (ret) if (ret)
efree(ret); efree(ret);
@ -2090,7 +2086,7 @@ ftp_nb_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t
if (ftp->data != NULL) { if (ftp->data != NULL) {
/* If there is a transfer in action, abort it. /* 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. */ * If we don't, we get an invalid state and memory leaks when the new connection gets opened. */
data_close(ftp, ftp->data); data_close(ftp);
if (!ftp_getresp(ftp) || (ftp->resp != 226 && ftp->resp != 250)) { if (!ftp_getresp(ftp) || (ftp->resp != 226 && ftp->resp != 250)) {
goto bail; goto bail;
} }
@ -2137,7 +2133,7 @@ ftp_nb_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t
return (ftp_nb_continue_read(ftp)); return (ftp_nb_continue_read(ftp));
bail: bail:
ftp->data = data_close(ftp, data); data_close(ftp);
return PHP_FTP_FAILED; return PHP_FTP_FAILED;
} }
/* }}} */ /* }}} */
@ -2189,7 +2185,7 @@ ftp_nb_continue_read(ftpbuf_t *ftp)
php_stream_putc(ftp->stream, '\r'); php_stream_putc(ftp->stream, '\r');
} }
ftp->data = data = data_close(ftp, data); data_close(ftp);
if (!ftp_getresp(ftp) || (ftp->resp != 226 && ftp->resp != 250)) { if (!ftp_getresp(ftp) || (ftp->resp != 226 && ftp->resp != 250)) {
goto bail; goto bail;
@ -2199,7 +2195,7 @@ ftp_nb_continue_read(ftpbuf_t *ftp)
return PHP_FTP_FINISHED; return PHP_FTP_FINISHED;
bail: bail:
ftp->nb = 0; ftp->nb = 0;
ftp->data = data_close(ftp, data); data_close(ftp);
return PHP_FTP_FAILED; return PHP_FTP_FAILED;
} }
/* }}} */ /* }}} */
@ -2251,7 +2247,7 @@ ftp_nb_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *i
return (ftp_nb_continue_write(ftp)); return (ftp_nb_continue_write(ftp));
bail: bail:
ftp->data = data_close(ftp, data); data_close(ftp);
return PHP_FTP_FAILED; return PHP_FTP_FAILED;
} }
/* }}} */ /* }}} */
@ -2294,7 +2290,7 @@ ftp_nb_continue_write(ftpbuf_t *ftp)
if (size && my_send(ftp, ftp->data->fd, ftp->data->buf, size) != size) { if (size && my_send(ftp, ftp->data->fd, ftp->data->buf, size) != size) {
goto bail; goto bail;
} }
ftp->data = data_close(ftp, ftp->data); data_close(ftp);
if (!ftp_getresp(ftp) || (ftp->resp != 226 && ftp->resp != 250)) { if (!ftp_getresp(ftp) || (ftp->resp != 226 && ftp->resp != 250)) {
goto bail; goto bail;
@ -2302,7 +2298,7 @@ ftp_nb_continue_write(ftpbuf_t *ftp)
ftp->nb = 0; ftp->nb = 0;
return PHP_FTP_FINISHED; return PHP_FTP_FINISHED;
bail: bail:
ftp->data = data_close(ftp, ftp->data); data_close(ftp);
ftp->nb = 0; ftp->nb = 0;
return PHP_FTP_FAILED; return PHP_FTP_FAILED;
} }