From 3d41cb012a536ace5c523f3629cfcc56dabbe4b6 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Sun, 30 Mar 2025 17:49:32 +0100 Subject: [PATCH 01/14] ext/fileinfo: Use magic_setflags() directly The only way this function returns -1 is if: > magic_setflags() returns -1 on systems that don't support utime(3), or utimes(2) when MAGIC_PRESERVE_ATIME is set. This is extremely unlikely and if this would happen we currently have a return type violation. --- ext/fileinfo/fileinfo.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/ext/fileinfo/fileinfo.c b/ext/fileinfo/fileinfo.c index 52172dfbd692d..4311afff1802d 100644 --- a/ext/fileinfo/fileinfo.c +++ b/ext/fileinfo/fileinfo.c @@ -94,14 +94,6 @@ PHP_FILEINFO_API zend_object *finfo_objects_new(zend_class_entry *class_type) } /* }}} */ -#define FINFO_SET_OPTION(magic, options) \ - if (magic_setflags(magic, options) == -1) { \ - php_error_docref(NULL, E_WARNING, "Failed to set option '" ZEND_LONG_FMT "' %d:%s", \ - options, magic_errno(magic), magic_error(magic)); \ - RETURN_FALSE; \ - } -/* }}} */ - /* {{{ PHP_MINIT_FUNCTION */ PHP_MINIT_FUNCTION(finfo) { @@ -276,7 +268,9 @@ PHP_FUNCTION(finfo_set_flags) } FILEINFO_FROM_OBJECT(finfo, self); - FINFO_SET_OPTION(finfo->magic, options) + /* We do not check the return value as it can only ever fail if options contains MAGIC_PRESERVE_ATIME + * and the system neither has utime(3) nor utimes(2). Something incredibly unlikely. */ + magic_setflags(finfo->magic, options); finfo->options = options; RETURN_TRUE; @@ -337,7 +331,9 @@ static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mime /* Set options for the current file/buffer. */ if (options) { - FINFO_SET_OPTION(magic, options) + /* We do not check the return value as it can only ever fail if options contains MAGIC_PRESERVE_ATIME + * and the system neither has utime(3) nor utimes(2). Something incredibly unlikely. */ + magic_setflags(magic, options); } switch (mode) { @@ -436,9 +432,8 @@ static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mime /* Restore options */ if (options) { - FINFO_SET_OPTION(magic, finfo->options) + magic_setflags(magic, finfo->options); } - return; } /* }}} */ From fabee4e2442c48fbb482329d0e87dc1e7bb2fc2e Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Sun, 30 Mar 2025 18:11:42 +0100 Subject: [PATCH 02/14] ext/fileinfo: Separate implementations of functions Instead of relying on a "god" function --- UPGRADING | 6 + ext/fileinfo/fileinfo.c | 293 ++++++++++++----------- ext/fileinfo/tests/finfo_file_001.phpt | 2 +- ext/fileinfo/tests/finfo_file_basic.phpt | 2 +- 4 files changed, 161 insertions(+), 142 deletions(-) diff --git a/UPGRADING b/UPGRADING index 9dfc984f8917d..082439b9264c8 100644 --- a/UPGRADING +++ b/UPGRADING @@ -45,6 +45,12 @@ PHP 8.5 UPGRADE NOTES change, but should closer match user expectations, demonstrated by GH-15753 and GH-16198. +- FileInfo: + . finfo_file() and finfo::file() now throws a ValueError instead of a + TypeError when $filename contains nul bytes. + This aligns the type of Error thrown to be consistent with the rest of + the language. + - Intl: . The extension now requires at least ICU 57.1. diff --git a/ext/fileinfo/fileinfo.c b/ext/fileinfo/fileinfo.c index 4311afff1802d..d3680c5755f25 100644 --- a/ext/fileinfo/fileinfo.c +++ b/ext/fileinfo/fileinfo.c @@ -277,57 +277,73 @@ PHP_FUNCTION(finfo_set_flags) } /* }}} */ -#define FILEINFO_MODE_BUFFER 0 -#define FILEINFO_MODE_STREAM 1 -#define FILEINFO_MODE_FILE 2 - -static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mimetype_emu) /* {{{ */ +static const char* php_fileinfo_from_path(struct magic_set *magic, const zend_string *path, php_stream_context *context) { - zend_long options = 0; - char *ret_val = NULL, *buffer = NULL; - size_t buffer_len; - php_fileinfo *finfo = NULL; - zval *zcontext = NULL; - zval *what; - char mime_directory[] = "directory"; - struct magic_set *magic = NULL; + ZEND_ASSERT(magic != NULL); + ZEND_ASSERT(path); + ZEND_ASSERT(ZSTR_LEN(path) != 0); + ZEND_ASSERT(!zend_str_has_nul_byte(path)); + ZEND_ASSERT(context != NULL); + + /* determine if the file is a local file or remote URL */ + const char *dummy; + php_stream_statbuf ssb; + + const php_stream_wrapper *wrap = php_stream_locate_url_wrapper(ZSTR_VAL(path), &dummy, 0); + if (UNEXPECTED(wrap == NULL)) { + return NULL; + } - if (mimetype_emu) { +#ifdef PHP_WIN32 + if (php_stream_stat_path_ex(ZSTR_VAL(path), 0, &ssb, context) == SUCCESS) { + if (ssb.sb.st_mode & S_IFDIR) { + return "directory"; + } + } +#endif - /* mime_content_type(..) emulation */ - if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &what) == FAILURE) { - RETURN_THROWS(); + php_stream *stream = php_stream_open_wrapper_ex(ZSTR_VAL(path), "rb", REPORT_ERRORS, NULL, context); + if (!stream) { + return NULL; + } + + const char *ret_val = NULL; + if (php_stream_stat(stream, &ssb) == SUCCESS) { + if (ssb.sb.st_mode & S_IFDIR) { + ret_val = "directory"; + } else { + ret_val = magic_stream(magic, stream); + if (UNEXPECTED(ret_val == NULL)) { + php_error_docref(NULL, E_WARNING, "Failed identify data %d:%s", magic_errno(magic), magic_error(magic)); + } } + } - switch (Z_TYPE_P(what)) { - case IS_STRING: - buffer = Z_STRVAL_P(what); - buffer_len = Z_STRLEN_P(what); - mode = FILEINFO_MODE_FILE; - break; + php_stream_close(stream); - case IS_RESOURCE: - mode = FILEINFO_MODE_STREAM; - break; + return ret_val; +} - default: - zend_argument_type_error(1, "must be of type resource|string, %s given", zend_zval_value_name(what)); - RETURN_THROWS(); - } +/* Return information about a file. */ +PHP_FUNCTION(finfo_file) +{ + zval *self; + zend_string *path = NULL; + zend_long options = 0; + zval *zcontext = NULL; + php_fileinfo *finfo = NULL; - magic = magic_open(MAGIC_MIME_TYPE); - if (magic_load(magic, NULL) == -1) { - php_error_docref(NULL, E_WARNING, "Failed to load magic database"); - goto common; - } - } else { - zval *self; - if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os|lr!", &self, finfo_class_entry, &buffer, &buffer_len, &options, &zcontext) == FAILURE) { - RETURN_THROWS(); - } - FILEINFO_FROM_OBJECT(finfo, self); - magic = finfo->magic; + if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OP|lr!", &self, finfo_class_entry, &path, &options, &zcontext) == FAILURE) { + RETURN_THROWS(); + } + FILEINFO_FROM_OBJECT(finfo, self); + struct magic_set *magic = finfo->magic; + + if (UNEXPECTED(ZSTR_LEN(path) == 0)) { + zend_argument_must_not_be_empty_error(2); + RETURN_THROWS(); } + php_stream_context *context = php_stream_context_from_zval(zcontext, false); /* Set options for the current file/buffer. */ if (options) { @@ -336,124 +352,121 @@ static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mime magic_setflags(magic, options); } - switch (mode) { - case FILEINFO_MODE_BUFFER: - { - ret_val = (char *) magic_buffer(magic, buffer, buffer_len); - break; - } - - case FILEINFO_MODE_STREAM: - { - php_stream *stream; - zend_off_t streampos; - - php_stream_from_zval_no_verify(stream, what); - if (!stream) { - goto common; - } - - streampos = php_stream_tell(stream); /* remember stream position for restoration */ - php_stream_seek(stream, 0, SEEK_SET); + const char *ret_val = php_fileinfo_from_path(magic, path, context); + /* Restore options */ + if (options) { + magic_setflags(magic, finfo->options); + } - ret_val = (char *) magic_stream(magic, stream); + if (UNEXPECTED(ret_val == NULL)) { + RETURN_FALSE; + } else { + RETURN_STRING(ret_val); + } +} - php_stream_seek(stream, streampos, SEEK_SET); - break; - } +/* Return information about a string buffer. */ +PHP_FUNCTION(finfo_buffer) +{ + zval *self; + zend_string *buffer = NULL; + zend_long options = 0; + zval *dummy_context = NULL; + php_fileinfo *finfo = NULL; - case FILEINFO_MODE_FILE: - { - /* determine if the file is a local file or remote URL */ - const char *tmp2; - php_stream_wrapper *wrap; - php_stream_statbuf ssb; - - // Implementation is used for both finfo_file() and mimetype_emu() - int buffer_param_num = (mimetype_emu ? 1 : 2); - if (buffer == NULL || buffer_len == 0) { - zend_argument_must_not_be_empty_error(buffer_param_num); - goto clean; - } - if (CHECK_NULL_PATH(buffer, buffer_len)) { - zend_argument_type_error(buffer_param_num, "must not contain any null bytes"); - goto clean; - } + if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OS|lr!", &self, finfo_class_entry, &buffer, &options, &dummy_context) == FAILURE) { + RETURN_THROWS(); + } + FILEINFO_FROM_OBJECT(finfo, self); + struct magic_set *magic = finfo->magic; - wrap = php_stream_locate_url_wrapper(buffer, &tmp2, 0); + /* Set options for the current file/buffer. */ + if (options) { + magic_setflags(magic, options); + } - if (wrap) { - php_stream *stream; - php_stream_context *context = php_stream_context_from_zval(zcontext, 0); + const char *ret_val = magic_buffer(magic, ZSTR_VAL(buffer), ZSTR_LEN(buffer)); -#ifdef PHP_WIN32 - if (php_stream_stat_path_ex(buffer, 0, &ssb, context) == SUCCESS) { - if (ssb.sb.st_mode & S_IFDIR) { - ret_val = mime_directory; - goto common; - } - } -#endif + /* Restore options */ + if (options) { + magic_setflags(magic, finfo->options); + } - stream = php_stream_open_wrapper_ex(buffer, "rb", REPORT_ERRORS, NULL, context); + if (UNEXPECTED(ret_val == NULL)) { + php_error_docref(NULL, E_WARNING, "Failed identify data %d:%s", magic_errno(magic), magic_error(magic)); + RETURN_FALSE; + } else { + RETURN_STRING(ret_val); + } +} - if (!stream) { - RETVAL_FALSE; - goto clean; - } +/* Return content-type for file */ +PHP_FUNCTION(mime_content_type) +{ + zval *path_or_stream; + const zend_string *path = NULL; + php_stream *stream = NULL; + struct magic_set *magic = NULL; - if (php_stream_stat(stream, &ssb) == SUCCESS) { - if (ssb.sb.st_mode & S_IFDIR) { - ret_val = mime_directory; - } else { - ret_val = (char *)magic_stream(magic, stream); - } - } + if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &path_or_stream) == FAILURE) { + RETURN_THROWS(); + } - php_stream_close(stream); + switch (Z_TYPE_P(path_or_stream)) { + case IS_STRING: + path = Z_STR_P(path_or_stream); + if (UNEXPECTED(ZSTR_LEN(path) == 0)) { + zend_argument_must_not_be_empty_error(1); + RETURN_THROWS(); + } + if (UNEXPECTED(zend_str_has_nul_byte(path))) { + zend_argument_type_error(1, "must not contain any null bytes"); + RETURN_THROWS(); } break; - } - EMPTY_SWITCH_DEFAULT_CASE() + + case IS_RESOURCE: + php_stream_from_zval(stream, path_or_stream); + break; + + default: + zend_argument_type_error(1, "must be of type resource|string, %s given", zend_zval_value_name(path_or_stream)); + RETURN_THROWS(); } -common: - if (ret_val) { - RETVAL_STRING(ret_val); - } else { - php_error_docref(NULL, E_WARNING, "Failed identify data %d:%s", magic_errno(magic), magic_error(magic)); - RETVAL_FALSE; + magic = magic_open(MAGIC_MIME_TYPE); + if (UNEXPECTED(magic == NULL)) { + php_error_docref(NULL, E_WARNING, "Failed to load magic database"); + RETURN_FALSE; } -clean: - if (mimetype_emu) { + if (UNEXPECTED(magic_load(magic, NULL) == -1)) { + php_error_docref(NULL, E_WARNING, "Failed identify data %d:%s", magic_errno(magic), magic_error(magic)); magic_close(magic); + RETURN_FALSE; } - /* Restore options */ - if (options) { - magic_setflags(magic, finfo->options); - } -} -/* }}} */ + const char *ret_val; + if (path) { + php_stream_context *context = php_stream_context_get_default(false); + ret_val = php_fileinfo_from_path(magic, path, context); + } else { + /* remember stream position for restoration */ + zend_off_t current_stream_pos = php_stream_tell(stream); + php_stream_seek(stream, 0, SEEK_SET); -/* {{{ Return information about a file. */ -PHP_FUNCTION(finfo_file) -{ - _php_finfo_get_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, FILEINFO_MODE_FILE, 0); -} -/* }}} */ + ret_val = magic_stream(magic, stream); + if (UNEXPECTED(ret_val == NULL)) { + php_error_docref(NULL, E_WARNING, "Failed identify data %d:%s", magic_errno(magic), magic_error(magic)); + } -/* {{{ Return information about a string buffer. */ -PHP_FUNCTION(finfo_buffer) -{ - _php_finfo_get_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, FILEINFO_MODE_BUFFER, 0); -} -/* }}} */ + php_stream_seek(stream, current_stream_pos, SEEK_SET); + } -/* {{{ Return content-type for file */ -PHP_FUNCTION(mime_content_type) -{ - _php_finfo_get_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, -1, 1); + if (UNEXPECTED(ret_val == NULL)) { + RETVAL_FALSE; + } else { + RETVAL_STRING(ret_val); + } + magic_close(magic); } -/* }}} */ diff --git a/ext/fileinfo/tests/finfo_file_001.phpt b/ext/fileinfo/tests/finfo_file_001.phpt index 1e31b8ad1c049..737b8db53ff77 100644 --- a/ext/fileinfo/tests/finfo_file_001.phpt +++ b/ext/fileinfo/tests/finfo_file_001.phpt @@ -8,7 +8,7 @@ fileinfo $fp = finfo_open(); try { var_dump(finfo_file($fp, "\0")); -} catch (\TypeError $e) { +} catch (\ValueError $e) { echo $e->getMessage() . \PHP_EOL; } try { diff --git a/ext/fileinfo/tests/finfo_file_basic.phpt b/ext/fileinfo/tests/finfo_file_basic.phpt index e6f2b1e8570d5..4545a3063a7fe 100644 --- a/ext/fileinfo/tests/finfo_file_basic.phpt +++ b/ext/fileinfo/tests/finfo_file_basic.phpt @@ -15,7 +15,7 @@ var_dump( finfo_file( $finfo, __FILE__, FILEINFO_CONTINUE ) ); var_dump( finfo_file( $finfo, $magicFile ) ); try { var_dump( finfo_file( $finfo, $magicFile.chr(0).$magicFile) ); -} catch (\TypeError $e) { +} catch (\ValueError $e) { echo $e->getMessage() . \PHP_EOL; } From 7fb8db014e4992b97d2c215255cbe23421ff8b73 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Thu, 3 Apr 2025 19:19:36 +0100 Subject: [PATCH 03/14] ext/ftp: Voidify ftp_close() --- ext/ftp/ftp.c | 6 ++---- ext/ftp/ftp.h | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/ext/ftp/ftp.c b/ext/ftp/ftp.c index 3dc401a4bffbb..970b90e9a484c 100644 --- a/ext/ftp/ftp.c +++ b/ext/ftp/ftp.c @@ -161,11 +161,10 @@ ftp_open(const char *host, short port, zend_long timeout_sec) /* }}} */ /* {{{ ftp_close */ -ftpbuf_t* -ftp_close(ftpbuf_t *ftp) +void ftp_close(ftpbuf_t *ftp) { if (ftp == NULL) { - return NULL; + return; } #ifdef HAVE_FTP_SSL if (ftp->last_ssl_session) { @@ -186,7 +185,6 @@ ftp_close(ftpbuf_t *ftp) } ftp_gc(ftp); efree(ftp); - return NULL; } /* }}} */ diff --git a/ext/ftp/ftp.h b/ext/ftp/ftp.h index 94abc588ca857..7b51734579de5 100644 --- a/ext/ftp/ftp.h +++ b/ext/ftp/ftp.h @@ -103,7 +103,7 @@ int ftp_quit(ftpbuf_t *ftp); void ftp_gc(ftpbuf_t *ftp); /* close the FTP connection and return NULL */ -ftpbuf_t* ftp_close(ftpbuf_t *ftp); +void ftp_close(ftpbuf_t *ftp); /* logs into the FTP server, returns true on success, false on error */ int ftp_login(ftpbuf_t *ftp, const char *user, const size_t user_len, const char *pass, const size_t pass_len); From 51fa97fb442c6ab40630390b91bec054a20fa6a7 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Thu, 3 Apr 2025 19:30:45 +0100 Subject: [PATCH 04/14] ext/ftp: Normalize coding style --- ext/ftp/ftp.c | 284 +++++++++++++------------------------------------- ext/ftp/ftp.h | 63 ++++++----- 2 files changed, 101 insertions(+), 246 deletions(-) diff --git a/ext/ftp/ftp.c b/ext/ftp/ftp.c index 970b90e9a484c..60ef58347163e 100644 --- a/ext/ftp/ftp.c +++ b/ext/ftp/ftp.c @@ -68,41 +68,37 @@ * it sends the string "cmd args\r\n" if args is non-null, or * "cmd\r\n" if args is null */ -static int ftp_putcmd( ftpbuf_t *ftp, - const char *cmd, - const size_t cmd_len, - const char *args, - const size_t args_len); +static int ftp_putcmd(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, const char *args, const size_t args_len); /* wrapper around send/recv to handle timeouts */ -static int my_send(ftpbuf_t *ftp, php_socket_t s, void *buf, size_t len); -static int my_recv(ftpbuf_t *ftp, php_socket_t s, void *buf, size_t len); -static int my_accept(ftpbuf_t *ftp, php_socket_t s, struct sockaddr *addr, socklen_t *addrlen); +static int my_send(ftpbuf_t *ftp, php_socket_t s, void *buf, size_t len); +static int my_recv(ftpbuf_t *ftp, php_socket_t s, void *buf, size_t len); +static int my_accept(ftpbuf_t *ftp, php_socket_t s, struct sockaddr *addr, socklen_t *addrlen); /* reads a line the socket , returns true on success, false on error */ -static int ftp_readline(ftpbuf_t *ftp); +static int ftp_readline(ftpbuf_t *ftp); /* reads an ftp response, returns true on success, false on error */ -static int ftp_getresp(ftpbuf_t *ftp); +static int ftp_getresp(ftpbuf_t *ftp); /* sets the ftp transfer type */ -static int ftp_type(ftpbuf_t *ftp, ftptype_t type); +static int ftp_type(ftpbuf_t *ftp, ftptype_t type); /* opens up a data stream */ -static databuf_t* ftp_getdata(ftpbuf_t *ftp); +static databuf_t* ftp_getdata(ftpbuf_t *ftp); /* 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, no-op if already closed */ -static void data_close(ftpbuf_t *ftp); +static void data_close(ftpbuf_t *ftp); /* 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); #ifdef HAVE_FTP_SSL /* shuts down a TLS/SSL connection */ -static void ftp_ssl_shutdown(ftpbuf_t *ftp, php_socket_t fd, SSL *ssl_handle); +static void ftp_ssl_shutdown(ftpbuf_t *ftp, php_socket_t fd, SSL *ssl_handle); #endif /* IP and port conversion box */ @@ -112,9 +108,7 @@ union ipbox { unsigned char c[8]; }; -/* {{{ ftp_open */ -ftpbuf_t* -ftp_open(const char *host, short port, zend_long timeout_sec) +ftpbuf_t* ftp_open(const char *host, short port, zend_long timeout_sec) { ftpbuf_t *ftp; socklen_t size; @@ -158,9 +152,7 @@ ftp_open(const char *host, short port, zend_long timeout_sec) efree(ftp); return NULL; } -/* }}} */ -/* {{{ ftp_close */ void ftp_close(ftpbuf_t *ftp) { if (ftp == NULL) { @@ -186,11 +178,8 @@ void ftp_close(ftpbuf_t *ftp) ftp_gc(ftp); efree(ftp); } -/* }}} */ -/* {{{ ftp_gc */ -void -ftp_gc(ftpbuf_t *ftp) +void ftp_gc(ftpbuf_t *ftp) { if (ftp == NULL) { return; @@ -204,11 +193,8 @@ ftp_gc(ftpbuf_t *ftp) ftp->syst = NULL; } } -/* }}} */ -/* {{{ ftp_quit */ -int -ftp_quit(ftpbuf_t *ftp) +int ftp_quit(ftpbuf_t *ftp) { if (ftp == NULL) { return 0; @@ -228,7 +214,6 @@ ftp_quit(ftpbuf_t *ftp) return 1; } -/* }}} */ #ifdef HAVE_FTP_SSL static int ftp_ssl_new_session_cb(SSL *ssl, SSL_SESSION *sess) @@ -246,9 +231,7 @@ static int ftp_ssl_new_session_cb(SSL *ssl, SSL_SESSION *sess) } #endif -/* {{{ ftp_login */ -int -ftp_login(ftpbuf_t *ftp, const char *user, const size_t user_len, const char *pass, const size_t pass_len) +int ftp_login(ftpbuf_t *ftp, const char *user, const size_t user_len, const char *pass, const size_t pass_len) { #ifdef HAVE_FTP_SSL SSL_CTX *ctx = NULL; @@ -393,11 +376,8 @@ ftp_login(ftpbuf_t *ftp, const char *user, const size_t user_len, const char *pa } return (ftp->resp == 230); } -/* }}} */ -/* {{{ ftp_reinit */ -int -ftp_reinit(ftpbuf_t *ftp) +int ftp_reinit(ftpbuf_t *ftp) { if (ftp == NULL) { return 0; @@ -416,11 +396,8 @@ ftp_reinit(ftpbuf_t *ftp) return 1; } -/* }}} */ -/* {{{ ftp_syst */ -const char* -ftp_syst(ftpbuf_t *ftp) +const char* ftp_syst(ftpbuf_t *ftp) { char *syst, *end; @@ -451,11 +428,8 @@ ftp_syst(ftpbuf_t *ftp) } return ftp->syst; } -/* }}} */ -/* {{{ ftp_pwd */ -const char* -ftp_pwd(ftpbuf_t *ftp) +const char* ftp_pwd(ftpbuf_t *ftp) { char *pwd, *end; @@ -484,11 +458,8 @@ ftp_pwd(ftpbuf_t *ftp) return ftp->pwd; } -/* }}} */ -/* {{{ ftp_exec */ -int -ftp_exec(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len) +int ftp_exec(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len) { if (ftp == NULL) { return 0; @@ -502,11 +473,8 @@ ftp_exec(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len) return 1; } -/* }}} */ -/* {{{ ftp_raw */ -void -ftp_raw(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, zval *return_value) +void ftp_raw(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, zval *return_value) { if (ftp == NULL || cmd == NULL) { RETURN_NULL(); @@ -522,11 +490,8 @@ ftp_raw(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, zval *return_value } } } -/* }}} */ -/* {{{ ftp_chdir */ -int -ftp_chdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len) +int ftp_chdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len) { if (ftp == NULL) { return 0; @@ -545,11 +510,8 @@ ftp_chdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len) } return 1; } -/* }}} */ -/* {{{ ftp_cdup */ -int -ftp_cdup(ftpbuf_t *ftp) +int ftp_cdup(ftpbuf_t *ftp) { if (ftp == NULL) { return 0; @@ -568,11 +530,8 @@ ftp_cdup(ftpbuf_t *ftp) } return 1; } -/* }}} */ -/* {{{ ftp_mkdir */ -zend_string* -ftp_mkdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len) +zend_string* ftp_mkdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len) { char *mkd, *end; zend_string *ret; @@ -599,11 +558,8 @@ ftp_mkdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len) return ret; } -/* }}} */ -/* {{{ ftp_rmdir */ -int -ftp_rmdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len) +int ftp_rmdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len) { if (ftp == NULL) { return 0; @@ -616,11 +572,8 @@ ftp_rmdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len) } return 1; } -/* }}} */ -/* {{{ ftp_chmod */ -int -ftp_chmod(ftpbuf_t *ftp, const int mode, const char *filename, const int filename_len) +int ftp_chmod(ftpbuf_t *ftp, const int mode, const char *filename, const int filename_len) { char *buffer; size_t buffer_len; @@ -648,11 +601,8 @@ ftp_chmod(ftpbuf_t *ftp, const int mode, const char *filename, const int filenam return 1; } -/* }}} */ -/* {{{ ftp_alloc */ -int -ftp_alloc(ftpbuf_t *ftp, const zend_long size, zend_string **response) +int ftp_alloc(ftpbuf_t *ftp, const zend_long size, zend_string **response) { char buffer[64]; int buffer_len; @@ -685,36 +635,24 @@ ftp_alloc(ftpbuf_t *ftp, const zend_long size, zend_string **response) return 1; } -/* }}} */ -/* {{{ ftp_nlist */ -char** -ftp_nlist(ftpbuf_t *ftp, const char *path, const size_t path_len) +char** ftp_nlist(ftpbuf_t *ftp, const char *path, const size_t path_len) { return ftp_genlist(ftp, "NLST", sizeof("NLST")-1, path, path_len); } -/* }}} */ -/* {{{ ftp_list */ -char** -ftp_list(ftpbuf_t *ftp, const char *path, const size_t path_len, int recursive) +char** ftp_list(ftpbuf_t *ftp, const char *path, const size_t path_len, int recursive) { return ftp_genlist(ftp, ((recursive) ? "LIST -R" : "LIST"), ((recursive) ? sizeof("LIST -R")-1 : sizeof("LIST")-1), path, path_len); } -/* }}} */ -/* {{{ ftp_mlsd */ -char** -ftp_mlsd(ftpbuf_t *ftp, const char *path, const size_t path_len) +char** ftp_mlsd(ftpbuf_t *ftp, const char *path, const size_t path_len) { return ftp_genlist(ftp, "MLSD", sizeof("MLSD")-1, path, path_len); } -/* }}} */ - -/* {{{ ftp_mlsd_parse_line */ -int -ftp_mlsd_parse_line(HashTable *ht, const char *input) { +int ftp_mlsd_parse_line(HashTable *ht, const char *input) +{ zval zstr; const char *end = input + strlen(input); @@ -753,11 +691,8 @@ ftp_mlsd_parse_line(HashTable *ht, const char *input) { return SUCCESS; } -/* }}} */ -/* {{{ ftp_type */ -int -ftp_type(ftpbuf_t *ftp, ftptype_t type) +int ftp_type(ftpbuf_t *ftp, ftptype_t type) { const char *typechar; @@ -784,11 +719,8 @@ ftp_type(ftpbuf_t *ftp, ftptype_t type) return 1; } -/* }}} */ -/* {{{ ftp_pasv */ -int -ftp_pasv(ftpbuf_t *ftp, int pasv) +int ftp_pasv(ftpbuf_t *ftp, int pasv) { char *ptr; union ipbox ipbox; @@ -877,11 +809,8 @@ ftp_pasv(ftpbuf_t *ftp, int pasv) return 1; } -/* }}} */ -/* {{{ ftp_get */ -int -ftp_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t path_len, ftptype_t type, zend_long resumepos) +int ftp_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t path_len, ftptype_t type, zend_long resumepos) { databuf_t *data = NULL; size_t rcvd; @@ -970,7 +899,6 @@ ftp_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t pat data_close(ftp); return 0; } -/* }}} */ static zend_result ftp_send_stream_to_data_socket(ftpbuf_t *ftp, databuf_t *data, php_stream *instream, ftptype_t type, bool send_once_and_return) { @@ -1045,9 +973,7 @@ static zend_result ftp_send_stream_to_data_socket(ftpbuf_t *ftp, databuf_t *data return SUCCESS; } -/* {{{ ftp_put */ -int -ftp_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type, zend_long startpos) +int ftp_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type, zend_long startpos) { databuf_t *data = NULL; char arg[MAX_LENGTH_OF_LONG]; @@ -1100,14 +1026,10 @@ ftp_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *inst data_close(ftp); return 0; } -/* }}} */ - -/* {{{ ftp_append */ -int -ftp_append(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type) +int ftp_append(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type) { - databuf_t *data = NULL; + databuf_t *data = NULL; if (ftp == NULL) { return 0; @@ -1144,11 +1066,8 @@ ftp_append(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *i data_close(ftp); return 0; } -/* }}} */ -/* {{{ ftp_size */ -zend_long -ftp_size(ftpbuf_t *ftp, const char *path, const size_t path_len) +zend_long ftp_size(ftpbuf_t *ftp, const char *path, const size_t path_len) { if (ftp == NULL) { return -1; @@ -1164,11 +1083,8 @@ ftp_size(ftpbuf_t *ftp, const char *path, const size_t path_len) } return ZEND_ATOL(ftp->inbuf); } -/* }}} */ -/* {{{ ftp_mdtm */ -time_t -ftp_mdtm(ftpbuf_t *ftp, const char *path, const size_t path_len) +time_t ftp_mdtm(ftpbuf_t *ftp, const char *path, const size_t path_len) { time_t stamp; struct tm *gmt, tmbuf; @@ -1211,11 +1127,8 @@ ftp_mdtm(ftpbuf_t *ftp, const char *path, const size_t path_len) return stamp; } -/* }}} */ -/* {{{ ftp_delete */ -int -ftp_delete(ftpbuf_t *ftp, const char *path, const size_t path_len) +int ftp_delete(ftpbuf_t *ftp, const char *path, const size_t path_len) { if (ftp == NULL) { return 0; @@ -1229,11 +1142,8 @@ ftp_delete(ftpbuf_t *ftp, const char *path, const size_t path_len) return 1; } -/* }}} */ -/* {{{ ftp_rename */ -int -ftp_rename(ftpbuf_t *ftp, const char *src, const size_t src_len, const char *dest, const size_t dest_len) +int ftp_rename(ftpbuf_t *ftp, const char *src, const size_t src_len, const char *dest, const size_t dest_len) { if (ftp == NULL) { return 0; @@ -1252,11 +1162,8 @@ ftp_rename(ftpbuf_t *ftp, const char *src, const size_t src_len, const char *des } return 1; } -/* }}} */ -/* {{{ ftp_site */ -int -ftp_site(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len) +int ftp_site(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len) { if (ftp == NULL) { return 0; @@ -1270,16 +1177,12 @@ ftp_site(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len) return 1; } -/* }}} */ /* static functions */ - -/* {{{ ftp_putcmd */ -int -ftp_putcmd(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, const char *args, const size_t args_len) +int ftp_putcmd(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, const char *args, const size_t args_len) { - int size; - char *data; + int size; + char *data; if (strpbrk(cmd, "\r\n")) { return 0; @@ -1313,14 +1216,11 @@ ftp_putcmd(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, const char *arg } return 1; } -/* }}} */ -/* {{{ ftp_readline */ -int -ftp_readline(ftpbuf_t *ftp) +int ftp_readline(ftpbuf_t *ftp) { - long size, rcvd; - char *data, *eol; + long size, rcvd; + char *data, *eol; /* shift the extra to the front */ size = FTP_BUFSIZE; @@ -1366,11 +1266,8 @@ ftp_readline(ftpbuf_t *ftp) *data = 0; return 0; } -/* }}} */ -/* {{{ ftp_getresp */ -int -ftp_getresp(ftpbuf_t *ftp) +int ftp_getresp(ftpbuf_t *ftp) { if (ftp == NULL) { return 0; @@ -1403,7 +1300,6 @@ ftp_getresp(ftpbuf_t *ftp) } return 1; } -/* }}} */ static ssize_t my_send_wrapper_with_restart(php_socket_t fd, const void *buf, size_t size, int flags) { ssize_t n; @@ -1504,9 +1400,7 @@ static int my_poll(php_socket_t fd, int events, int timeout) { return n; } -/* {{{ my_send */ -int -my_send(ftpbuf_t *ftp, php_socket_t s, void *buf, size_t len) +int my_send(ftpbuf_t *ftp, php_socket_t s, void *buf, size_t len) { zend_long size, sent; int n; @@ -1539,13 +1433,10 @@ my_send(ftpbuf_t *ftp, php_socket_t s, void *buf, size_t len) return len; } -/* }}} */ -/* {{{ my_recv */ -int -my_recv(ftpbuf_t *ftp, php_socket_t s, void *buf, size_t len) +int my_recv(ftpbuf_t *ftp, php_socket_t s, void *buf, size_t len) { - int n, nr_bytes; + int n, nr_bytes; #ifdef HAVE_FTP_SSL int err; bool retry = 0; @@ -1618,13 +1509,10 @@ my_recv(ftpbuf_t *ftp, php_socket_t s, void *buf, size_t len) #endif return (nr_bytes); } -/* }}} */ -/* {{{ data_available */ -int -data_available(ftpbuf_t *ftp, php_socket_t s) +int data_available(ftpbuf_t *ftp, php_socket_t s) { - int n; + int n; n = my_poll(s, PHP_POLLREADABLE, 1000); if (n < 1) { @@ -1642,12 +1530,10 @@ data_available(ftpbuf_t *ftp, php_socket_t s) return 1; } -/* }}} */ -/* {{{ data_writeable */ -int -data_writeable(ftpbuf_t *ftp, php_socket_t s) + +int data_writeable(ftpbuf_t *ftp, php_socket_t s) { - int n; + int n; n = my_poll(s, POLLOUT, 1000); if (n < 1) { @@ -1665,13 +1551,10 @@ data_writeable(ftpbuf_t *ftp, php_socket_t s) return 1; } -/* }}} */ -/* {{{ my_accept */ -int -my_accept(ftpbuf_t *ftp, php_socket_t s, struct sockaddr *addr, socklen_t *addrlen) +int my_accept(ftpbuf_t *ftp, php_socket_t s, struct sockaddr *addr, socklen_t *addrlen) { - int n; + int n; n = my_poll(s, PHP_POLLREADABLE, ftp->timeout_sec * 1000); if (n < 1) { @@ -1689,11 +1572,8 @@ my_accept(ftpbuf_t *ftp, php_socket_t s, struct sockaddr *addr, socklen_t *addrl return accept(s, addr, addrlen); } -/* }}} */ -/* {{{ ftp_getdata */ -databuf_t* -ftp_getdata(ftpbuf_t *ftp) +databuf_t* ftp_getdata(ftpbuf_t *ftp) { int fd = -1; databuf_t *data; @@ -1822,14 +1702,11 @@ ftp_getdata(ftpbuf_t *ftp) efree(data); return NULL; } -/* }}} */ -/* {{{ data_accept */ -databuf_t* -data_accept(databuf_t *data, ftpbuf_t *ftp) +databuf_t* data_accept(databuf_t *data, ftpbuf_t *ftp) { php_sockaddr_storage addr; - socklen_t size; + socklen_t size; #ifdef HAVE_FTP_SSL SSL_CTX *ctx; @@ -1935,9 +1812,7 @@ data_accept(databuf_t *data, ftpbuf_t *ftp) return data; } -/* }}} */ -/* {{{ ftp_ssl_shutdown */ #ifdef HAVE_FTP_SSL static void ftp_ssl_shutdown(ftpbuf_t *ftp, php_socket_t fd, SSL *ssl_handle) { /* In TLS 1.3 it's common to receive session tickets after the handshake has completed. We need to train @@ -1998,9 +1873,7 @@ static void ftp_ssl_shutdown(ftpbuf_t *ftp, php_socket_t fd, SSL *ssl_handle) { (void)SSL_free(ssl_handle); } #endif -/* }}} */ -/* {{{ data_close */ void data_close(ftpbuf_t *ftp) { ZEND_ASSERT(ftp != NULL); @@ -2031,11 +1904,8 @@ void data_close(ftpbuf_t *ftp) ftp->data = NULL; efree(data); } -/* }}} */ -/* {{{ ftp_genlist */ -char** -ftp_genlist(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, const char *path, const size_t path_len) +char** ftp_genlist(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, const char *path, const size_t path_len) { php_stream *tmpstream = NULL; databuf_t *data = NULL; @@ -2135,14 +2005,11 @@ ftp_genlist(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, const char *pa efree(ret); return NULL; } -/* }}} */ -/* {{{ ftp_nb_get */ -int -ftp_nb_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t path_len, ftptype_t type, zend_long resumepos) +int ftp_nb_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t path_len, ftptype_t type, zend_long resumepos) { - databuf_t *data = NULL; - char arg[MAX_LENGTH_OF_LONG]; + databuf_t *data = NULL; + char arg[MAX_LENGTH_OF_LONG]; if (ftp == NULL) { return PHP_FTP_FAILED; @@ -2201,11 +2068,8 @@ ftp_nb_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t data_close(ftp); return PHP_FTP_FAILED; } -/* }}} */ -/* {{{ ftp_nb_continue_read */ -int -ftp_nb_continue_read(ftpbuf_t *ftp) +int ftp_nb_continue_read(ftpbuf_t *ftp) { databuf_t *data = NULL; char *ptr; @@ -2263,11 +2127,8 @@ ftp_nb_continue_read(ftpbuf_t *ftp) data_close(ftp); return PHP_FTP_FAILED; } -/* }}} */ -/* {{{ ftp_nb_put */ -int -ftp_nb_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type, zend_long startpos) +int ftp_nb_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type, zend_long startpos) { databuf_t *data = NULL; char arg[MAX_LENGTH_OF_LONG]; @@ -2315,12 +2176,8 @@ ftp_nb_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *i data_close(ftp); return PHP_FTP_FAILED; } -/* }}} */ - -/* {{{ ftp_nb_continue_write */ -int -ftp_nb_continue_write(ftpbuf_t *ftp) +int ftp_nb_continue_write(ftpbuf_t *ftp) { /* check if we can write more data */ if (!data_writeable(ftp, ftp->data->fd)) { @@ -2347,4 +2204,3 @@ ftp_nb_continue_write(ftpbuf_t *ftp) ftp->nb = 0; return PHP_FTP_FAILED; } -/* }}} */ diff --git a/ext/ftp/ftp.h b/ext/ftp/ftp.h index 7b51734579de5..bcd69059cbc86 100644 --- a/ext/ftp/ftp.h +++ b/ext/ftp/ftp.h @@ -92,42 +92,42 @@ typedef struct ftpbuf /* open a FTP connection, returns ftpbuf (NULL on error) * port is the ftp port in network byte order, or 0 for the default */ -ftpbuf_t* ftp_open(const char *host, short port, zend_long timeout_sec); +ftpbuf_t* ftp_open(const char *host, short port, zend_long timeout_sec); /* quits from the ftp session (it still needs to be closed) * return true on success, false on error */ -int ftp_quit(ftpbuf_t *ftp); +int ftp_quit(ftpbuf_t *ftp); /* frees up any cached data held in the ftp buffer */ -void ftp_gc(ftpbuf_t *ftp); +void ftp_gc(ftpbuf_t *ftp); /* close the FTP connection and return NULL */ void ftp_close(ftpbuf_t *ftp); /* logs into the FTP server, returns true on success, false on error */ -int ftp_login(ftpbuf_t *ftp, const char *user, const size_t user_len, const char *pass, const size_t pass_len); +int ftp_login(ftpbuf_t *ftp, const char *user, const size_t user_len, const char *pass, const size_t pass_len); /* reinitializes the connection, returns true on success, false on error */ -int ftp_reinit(ftpbuf_t *ftp); +int ftp_reinit(ftpbuf_t *ftp); /* returns the remote system type (NULL on error) */ -const char* ftp_syst(ftpbuf_t *ftp); +const char* ftp_syst(ftpbuf_t *ftp); /* returns the present working directory (NULL on error) */ -const char* ftp_pwd(ftpbuf_t *ftp); +const char* ftp_pwd(ftpbuf_t *ftp); /* exec a command [special features], return true on success, false on error */ -int ftp_exec(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len); +int ftp_exec(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len); /* send a raw ftp command, return response as a hashtable, NULL on error */ -void ftp_raw(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, zval *return_value); +void ftp_raw(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, zval *return_value); /* changes directories, return true on success, false on error */ -int ftp_chdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len); +int ftp_chdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len); /* changes to parent directory, return true on success, false on error */ -int ftp_cdup(ftpbuf_t *ftp); +int ftp_cdup(ftpbuf_t *ftp); /* creates a directory, return the directory name on success, NULL on error. * the return value must be freed @@ -135,95 +135,94 @@ int ftp_cdup(ftpbuf_t *ftp); zend_string* ftp_mkdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len); /* removes a directory, return true on success, false on error */ -int ftp_rmdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len); +int ftp_rmdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len); /* Set permissions on a file */ -int ftp_chmod(ftpbuf_t *ftp, const int mode, const char *filename, const int filename_len); +int ftp_chmod(ftpbuf_t *ftp, const int mode, const char *filename, const int filename_len); /* Allocate space on remote server with ALLO command * Many servers will respond with 202 Allocation not necessary, * however some servers will not accept STOR or APPE until ALLO is confirmed. * If response is passed, it is estrdup()ed from ftp->inbuf and must be freed * or assigned to a zval returned to the user */ -int ftp_alloc(ftpbuf_t *ftp, const zend_long size, zend_string **response); +int ftp_alloc(ftpbuf_t *ftp, const zend_long size, zend_string **response); /* returns a NULL-terminated array of filenames in the given path * or NULL on error. the return array must be freed (but don't * free the array elements) */ -char** ftp_nlist(ftpbuf_t *ftp, const char *path, const size_t path_len); +char** ftp_nlist(ftpbuf_t *ftp, const char *path, const size_t path_len); /* returns a NULL-terminated array of lines returned by the ftp * LIST command for the given path or NULL on error. the return * array must be freed (but don't * free the array elements) */ -char** ftp_list(ftpbuf_t *ftp, const char *path, const size_t path_len, int recursive); +char** ftp_list(ftpbuf_t *ftp, const char *path, const size_t path_len, int recursive); /* populates a hashtable with the facts contained in one line of * an MLSD response. */ -int ftp_mlsd_parse_line(HashTable *ht, const char *input); +int ftp_mlsd_parse_line(HashTable *ht, const char *input); /* returns a NULL-terminated array of lines returned by the ftp * MLSD command for the given path or NULL on error. the return * array must be freed (but don't * free the array elements) */ -char** ftp_mlsd(ftpbuf_t *ftp, const char *path, const size_t path_len); +char** ftp_mlsd(ftpbuf_t *ftp, const char *path, const size_t path_len); /* switches passive mode on or off * returns true on success, false on error */ -int ftp_pasv(ftpbuf_t *ftp, int pasv); +int ftp_pasv(ftpbuf_t *ftp, int pasv); /* retrieves a file and saves its contents to outfp * returns true on success, false on error */ -int ftp_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t path_len, ftptype_t type, zend_long resumepos); +int ftp_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t path_len, ftptype_t type, zend_long resumepos); /* stores the data from a file, socket, or process as a file on the remote server * returns true on success, false on error */ -int ftp_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type, zend_long startpos); +int ftp_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type, zend_long startpos); /* append the data from a file, socket, or process as a file on the remote server * returns true on success, false on error */ -int ftp_append(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type); +int ftp_append(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type); /* returns the size of the given file, or -1 on error */ -zend_long ftp_size(ftpbuf_t *ftp, const char *path, const size_t path_len); +zend_long ftp_size(ftpbuf_t *ftp, const char *path, const size_t path_len); /* returns the last modified time of the given file, or -1 on error */ -time_t ftp_mdtm(ftpbuf_t *ftp, const char *path, const size_t path_len); +time_t ftp_mdtm(ftpbuf_t *ftp, const char *path, const size_t path_len); /* renames a file on the server */ -int ftp_rename(ftpbuf_t *ftp, const char *src, const size_t src_len, const char *dest, const size_t dest_len); +int ftp_rename(ftpbuf_t *ftp, const char *src, const size_t src_len, const char *dest, const size_t dest_len); /* deletes the file from the server */ -int ftp_delete(ftpbuf_t *ftp, const char *path, const size_t path_len); +int ftp_delete(ftpbuf_t *ftp, const char *path, const size_t path_len); /* sends a SITE command to the server */ -int ftp_site(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len); +int ftp_site(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len); /* retrieves part of a file and saves its contents to outfp * returns true on success, false on error */ -int ftp_nb_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t path_len, ftptype_t type, zend_long resumepos); +int ftp_nb_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t path_len, ftptype_t type, zend_long resumepos); /* stores the data from a file, socket, or process as a file on the remote server * returns true on success, false on error */ -int ftp_nb_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type, zend_long startpos); +int ftp_nb_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type, zend_long startpos); /* continues a previous nb_(f)get command */ -int ftp_nb_continue_read(ftpbuf_t *ftp); +int ftp_nb_continue_read(ftpbuf_t *ftp); /* continues a previous nb_(f)put command */ -int ftp_nb_continue_write(ftpbuf_t *ftp); - +int ftp_nb_continue_write(ftpbuf_t *ftp); #endif From 114a8ffb9da006f58776c9978b89d2bee9f6f58d Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Thu, 3 Apr 2025 19:39:34 +0100 Subject: [PATCH 05/14] ext/ftp: Mark static functions as such Removing missleading comment --- ext/ftp/ftp.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/ext/ftp/ftp.c b/ext/ftp/ftp.c index 60ef58347163e..c707a784a1194 100644 --- a/ext/ftp/ftp.c +++ b/ext/ftp/ftp.c @@ -692,7 +692,7 @@ int ftp_mlsd_parse_line(HashTable *ht, const char *input) return SUCCESS; } -int ftp_type(ftpbuf_t *ftp, ftptype_t type) +static int ftp_type(ftpbuf_t *ftp, ftptype_t type) { const char *typechar; @@ -1178,8 +1178,7 @@ int ftp_site(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len) return 1; } -/* static functions */ -int ftp_putcmd(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, const char *args, const size_t args_len) +static int ftp_putcmd(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, const char *args, const size_t args_len) { int size; char *data; @@ -1217,7 +1216,7 @@ int ftp_putcmd(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, const char return 1; } -int ftp_readline(ftpbuf_t *ftp) +static int ftp_readline(ftpbuf_t *ftp) { long size, rcvd; char *data, *eol; @@ -1267,7 +1266,7 @@ int ftp_readline(ftpbuf_t *ftp) return 0; } -int ftp_getresp(ftpbuf_t *ftp) +static int ftp_getresp(ftpbuf_t *ftp) { if (ftp == NULL) { return 0; @@ -1317,7 +1316,7 @@ static ssize_t my_recv_wrapper_with_restart(php_socket_t fd, void *buf, size_t s return n; } -int single_send(ftpbuf_t *ftp, php_socket_t s, void *buf, size_t size) { +static int single_send(ftpbuf_t *ftp, php_socket_t s, void *buf, size_t size) { #ifdef HAVE_FTP_SSL int err; bool retry = 0; @@ -1400,7 +1399,7 @@ static int my_poll(php_socket_t fd, int events, int timeout) { return n; } -int my_send(ftpbuf_t *ftp, php_socket_t s, void *buf, size_t len) +static int my_send(ftpbuf_t *ftp, php_socket_t s, void *buf, size_t len) { zend_long size, sent; int n; @@ -1434,7 +1433,7 @@ int my_send(ftpbuf_t *ftp, php_socket_t s, void *buf, size_t len) return len; } -int my_recv(ftpbuf_t *ftp, php_socket_t s, void *buf, size_t len) +static int my_recv(ftpbuf_t *ftp, php_socket_t s, void *buf, size_t len) { int n, nr_bytes; #ifdef HAVE_FTP_SSL @@ -1510,7 +1509,7 @@ int my_recv(ftpbuf_t *ftp, php_socket_t s, void *buf, size_t len) return (nr_bytes); } -int data_available(ftpbuf_t *ftp, php_socket_t s) +static int data_available(ftpbuf_t *ftp, php_socket_t s) { int n; @@ -1531,7 +1530,7 @@ int data_available(ftpbuf_t *ftp, php_socket_t s) return 1; } -int data_writeable(ftpbuf_t *ftp, php_socket_t s) +static int data_writeable(ftpbuf_t *ftp, php_socket_t s) { int n; @@ -1552,7 +1551,7 @@ int data_writeable(ftpbuf_t *ftp, php_socket_t s) return 1; } -int my_accept(ftpbuf_t *ftp, php_socket_t s, struct sockaddr *addr, socklen_t *addrlen) +static int my_accept(ftpbuf_t *ftp, php_socket_t s, struct sockaddr *addr, socklen_t *addrlen) { int n; @@ -1573,7 +1572,7 @@ int my_accept(ftpbuf_t *ftp, php_socket_t s, struct sockaddr *addr, socklen_t *a return accept(s, addr, addrlen); } -databuf_t* ftp_getdata(ftpbuf_t *ftp) +static databuf_t* ftp_getdata(ftpbuf_t *ftp) { int fd = -1; databuf_t *data; @@ -1703,7 +1702,7 @@ databuf_t* ftp_getdata(ftpbuf_t *ftp) return NULL; } -databuf_t* data_accept(databuf_t *data, ftpbuf_t *ftp) +static databuf_t* data_accept(databuf_t *data, ftpbuf_t *ftp) { php_sockaddr_storage addr; socklen_t size; @@ -1874,7 +1873,7 @@ static void ftp_ssl_shutdown(ftpbuf_t *ftp, php_socket_t fd, SSL *ssl_handle) { } #endif -void data_close(ftpbuf_t *ftp) +static void data_close(ftpbuf_t *ftp) { ZEND_ASSERT(ftp != NULL); databuf_t *data = ftp->data; @@ -1905,7 +1904,7 @@ void data_close(ftpbuf_t *ftp) efree(data); } -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) { php_stream *tmpstream = NULL; databuf_t *data = NULL; From 169573bcb578d59b80e0457dae6d2f8f0fff56e9 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Thu, 3 Apr 2025 19:43:17 +0100 Subject: [PATCH 06/14] ext/ftp: Use bool type instead of int type --- ext/ftp/ftp.c | 284 +++++++++++++++++++++++----------------------- ext/ftp/ftp.h | 32 +++--- ext/ftp/php_ftp.c | 26 ++--- 3 files changed, 169 insertions(+), 173 deletions(-) diff --git a/ext/ftp/ftp.c b/ext/ftp/ftp.c index c707a784a1194..0b63822211f72 100644 --- a/ext/ftp/ftp.c +++ b/ext/ftp/ftp.c @@ -68,7 +68,7 @@ * it sends the string "cmd args\r\n" if args is non-null, or * "cmd\r\n" if args is null */ -static int ftp_putcmd(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, const char *args, const size_t args_len); +static bool ftp_putcmd(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, const char *args, const size_t args_len); /* wrapper around send/recv to handle timeouts */ static int my_send(ftpbuf_t *ftp, php_socket_t s, void *buf, size_t len); @@ -76,13 +76,13 @@ static int my_recv(ftpbuf_t *ftp, php_socket_t s, void *buf, size_t len); static int my_accept(ftpbuf_t *ftp, php_socket_t s, struct sockaddr *addr, socklen_t *addrlen); /* reads a line the socket , returns true on success, false on error */ -static int ftp_readline(ftpbuf_t *ftp); +static bool ftp_readline(ftpbuf_t *ftp); /* reads an ftp response, returns true on success, false on error */ -static int ftp_getresp(ftpbuf_t *ftp); +static bool ftp_getresp(ftpbuf_t *ftp); /* sets the ftp transfer type */ -static int ftp_type(ftpbuf_t *ftp, ftptype_t type); +static bool ftp_type(ftpbuf_t *ftp, ftptype_t type); /* opens up a data stream */ static databuf_t* ftp_getdata(ftpbuf_t *ftp); @@ -194,17 +194,17 @@ void ftp_gc(ftpbuf_t *ftp) } } -int ftp_quit(ftpbuf_t *ftp) +bool ftp_quit(ftpbuf_t *ftp) { if (ftp == NULL) { - return 0; + return false; } if (!ftp_putcmd(ftp, "QUIT", sizeof("QUIT")-1, NULL, (size_t) 0)) { - return 0; + return false; } if (!ftp_getresp(ftp) || ftp->resp != 221) { - return 0; + return false; } if (ftp->pwd) { @@ -212,7 +212,7 @@ int ftp_quit(ftpbuf_t *ftp) ftp->pwd = NULL; } - return 1; + return true; } #ifdef HAVE_FTP_SSL @@ -231,7 +231,7 @@ static int ftp_ssl_new_session_cb(SSL *ssl, SSL_SESSION *sess) } #endif -int ftp_login(ftpbuf_t *ftp, const char *user, const size_t user_len, const char *pass, const size_t pass_len) +bool ftp_login(ftpbuf_t *ftp, const char *user, const size_t user_len, const char *pass, const size_t pass_len) { #ifdef HAVE_FTP_SSL SSL_CTX *ctx = NULL; @@ -240,28 +240,28 @@ int ftp_login(ftpbuf_t *ftp, const char *user, const size_t user_len, const char bool retry; #endif if (ftp == NULL) { - return 0; + return false; } #ifdef HAVE_FTP_SSL if (ftp->use_ssl && !ftp->ssl_active) { if (!ftp_putcmd(ftp, "AUTH", sizeof("AUTH")-1, "TLS", sizeof("TLS")-1)) { - return 0; + return false; } if (!ftp_getresp(ftp)) { - return 0; + return false; } if (ftp->resp != 234) { if (!ftp_putcmd(ftp, "AUTH", sizeof("AUTH")-1, "SSL", sizeof("SSL")-1)) { - return 0; + return false; } if (!ftp_getresp(ftp)) { - return 0; + return false; } if (ftp->resp != 334) { - return 0; + return false; } else { ftp->old_ssl = 1; ftp->use_ssl_for_data = 1; @@ -271,7 +271,7 @@ int ftp_login(ftpbuf_t *ftp, const char *user, const size_t user_len, const char ctx = SSL_CTX_new(SSLv23_client_method()); if (ctx == NULL) { php_error_docref(NULL, E_WARNING, "Failed to create the SSL context"); - return 0; + return false; } ssl_ctx_options &= ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; @@ -288,7 +288,7 @@ int ftp_login(ftpbuf_t *ftp, const char *user, const size_t user_len, const char if (ftp->ssl_handle == NULL) { php_error_docref(NULL, E_WARNING, "Failed to create the SSL handle"); - return 0; + return false; } SSL_set_fd(ftp->ssl_handle, ftp->fd); @@ -300,11 +300,11 @@ int ftp_login(ftpbuf_t *ftp, const char *user, const size_t user_len, const char /* TODO check if handling other error codes would make sense */ switch (err) { case SSL_ERROR_NONE: - retry = 0; + retry = false; break; case SSL_ERROR_ZERO_RETURN: - retry = 0; + retry = false; SSL_shutdown(ftp->ssl_handle); break; @@ -327,28 +327,28 @@ int ftp_login(ftpbuf_t *ftp, const char *user, const size_t user_len, const char php_error_docref(NULL, E_WARNING, "SSL/TLS handshake failed"); SSL_shutdown(ftp->ssl_handle); SSL_free(ftp->ssl_handle); - return 0; + return false; } } while (retry); - ftp->ssl_active = 1; + ftp->ssl_active = true; if (!ftp->old_ssl) { /* set protection buffersize to zero */ if (!ftp_putcmd(ftp, "PBSZ", sizeof("PBSZ")-1, "0", sizeof("0")-1)) { - return 0; + return false; } if (!ftp_getresp(ftp)) { - return 0; + return false; } /* enable data conn encryption */ if (!ftp_putcmd(ftp, "PROT", sizeof("PROT")-1, "P", sizeof("P")-1)) { - return 0; + return false; } if (!ftp_getresp(ftp)) { - return 0; + return false; } ftp->use_ssl_for_data = (ftp->resp >= 200 && ftp->resp <=299); @@ -357,30 +357,30 @@ int ftp_login(ftpbuf_t *ftp, const char *user, const size_t user_len, const char #endif if (!ftp_putcmd(ftp, "USER", sizeof("USER")-1, user, user_len)) { - return 0; + return false; } if (!ftp_getresp(ftp)) { - return 0; + return false; } if (ftp->resp == 230) { - return 1; + return true; } if (ftp->resp != 331) { - return 0; + return false; } if (!ftp_putcmd(ftp, "PASS", sizeof("PASS")-1, pass, pass_len)) { - return 0; + return false; } if (!ftp_getresp(ftp)) { - return 0; + return false; } return (ftp->resp == 230); } -int ftp_reinit(ftpbuf_t *ftp) +bool ftp_reinit(ftpbuf_t *ftp) { if (ftp == NULL) { - return 0; + return false; } ftp_gc(ftp); @@ -388,13 +388,13 @@ int ftp_reinit(ftpbuf_t *ftp) ftp->nb = 0; if (!ftp_putcmd(ftp, "REIN", sizeof("REIN")-1, NULL, (size_t) 0)) { - return 0; + return false; } if (!ftp_getresp(ftp) || ftp->resp != 220) { - return 0; + return false; } - return 1; + return true; } const char* ftp_syst(ftpbuf_t *ftp) @@ -459,19 +459,19 @@ const char* ftp_pwd(ftpbuf_t *ftp) return ftp->pwd; } -int ftp_exec(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len) +bool ftp_exec(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len) { if (ftp == NULL) { - return 0; + return false; } if (!ftp_putcmd(ftp, "SITE EXEC", sizeof("SITE EXEC")-1, cmd, cmd_len)) { - return 0; + return false; } if (!ftp_getresp(ftp) || ftp->resp != 200) { - return 0; + return false; } - return 1; + return true; } void ftp_raw(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, zval *return_value) @@ -491,10 +491,10 @@ void ftp_raw(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, zval *return_ } } -int ftp_chdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len) +bool ftp_chdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len) { if (ftp == NULL) { - return 0; + return false; } if (ftp->pwd) { @@ -503,18 +503,18 @@ int ftp_chdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len) } if (!ftp_putcmd(ftp, "CWD", sizeof("CWD")-1, dir, dir_len)) { - return 0; + return false; } if (!ftp_getresp(ftp) || ftp->resp != 250) { - return 0; + return false; } - return 1; + return true; } -int ftp_cdup(ftpbuf_t *ftp) +bool ftp_cdup(ftpbuf_t *ftp) { if (ftp == NULL) { - return 0; + return false; } if (ftp->pwd) { @@ -523,12 +523,12 @@ int ftp_cdup(ftpbuf_t *ftp) } if (!ftp_putcmd(ftp, "CDUP", sizeof("CDUP")-1, NULL, (size_t) 0)) { - return 0; + return false; } if (!ftp_getresp(ftp) || ftp->resp != 250) { - return 0; + return false; } - return 1; + return true; } zend_string* ftp_mkdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len) @@ -559,70 +559,70 @@ zend_string* ftp_mkdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len) return ret; } -int ftp_rmdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len) +bool ftp_rmdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len) { if (ftp == NULL) { - return 0; + return false; } if (!ftp_putcmd(ftp, "RMD", sizeof("RMD")-1, dir, dir_len)) { - return 0; + return false; } if (!ftp_getresp(ftp) || ftp->resp != 250) { - return 0; + return false; } - return 1; + return true; } -int ftp_chmod(ftpbuf_t *ftp, const int mode, const char *filename, const int filename_len) +bool ftp_chmod(ftpbuf_t *ftp, const int mode, const char *filename, const int filename_len) { char *buffer; size_t buffer_len; if (ftp == NULL || filename_len <= 0) { - return 0; + return false; } buffer_len = spprintf(&buffer, 0, "CHMOD %o %s", mode, filename); if (!buffer) { - return 0; + return false; } if (!ftp_putcmd(ftp, "SITE", sizeof("SITE")-1, buffer, buffer_len)) { efree(buffer); - return 0; + return false; } efree(buffer); if (!ftp_getresp(ftp) || ftp->resp != 200) { - return 0; + return false; } - return 1; + return true; } -int ftp_alloc(ftpbuf_t *ftp, const zend_long size, zend_string **response) +bool ftp_alloc(ftpbuf_t *ftp, const zend_long size, zend_string **response) { char buffer[64]; int buffer_len; if (ftp == NULL || size <= 0) { - return 0; + return false; } buffer_len = snprintf(buffer, sizeof(buffer) - 1, ZEND_LONG_FMT, size); if (buffer_len < 0) { - return 0; + return false; } if (!ftp_putcmd(ftp, "ALLO", sizeof("ALLO")-1, buffer, buffer_len)) { - return 0; + return false; } if (!ftp_getresp(ftp)) { - return 0; + return false; } if (response) { @@ -630,10 +630,10 @@ int ftp_alloc(ftpbuf_t *ftp, const zend_long size, zend_string **response) } if (ftp->resp < 200 || ftp->resp >= 300) { - return 0; + return false; } - return 1; + return true; } char** ftp_nlist(ftpbuf_t *ftp, const char *path, const size_t path_len) @@ -692,35 +692,35 @@ int ftp_mlsd_parse_line(HashTable *ht, const char *input) return SUCCESS; } -static int ftp_type(ftpbuf_t *ftp, ftptype_t type) +static bool ftp_type(ftpbuf_t *ftp, ftptype_t type) { const char *typechar; if (ftp == NULL) { - return 0; + return false; } if (type == ftp->type) { - return 1; + return true; } if (type == FTPTYPE_ASCII) { typechar = "A"; } else if (type == FTPTYPE_IMAGE) { typechar = "I"; } else { - return 0; + return false; } if (!ftp_putcmd(ftp, "TYPE", sizeof("TYPE")-1, typechar, 1)) { - return 0; + return false; } if (!ftp_getresp(ftp) || ftp->resp != 200) { - return 0; + return false; } ftp->type = type; - return 1; + return true; } -int ftp_pasv(ftpbuf_t *ftp, int pasv) +bool ftp_pasv(ftpbuf_t *ftp, int pasv) { char *ptr; union ipbox ipbox; @@ -730,21 +730,21 @@ int ftp_pasv(ftpbuf_t *ftp, int pasv) struct sockaddr_in *sin; if (ftp == NULL) { - return 0; + return false; } if (pasv && ftp->pasv == 2) { - return 1; + return true; } ftp->pasv = 0; if (!pasv) { - return 1; + return true; } n = sizeof(ftp->pasvaddr); memset(&ftp->pasvaddr, 0, n); sa = (struct sockaddr *) &ftp->pasvaddr; if (getpeername(ftp->fd, sa, &n) < 0) { - return 0; + return false; } #ifdef HAVE_IPV6 @@ -754,16 +754,16 @@ int ftp_pasv(ftpbuf_t *ftp, int pasv) /* try EPSV first */ if (!ftp_putcmd(ftp, "EPSV", sizeof("EPSV")-1, NULL, (size_t) 0)) { - return 0; + return false; } if (!ftp_getresp(ftp)) { - return 0; + return false; } if (ftp->resp == 229) { /* parse out the port */ for (ptr = ftp->inbuf; *ptr && *ptr != '('; ptr++); if (!*ptr) { - return 0; + return false; } delimiter = *++ptr; for (n = 0; *ptr && n < 3; ptr++) { @@ -774,10 +774,10 @@ int ftp_pasv(ftpbuf_t *ftp, int pasv) sin6->sin6_port = htons((unsigned short) strtoul(ptr, &endptr, 10)); if (ptr == endptr || *endptr != delimiter) { - return 0; + return false; } ftp->pasv = 2; - return 1; + return true; } } @@ -785,16 +785,16 @@ int ftp_pasv(ftpbuf_t *ftp, int pasv) #endif if (!ftp_putcmd(ftp, "PASV", sizeof("PASV")-1, NULL, (size_t) 0)) { - return 0; + return false; } if (!ftp_getresp(ftp) || ftp->resp != 227) { - return 0; + return false; } /* parse out the IP and port */ for (ptr = ftp->inbuf; *ptr && !isdigit(*ptr); ptr++); n = sscanf(ptr, "%lu,%lu,%lu,%lu,%lu,%lu", &b[0], &b[1], &b[2], &b[3], &b[4], &b[5]); if (n != 6) { - return 0; + return false; } for (n = 0; n < 6; n++) { ipbox.c[n] = (unsigned char) b[n]; @@ -807,17 +807,17 @@ int ftp_pasv(ftpbuf_t *ftp, int pasv) ftp->pasv = 2; - return 1; + return true; } -int ftp_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t path_len, ftptype_t type, zend_long resumepos) +bool ftp_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t path_len, ftptype_t type, zend_long resumepos) { databuf_t *data = NULL; size_t rcvd; char arg[MAX_LENGTH_OF_LONG]; if (ftp == NULL) { - return 0; + return false; } if (!ftp_type(ftp, type)) { goto bail; @@ -894,10 +894,10 @@ int ftp_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t goto bail; } - return 1; + return true; bail: data_close(ftp); - return 0; + return false; } static zend_result ftp_send_stream_to_data_socket(ftpbuf_t *ftp, databuf_t *data, php_stream *instream, ftptype_t type, bool send_once_and_return) @@ -973,13 +973,13 @@ static zend_result ftp_send_stream_to_data_socket(ftpbuf_t *ftp, databuf_t *data return SUCCESS; } -int ftp_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type, zend_long startpos) +bool ftp_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type, zend_long startpos) { databuf_t *data = NULL; char arg[MAX_LENGTH_OF_LONG]; if (ftp == NULL) { - return 0; + return false; } if (!ftp_type(ftp, type)) { goto bail; @@ -1021,18 +1021,18 @@ int ftp_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream * if (!ftp_getresp(ftp) || (ftp->resp != 226 && ftp->resp != 250 && ftp->resp != 200)) { goto bail; } - return 1; + return true; bail: data_close(ftp); - return 0; + return false; } -int ftp_append(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type) +bool ftp_append(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type) { databuf_t *data = NULL; if (ftp == NULL) { - return 0; + return false; } if (!ftp_type(ftp, type)) { goto bail; @@ -1061,10 +1061,10 @@ int ftp_append(ftpbuf_t *ftp, const char *path, const size_t path_len, php_strea if (!ftp_getresp(ftp) || (ftp->resp != 226 && ftp->resp != 250 && ftp->resp != 200)) { goto bail; } - return 1; + return true; bail: data_close(ftp); - return 0; + return false; } zend_long ftp_size(ftpbuf_t *ftp, const char *path, const size_t path_len) @@ -1128,69 +1128,69 @@ time_t ftp_mdtm(ftpbuf_t *ftp, const char *path, const size_t path_len) return stamp; } -int ftp_delete(ftpbuf_t *ftp, const char *path, const size_t path_len) +bool ftp_delete(ftpbuf_t *ftp, const char *path, const size_t path_len) { if (ftp == NULL) { - return 0; + return false; } if (!ftp_putcmd(ftp, "DELE", sizeof("DELE")-1, path, path_len)) { - return 0; + return false; } if (!ftp_getresp(ftp) || ftp->resp != 250) { - return 0; + return false; } - return 1; + return true; } -int ftp_rename(ftpbuf_t *ftp, const char *src, const size_t src_len, const char *dest, const size_t dest_len) +bool ftp_rename(ftpbuf_t *ftp, const char *src, const size_t src_len, const char *dest, const size_t dest_len) { if (ftp == NULL) { - return 0; + return false; } if (!ftp_putcmd(ftp, "RNFR", sizeof("RNFR")-1, src, src_len)) { - return 0; + return false; } if (!ftp_getresp(ftp) || ftp->resp != 350) { - return 0; + return false; } if (!ftp_putcmd(ftp, "RNTO", sizeof("RNTO")-1, dest, dest_len)) { - return 0; + return false; } if (!ftp_getresp(ftp) || ftp->resp != 250) { - return 0; + return false; } - return 1; + return true; } -int ftp_site(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len) +bool ftp_site(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len) { if (ftp == NULL) { - return 0; + return false; } if (!ftp_putcmd(ftp, "SITE", sizeof("SITE")-1, cmd, cmd_len)) { - return 0; + return false; } if (!ftp_getresp(ftp) || ftp->resp < 200 || ftp->resp >= 300) { - return 0; + return false; } - return 1; + return true; } -static int ftp_putcmd(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, const char *args, const size_t args_len) +static bool ftp_putcmd(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, const char *args, const size_t args_len) { int size; char *data; if (strpbrk(cmd, "\r\n")) { - return 0; + return false; } /* build the output buffer */ if (args && args[0]) { /* "cmd args\r\n\0" */ if (cmd_len + args_len + 4 > FTP_BUFSIZE) { - return 0; + return false; } if (strpbrk(args, "\r\n")) { return 0; @@ -1199,7 +1199,7 @@ static int ftp_putcmd(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, cons } else { /* "cmd\r\n\0" */ if (cmd_len + 3 > FTP_BUFSIZE) { - return 0; + return false; } size = slprintf(ftp->outbuf, sizeof(ftp->outbuf), "%s\r\n", cmd); } @@ -1211,12 +1211,12 @@ static int ftp_putcmd(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, cons ftp->extra = NULL; if (my_send(ftp, ftp->fd, data, size) != size) { - return 0; + return false; } - return 1; + return true; } -static int ftp_readline(ftpbuf_t *ftp) +static bool ftp_readline(ftpbuf_t *ftp) { long size, rcvd; char *data, *eol; @@ -1244,39 +1244,39 @@ static int ftp_readline(ftpbuf_t *ftp) if ((ftp->extralen = --rcvd) == 0) { ftp->extra = NULL; } - return 1; + return true; } else if (*eol == '\n') { *eol = 0; ftp->extra = eol + 1; if ((ftp->extralen = --rcvd) == 0) { ftp->extra = NULL; } - return 1; + return true; } } data = eol; if ((rcvd = my_recv(ftp, ftp->fd, data, size)) < 1) { *data = 0; - return 0; + return false; } } while (size); *data = 0; - return 0; + return false; } -static int ftp_getresp(ftpbuf_t *ftp) +static bool ftp_getresp(ftpbuf_t *ftp) { if (ftp == NULL) { - return 0; + return false; } ftp->resp = 0; while (1) { if (!ftp_readline(ftp)) { - return 0; + return false; } /* Break out when the end-tag is found */ @@ -1287,7 +1287,7 @@ static int ftp_getresp(ftpbuf_t *ftp) /* translate the tag */ if (!isdigit(ftp->inbuf[0]) || !isdigit(ftp->inbuf[1]) || !isdigit(ftp->inbuf[2])) { - return 0; + return false; } ftp->resp = 100 * (ftp->inbuf[0] - '0') + 10 * (ftp->inbuf[1] - '0') + (ftp->inbuf[2] - '0'); @@ -1297,7 +1297,7 @@ static int ftp_getresp(ftpbuf_t *ftp) if (ftp->extra) { ftp->extra -= 4; } - return 1; + return true; } static ssize_t my_send_wrapper_with_restart(php_socket_t fd, const void *buf, size_t size, int flags) { @@ -1509,7 +1509,7 @@ static int my_recv(ftpbuf_t *ftp, php_socket_t s, void *buf, size_t len) return (nr_bytes); } -static int data_available(ftpbuf_t *ftp, php_socket_t s) +static bool data_available(ftpbuf_t *ftp, php_socket_t s) { int n; @@ -1524,13 +1524,13 @@ static int data_available(ftpbuf_t *ftp, php_socket_t s) #endif } php_error_docref(NULL, E_WARNING, "%s", php_socket_strerror(errno, buf, sizeof buf)); - return 0; + return false; } - return 1; + return true; } -static int data_writeable(ftpbuf_t *ftp, php_socket_t s) +static bool data_writeable(ftpbuf_t *ftp, php_socket_t s) { int n; @@ -1545,10 +1545,10 @@ static int data_writeable(ftpbuf_t *ftp, php_socket_t s) #endif } php_error_docref(NULL, E_WARNING, "%s", php_socket_strerror(errno, buf, sizeof buf)); - return 0; + return false; } - return 1; + return true; } static int my_accept(ftpbuf_t *ftp, php_socket_t s, struct sockaddr *addr, socklen_t *addrlen) diff --git a/ext/ftp/ftp.h b/ext/ftp/ftp.h index bcd69059cbc86..8f25a5089beb1 100644 --- a/ext/ftp/ftp.h +++ b/ext/ftp/ftp.h @@ -97,7 +97,7 @@ ftpbuf_t* ftp_open(const char *host, short port, zend_long timeout_sec); /* quits from the ftp session (it still needs to be closed) * return true on success, false on error */ -int ftp_quit(ftpbuf_t *ftp); +bool ftp_quit(ftpbuf_t *ftp); /* frees up any cached data held in the ftp buffer */ void ftp_gc(ftpbuf_t *ftp); @@ -106,10 +106,10 @@ void ftp_gc(ftpbuf_t *ftp); void ftp_close(ftpbuf_t *ftp); /* logs into the FTP server, returns true on success, false on error */ -int ftp_login(ftpbuf_t *ftp, const char *user, const size_t user_len, const char *pass, const size_t pass_len); +bool ftp_login(ftpbuf_t *ftp, const char *user, const size_t user_len, const char *pass, const size_t pass_len); /* reinitializes the connection, returns true on success, false on error */ -int ftp_reinit(ftpbuf_t *ftp); +bool ftp_reinit(ftpbuf_t *ftp); /* returns the remote system type (NULL on error) */ const char* ftp_syst(ftpbuf_t *ftp); @@ -118,16 +118,16 @@ const char* ftp_syst(ftpbuf_t *ftp); const char* ftp_pwd(ftpbuf_t *ftp); /* exec a command [special features], return true on success, false on error */ -int ftp_exec(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len); +bool ftp_exec(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len); /* send a raw ftp command, return response as a hashtable, NULL on error */ void ftp_raw(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, zval *return_value); /* changes directories, return true on success, false on error */ -int ftp_chdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len); +bool ftp_chdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len); /* changes to parent directory, return true on success, false on error */ -int ftp_cdup(ftpbuf_t *ftp); +bool ftp_cdup(ftpbuf_t *ftp); /* creates a directory, return the directory name on success, NULL on error. * the return value must be freed @@ -135,17 +135,17 @@ int ftp_cdup(ftpbuf_t *ftp); zend_string* ftp_mkdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len); /* removes a directory, return true on success, false on error */ -int ftp_rmdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len); +bool ftp_rmdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len); /* Set permissions on a file */ -int ftp_chmod(ftpbuf_t *ftp, const int mode, const char *filename, const int filename_len); +bool ftp_chmod(ftpbuf_t *ftp, const int mode, const char *filename, const int filename_len); /* Allocate space on remote server with ALLO command * Many servers will respond with 202 Allocation not necessary, * however some servers will not accept STOR or APPE until ALLO is confirmed. * If response is passed, it is estrdup()ed from ftp->inbuf and must be freed * or assigned to a zval returned to the user */ -int ftp_alloc(ftpbuf_t *ftp, const zend_long size, zend_string **response); +bool ftp_alloc(ftpbuf_t *ftp, const zend_long size, zend_string **response); /* returns a NULL-terminated array of filenames in the given path * or NULL on error. the return array must be freed (but don't @@ -175,22 +175,22 @@ char** ftp_mlsd(ftpbuf_t *ftp, const char *path, const size_t path_len); /* switches passive mode on or off * returns true on success, false on error */ -int ftp_pasv(ftpbuf_t *ftp, int pasv); +bool ftp_pasv(ftpbuf_t *ftp, int pasv); /* retrieves a file and saves its contents to outfp * returns true on success, false on error */ -int ftp_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t path_len, ftptype_t type, zend_long resumepos); +bool ftp_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t path_len, ftptype_t type, zend_long resumepos); /* stores the data from a file, socket, or process as a file on the remote server * returns true on success, false on error */ -int ftp_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type, zend_long startpos); +bool ftp_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type, zend_long startpos); /* append the data from a file, socket, or process as a file on the remote server * returns true on success, false on error */ -int ftp_append(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type); +bool ftp_append(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type); /* returns the size of the given file, or -1 on error */ zend_long ftp_size(ftpbuf_t *ftp, const char *path, const size_t path_len); @@ -199,13 +199,13 @@ zend_long ftp_size(ftpbuf_t *ftp, const char *path, const size_t path_len); time_t ftp_mdtm(ftpbuf_t *ftp, const char *path, const size_t path_len); /* renames a file on the server */ -int ftp_rename(ftpbuf_t *ftp, const char *src, const size_t src_len, const char *dest, const size_t dest_len); +bool ftp_rename(ftpbuf_t *ftp, const char *src, const size_t src_len, const char *dest, const size_t dest_len); /* deletes the file from the server */ -int ftp_delete(ftpbuf_t *ftp, const char *path, const size_t path_len); +bool ftp_delete(ftpbuf_t *ftp, const char *path, const size_t path_len); /* sends a SITE command to the server */ -int ftp_site(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len); +bool ftp_site(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len); /* retrieves part of a file and saves its contents to outfp * returns true on success, false on error diff --git a/ext/ftp/php_ftp.c b/ext/ftp/php_ftp.c index 17dc94b728eba..a1e0ff1f809ce 100644 --- a/ext/ftp/php_ftp.c +++ b/ext/ftp/php_ftp.c @@ -162,7 +162,7 @@ PHP_FUNCTION(ftp_connect) ftp->usepasvaddress = FTP_DEFAULT_USEPASVADDRESS; #ifdef HAVE_FTP_SSL /* disable ssl */ - ftp->use_ssl = 0; + ftp->use_ssl = false; #endif object_init_ex(return_value, php_ftp_ce); @@ -198,7 +198,7 @@ PHP_FUNCTION(ftp_ssl_connect) ftp->autoseek = FTP_DEFAULT_AUTOSEEK; ftp->usepasvaddress = FTP_DEFAULT_USEPASVADDRESS; /* enable ssl */ - ftp->use_ssl = 1; + ftp->use_ssl = true; object_init_ex(return_value, php_ftp_ce); ftp_object_from_zend_object(Z_OBJ_P(return_value))->ftp = ftp; @@ -432,7 +432,7 @@ PHP_FUNCTION(ftp_alloc) { zval *z_ftp, *zresponse = NULL; ftpbuf_t *ftp; - zend_long size, ret; + zend_long size; zend_string *response = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol|z", &z_ftp, php_ftp_ce, &size, &zresponse) == FAILURE) { @@ -440,17 +440,13 @@ PHP_FUNCTION(ftp_alloc) } GET_FTPBUF(ftp, z_ftp); - ret = ftp_alloc(ftp, size, zresponse ? &response : NULL); + bool ret = ftp_alloc(ftp, size, zresponse ? &response : NULL); if (response) { ZEND_TRY_ASSIGN_REF_STR(zresponse, response); } - if (!ret) { - RETURN_FALSE; - } - - RETURN_TRUE; + RETURN_BOOL(ret); } /* }}} */ @@ -797,8 +793,8 @@ PHP_FUNCTION(ftp_nb_get) } /* configuration */ - ftp->direction = 0; /* recv */ - ftp->closestream = 1; /* do close */ + ftp->direction = false; /* recv */ + ftp->closestream = true; /* do close */ if ((ret = ftp_nb_get(ftp, outstream, remote, remote_len, xtype, resumepos)) == PHP_FTP_FAILED) { php_stream_close(outstream); @@ -950,8 +946,8 @@ PHP_FUNCTION(ftp_nb_fput) } /* configuration */ - ftp->direction = 1; /* send */ - ftp->closestream = 0; /* do not close */ + ftp->direction = true; /* send */ + ftp->closestream = false; /* do not close */ if (((ret = ftp_nb_put(ftp, remote, remote_len, stream, xtype, startpos)) == PHP_FTP_FAILED)) { if (*ftp->inbuf) { @@ -1091,8 +1087,8 @@ PHP_FUNCTION(ftp_nb_put) } /* configuration */ - ftp->direction = 1; /* send */ - ftp->closestream = 1; /* do close */ + ftp->direction = true; /* send */ + ftp->closestream = true; /* do close */ ret = ftp_nb_put(ftp, remote, remote_len, instream, xtype, startpos); From 7fcdf1cfa20258609e2e68b10e373f822b8111df Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Thu, 3 Apr 2025 19:56:22 +0100 Subject: [PATCH 07/14] ext/ftp: Use zend_result type instead of int type --- ext/ftp/ftp.c | 2 +- ext/ftp/ftp.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/ftp/ftp.c b/ext/ftp/ftp.c index 0b63822211f72..ffe955766bc0d 100644 --- a/ext/ftp/ftp.c +++ b/ext/ftp/ftp.c @@ -651,7 +651,7 @@ char** ftp_mlsd(ftpbuf_t *ftp, const char *path, const size_t path_len) return ftp_genlist(ftp, "MLSD", sizeof("MLSD")-1, path, path_len); } -int ftp_mlsd_parse_line(HashTable *ht, const char *input) +zend_result ftp_mlsd_parse_line(HashTable *ht, const char *input) { zval zstr; const char *end = input + strlen(input); diff --git a/ext/ftp/ftp.h b/ext/ftp/ftp.h index 8f25a5089beb1..0e73ee30850f4 100644 --- a/ext/ftp/ftp.h +++ b/ext/ftp/ftp.h @@ -163,7 +163,7 @@ char** ftp_list(ftpbuf_t *ftp, const char *path, const size_t path_len, int recu /* populates a hashtable with the facts contained in one line of * an MLSD response. */ -int ftp_mlsd_parse_line(HashTable *ht, const char *input); +zend_result ftp_mlsd_parse_line(HashTable *ht, const char *input); /* returns a NULL-terminated array of lines returned by the ftp * MLSD command for the given path or NULL on error. the return From 96cf1b5a9f39fc020bdaab7a894641d0412004bb Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Thu, 3 Apr 2025 20:17:13 +0100 Subject: [PATCH 08/14] ext/ftp: Use size_t type instead of int type --- ext/ftp/ftp.c | 4 ++-- ext/ftp/ftp.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/ftp/ftp.c b/ext/ftp/ftp.c index ffe955766bc0d..39555d8c7e322 100644 --- a/ext/ftp/ftp.c +++ b/ext/ftp/ftp.c @@ -573,12 +573,12 @@ bool ftp_rmdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len) return true; } -bool ftp_chmod(ftpbuf_t *ftp, const int mode, const char *filename, const int filename_len) +bool ftp_chmod(ftpbuf_t *ftp, const int mode, const char *filename, const size_t filename_len) { char *buffer; size_t buffer_len; - if (ftp == NULL || filename_len <= 0) { + if (ftp == NULL || filename_len == 0) { return false; } diff --git a/ext/ftp/ftp.h b/ext/ftp/ftp.h index 0e73ee30850f4..5f01f202b8e26 100644 --- a/ext/ftp/ftp.h +++ b/ext/ftp/ftp.h @@ -138,7 +138,7 @@ zend_string* ftp_mkdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len); bool ftp_rmdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len); /* Set permissions on a file */ -bool ftp_chmod(ftpbuf_t *ftp, const int mode, const char *filename, const int filename_len); +bool ftp_chmod(ftpbuf_t *ftp, const int mode, const char *filename, const size_t filename_len); /* Allocate space on remote server with ALLO command * Many servers will respond with 202 Allocation not necessary, From c8559648faa8f507c7528634a16f60e11cc85ddf Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Thu, 3 Apr 2025 20:15:38 +0100 Subject: [PATCH 09/14] [skip ci] ext/ftp: Fix typos --- ext/ftp/php_ftp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/ftp/php_ftp.c b/ext/ftp/php_ftp.c index a1e0ff1f809ce..fb771a66d73e4 100644 --- a/ext/ftp/php_ftp.c +++ b/ext/ftp/php_ftp.c @@ -390,7 +390,7 @@ PHP_FUNCTION(ftp_rmdir) } GET_FTPBUF(ftp, z_ftp); - /* remove directorie */ + /* remove directories */ if (!ftp_rmdir(ftp, dir, dir_len)) { if (*ftp->inbuf) { php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf); From 1dd788acdcf4a58aa6ea7fb573bbe16d38b4bb36 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Thu, 3 Apr 2025 20:24:33 +0100 Subject: [PATCH 10/14] ext/ftp: Change return type of ftp_set_option() to true It either returns true or throws an Error --- ext/ftp/ftp.stub.php | 2 +- ext/ftp/ftp_arginfo.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/ftp/ftp.stub.php b/ext/ftp/ftp.stub.php index beab97f2d98e4..6560dd8930186 100644 --- a/ext/ftp/ftp.stub.php +++ b/ext/ftp/ftp.stub.php @@ -132,7 +132,7 @@ function ftp_close(FTP\Connection $ftp): bool {} function ftp_quit(FTP\Connection $ftp): bool {} /** @param int|bool $value */ - function ftp_set_option(FTP\Connection $ftp, int $option, $value): bool {} + function ftp_set_option(FTP\Connection $ftp, int $option, $value): true {} function ftp_get_option(FTP\Connection $ftp, int $option): int|bool {} } diff --git a/ext/ftp/ftp_arginfo.h b/ext/ftp/ftp_arginfo.h index e28a549f9d670..77bc47df03f69 100644 --- a/ext/ftp/ftp_arginfo.h +++ b/ext/ftp/ftp_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 072486274a3361dee3655cfd046a293cfb8a2757 */ + * Stub hash: 29606d7114a0698b8ae231173a624b17c196ffec */ ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_ftp_connect, 0, 1, FTP\\Connection, MAY_BE_FALSE) ZEND_ARG_TYPE_INFO(0, hostname, IS_STRING, 0) @@ -182,7 +182,7 @@ ZEND_END_ARG_INFO() #define arginfo_ftp_quit arginfo_ftp_cdup -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ftp_set_option, 0, 3, _IS_BOOL, 0) +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ftp_set_option, 0, 3, IS_TRUE, 0) ZEND_ARG_OBJ_INFO(0, ftp, FTP\\Connection, 0) ZEND_ARG_TYPE_INFO(0, option, IS_LONG, 0) ZEND_ARG_INFO(0, value) From 8033b058a97a59aa467f6031d313110f26c714b5 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Thu, 3 Apr 2025 20:35:04 +0100 Subject: [PATCH 11/14] ext/ftp: Remove output field of ftpbuf_t struct It was only used once, and removing it reduces the size of a userland FTP object by 4096 bytes --- ext/ftp/ftp.c | 8 +++----- ext/ftp/ftp.h | 1 - 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/ext/ftp/ftp.c b/ext/ftp/ftp.c index 39555d8c7e322..f37ee68617616 100644 --- a/ext/ftp/ftp.c +++ b/ext/ftp/ftp.c @@ -1181,7 +1181,7 @@ bool ftp_site(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len) static bool ftp_putcmd(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, const char *args, const size_t args_len) { int size; - char *data; + char data[FTP_BUFSIZE]; if (strpbrk(cmd, "\r\n")) { return false; @@ -1195,17 +1195,15 @@ static bool ftp_putcmd(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, con if (strpbrk(args, "\r\n")) { return 0; } - size = slprintf(ftp->outbuf, sizeof(ftp->outbuf), "%s %s\r\n", cmd, args); + size = slprintf(data, sizeof(data), "%s %s\r\n", cmd, args); } else { /* "cmd\r\n\0" */ if (cmd_len + 3 > FTP_BUFSIZE) { return false; } - size = slprintf(ftp->outbuf, sizeof(ftp->outbuf), "%s\r\n", cmd); + size = slprintf(data, sizeof(data), "%s\r\n", cmd); } - data = ftp->outbuf; - /* Clear the inbuf and extra-lines buffer */ ftp->inbuf[0] = '\0'; ftp->extra = NULL; diff --git a/ext/ftp/ftp.h b/ext/ftp/ftp.h index 5f01f202b8e26..241f92f57ec35 100644 --- a/ext/ftp/ftp.h +++ b/ext/ftp/ftp.h @@ -60,7 +60,6 @@ typedef struct ftpbuf char inbuf[FTP_BUFSIZE]; /* last response text */ char *extra; /* extra characters */ int extralen; /* number of extra chars */ - char outbuf[FTP_BUFSIZE]; /* command output buffer */ char *pwd; /* cached pwd */ char *syst; /* cached system type */ ftptype_t type; /* current transfer type */ From 2c326d8222199e952d3a9ab07621724bbf8d93c6 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Sun, 16 Mar 2025 00:42:45 +0000 Subject: [PATCH 12/14] ext/standard: Add Directory test with messed up internal state --- ...adonly_handle_by_pass_via_ArrayObject.phpt | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 ext/standard/tests/directory/DirectoryClass_readonly_handle_by_pass_via_ArrayObject.phpt diff --git a/ext/standard/tests/directory/DirectoryClass_readonly_handle_by_pass_via_ArrayObject.phpt b/ext/standard/tests/directory/DirectoryClass_readonly_handle_by_pass_via_ArrayObject.phpt new file mode 100644 index 0000000000000..cc57fc6933aa4 --- /dev/null +++ b/ext/standard/tests/directory/DirectoryClass_readonly_handle_by_pass_via_ArrayObject.phpt @@ -0,0 +1,34 @@ +--TEST-- +Changing Directory::$handle property +--FILE-- +getMessage(), PHP_EOL; +} +var_dump($d->handle); + +try { + $s = $d->read(); + var_dump($s); +} catch (\Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} + +try { + unset($ao['handle']); + var_dump($d->handle); +} catch (\Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} + +?> +--EXPECT-- +resource(3) of type (stream) +TypeError: Directory::read(): Argument #1 must be a valid Directory resource +Error: Typed property Directory::$handle must not be accessed before initialization From 4101a8c09997a7ca1a681a193eb005f399088bfd Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Sun, 16 Mar 2025 01:58:48 +0000 Subject: [PATCH 13/14] ext/standard/dir.c: Refactor implementation of Directory and dir functions --- ext/standard/dir.c | 224 ++++++++++++------ ext/standard/dir.stub.php | 9 - ext/standard/dir_arginfo.h | 14 +- .../dir/closedir_variation2-win32-mb.phpt | 2 +- .../tests/dir/closedir_variation2.phpt | 2 +- .../tests/dir/dir_basic-win32-mb.phpt | 2 +- ext/standard/tests/dir/dir_basic.phpt | 2 +- .../dir/rewinddir_variation2-win32-mb.phpt | 2 +- .../tests/dir/rewinddir_variation2.phpt | 2 +- ...adonly_handle_by_pass_via_ArrayObject.phpt | 2 +- ...flection_create_instance_no_construct.phpt | 2 +- 11 files changed, 160 insertions(+), 103 deletions(-) diff --git a/ext/standard/dir.c b/ext/standard/dir.c index a2a50eab7f51d..845fcd03eeadc 100644 --- a/ext/standard/dir.c +++ b/ext/standard/dir.c @@ -59,39 +59,6 @@ static zend_function *dir_class_get_constructor(zend_object *object) return NULL; } -#define FETCH_DIRP() \ - myself = getThis(); \ - if (!myself) { \ - ZEND_PARSE_PARAMETERS_START(0, 1) \ - Z_PARAM_OPTIONAL \ - Z_PARAM_RESOURCE_OR_NULL(id) \ - ZEND_PARSE_PARAMETERS_END(); \ - if (id) { \ - if ((dirp = (php_stream *)zend_fetch_resource(Z_RES_P(id), "Directory", php_file_le_stream())) == NULL) { \ - RETURN_THROWS(); \ - } \ - } else { \ - if (!DIRG(default_dir)) { \ - zend_type_error("No resource supplied"); \ - RETURN_THROWS(); \ - } \ - if ((dirp = (php_stream *)zend_fetch_resource(DIRG(default_dir), "Directory", php_file_le_stream())) == NULL) { \ - RETURN_THROWS(); \ - } \ - } \ - } else { \ - ZEND_PARSE_PARAMETERS_NONE(); \ - zval *handle_zv = Z_DIRECTORY_HANDLE_P(myself); \ - if (Z_TYPE_P(handle_zv) != IS_RESOURCE) { \ - zend_throw_error(NULL, "Unable to find my handle property"); \ - RETURN_THROWS(); \ - } \ - if ((dirp = (php_stream *)zend_fetch_resource_ex(handle_zv, "Directory", php_file_le_stream())) == NULL) { \ - RETURN_THROWS(); \ - } \ - } - - static void php_set_default_dir(zend_resource *res) { if (DIRG(default_dir)) { @@ -189,29 +156,166 @@ PHP_FUNCTION(dir) } /* }}} */ + +static php_stream* php_dir_get_directory_stream_from_user_arg(zval *arg) +{ + zend_resource *res; + if (arg == NULL) { + if (UNEXPECTED(DIRG(default_dir) == NULL)) { + zend_type_error("No resource supplied"); + return NULL; + } + res = DIRG(default_dir); + } else { + ZEND_ASSERT(Z_TYPE_P(arg) == IS_RESOURCE); + res = Z_RES_P(arg); + } + + if (UNEXPECTED(res->type != php_file_le_stream())) { + zend_argument_type_error(1, "must be a valid Directory resource"); + return NULL; + } + php_stream *dir_stream = (php_stream*) res->ptr; + if (UNEXPECTED((dir_stream->flags & PHP_STREAM_FLAG_IS_DIR)) == 0) { + zend_argument_type_error(1, "must be a valid Directory resource"); + return NULL; + } + return dir_stream; +} + +static php_stream* php_dir_get_directory_stream_from_this(zval *this_z) +{ + zval *handle_zv = Z_DIRECTORY_HANDLE_P(this_z); + if (UNEXPECTED(Z_TYPE_P(handle_zv) != IS_RESOURCE)) { + zend_throw_error(NULL, "Internal directory stream has been altered"); + return NULL; + } + zend_resource *res = Z_RES_P(handle_zv); + /* Assume the close() method was called + * (instead of the hacky case where a different resource would have been set via the ArrayObject "hack") */ + if (UNEXPECTED(res->type != php_file_le_stream())) { + /* TypeError is used for BC, TODO: Use base Error in PHP 9 */ + zend_type_error("Directory::%s(): cannot use Directory resource after it has been closed", get_active_function_name()); + return NULL; + } + php_stream *dir_stream = (php_stream*) res->ptr; + if (UNEXPECTED((dir_stream->flags & PHP_STREAM_FLAG_IS_DIR)) == 0) { + zend_throw_error(NULL, "Internal directory stream has been altered"); + return NULL; + } + return dir_stream; +} + /* {{{ Close directory connection identified by the dir_handle */ PHP_FUNCTION(closedir) { - zval *id = NULL, *myself; - php_stream *dirp; - zend_resource *res; + zval *id = NULL; - FETCH_DIRP(); + ZEND_PARSE_PARAMETERS_START(0, 1) + Z_PARAM_OPTIONAL + Z_PARAM_RESOURCE_OR_NULL(id) + ZEND_PARSE_PARAMETERS_END(); - if (!(dirp->flags & PHP_STREAM_FLAG_IS_DIR)) { - zend_argument_type_error(1, "must be a valid Directory resource"); + php_stream *dirp = php_dir_get_directory_stream_from_user_arg(id); + if (UNEXPECTED(dirp == NULL)) { RETURN_THROWS(); } + zend_resource *res = dirp->res; + zend_list_close(res); - res = dirp->res; - zend_list_close(dirp->res); + if (res == DIRG(default_dir)) { + php_set_default_dir(NULL); + } +} +/* }}} */ + +PHP_METHOD(Directory, close) +{ + ZEND_PARSE_PARAMETERS_NONE(); + + php_stream *dirp = php_dir_get_directory_stream_from_this(ZEND_THIS); + if (UNEXPECTED(dirp == NULL)) { + RETURN_THROWS(); + } + + zend_resource *res = dirp->res; + zend_list_close(res); if (res == DIRG(default_dir)) { php_set_default_dir(NULL); } } + +/* {{{ Rewind dir_handle back to the start */ +PHP_FUNCTION(rewinddir) +{ + zval *id = NULL; + + ZEND_PARSE_PARAMETERS_START(0, 1) + Z_PARAM_OPTIONAL + Z_PARAM_RESOURCE_OR_NULL(id) + ZEND_PARSE_PARAMETERS_END(); + + php_stream *dirp = php_dir_get_directory_stream_from_user_arg(id); + if (UNEXPECTED(dirp == NULL)) { + RETURN_THROWS(); + } + + php_stream_rewinddir(dirp); +} /* }}} */ +PHP_METHOD(Directory, rewind) +{ + ZEND_PARSE_PARAMETERS_NONE(); + + php_stream *dirp = php_dir_get_directory_stream_from_this(ZEND_THIS); + if (UNEXPECTED(dirp == NULL)) { + RETURN_THROWS(); + } + + php_stream_rewinddir(dirp); +} + +/* {{{ Read directory entry from dir_handle */ +PHP_FUNCTION(readdir) +{ + zval *id = NULL; + + ZEND_PARSE_PARAMETERS_START(0, 1) + Z_PARAM_OPTIONAL + Z_PARAM_RESOURCE_OR_NULL(id) + ZEND_PARSE_PARAMETERS_END(); + + php_stream *dirp = php_dir_get_directory_stream_from_user_arg(id); + if (UNEXPECTED(dirp == NULL)) { + RETURN_THROWS(); + } + + php_stream_dirent entry; + if (php_stream_readdir(dirp, &entry)) { + RETURN_STRING(entry.d_name); + } + RETURN_FALSE; +} +/* }}} */ + +PHP_METHOD(Directory, read) +{ + ZEND_PARSE_PARAMETERS_NONE(); + + php_stream *dirp = php_dir_get_directory_stream_from_this(ZEND_THIS); + if (UNEXPECTED(dirp == NULL)) { + RETURN_THROWS(); + } + + php_stream_dirent entry; + if (php_stream_readdir(dirp, &entry)) { + RETURN_STRING(entry.d_name); + } + RETURN_FALSE; +} + #if defined(HAVE_CHROOT) && !defined(ZTS) && defined(ENABLE_CHROOT_FUNC) /* {{{ Change root directory */ PHP_FUNCTION(chroot) @@ -300,44 +404,6 @@ PHP_FUNCTION(getcwd) } /* }}} */ -/* {{{ Rewind dir_handle back to the start */ -PHP_FUNCTION(rewinddir) -{ - zval *id = NULL, *myself; - php_stream *dirp; - - FETCH_DIRP(); - - if (!(dirp->flags & PHP_STREAM_FLAG_IS_DIR)) { - zend_argument_type_error(1, "must be a valid Directory resource"); - RETURN_THROWS(); - } - - php_stream_rewinddir(dirp); -} -/* }}} */ - -/* {{{ Read directory entry from dir_handle */ -PHP_FUNCTION(readdir) -{ - zval *id = NULL, *myself; - php_stream *dirp; - php_stream_dirent entry; - - FETCH_DIRP(); - - if (!(dirp->flags & PHP_STREAM_FLAG_IS_DIR)) { - zend_argument_type_error(1, "must be a valid Directory resource"); - RETURN_THROWS(); - } - - if (php_stream_readdir(dirp, &entry)) { - RETURN_STRINGL(entry.d_name, strlen(entry.d_name)); - } - RETURN_FALSE; -} -/* }}} */ - #ifdef HAVE_GLOB /* {{{ Find pathnames matching a pattern */ PHP_FUNCTION(glob) diff --git a/ext/standard/dir.stub.php b/ext/standard/dir.stub.php index 457a965352516..afca1fcacc421 100644 --- a/ext/standard/dir.stub.php +++ b/ext/standard/dir.stub.php @@ -98,18 +98,9 @@ final class Directory /** @var resource */ public readonly mixed $handle; - /** - * @implementation-alias closedir - */ public function close(): void {} - /** - * @implementation-alias rewinddir - */ public function rewind(): void {} - /** - * @implementation-alias readdir - */ public function read(): string|false {} } diff --git a/ext/standard/dir_arginfo.h b/ext/standard/dir_arginfo.h index 9b293f3cd753f..f73b0e5d86ad1 100644 --- a/ext/standard/dir_arginfo.h +++ b/ext/standard/dir_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 069117bab1b9502faf516307aa7e80308f7b7f13 */ + * Stub hash: 543d0d12062ed88dab7a3ac4354499682c5c7166 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Directory_close, 0, 0, IS_VOID, 0) ZEND_END_ARG_INFO() @@ -9,14 +9,14 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_Directory_read, 0, 0, MAY_BE_STRING|MAY_BE_FALSE) ZEND_END_ARG_INFO() -ZEND_FUNCTION(closedir); -ZEND_FUNCTION(rewinddir); -ZEND_FUNCTION(readdir); +ZEND_METHOD(Directory, close); +ZEND_METHOD(Directory, rewind); +ZEND_METHOD(Directory, read); static const zend_function_entry class_Directory_methods[] = { - ZEND_RAW_FENTRY("close", zif_closedir, arginfo_class_Directory_close, ZEND_ACC_PUBLIC, NULL, NULL) - ZEND_RAW_FENTRY("rewind", zif_rewinddir, arginfo_class_Directory_rewind, ZEND_ACC_PUBLIC, NULL, NULL) - ZEND_RAW_FENTRY("read", zif_readdir, arginfo_class_Directory_read, ZEND_ACC_PUBLIC, NULL, NULL) + ZEND_ME(Directory, close, arginfo_class_Directory_close, ZEND_ACC_PUBLIC) + ZEND_ME(Directory, rewind, arginfo_class_Directory_rewind, ZEND_ACC_PUBLIC) + ZEND_ME(Directory, read, arginfo_class_Directory_read, ZEND_ACC_PUBLIC) ZEND_FE_END }; diff --git a/ext/standard/tests/dir/closedir_variation2-win32-mb.phpt b/ext/standard/tests/dir/closedir_variation2-win32-mb.phpt index 9999cb8b10e51..08842ae7f7b10 100644 --- a/ext/standard/tests/dir/closedir_variation2-win32-mb.phpt +++ b/ext/standard/tests/dir/closedir_variation2-win32-mb.phpt @@ -47,5 +47,5 @@ NULL Directory Handle: resource(%d) of type (Unknown) -- Close directory handle second time: -- -closedir(): %s is not a valid Directory resource +closedir(): Argument #1 ($dir_handle) must be a valid Directory resource Directory Handle: resource(%d) of type (Unknown) diff --git a/ext/standard/tests/dir/closedir_variation2.phpt b/ext/standard/tests/dir/closedir_variation2.phpt index e4b782c255ce8..e8518c3d3efcf 100644 --- a/ext/standard/tests/dir/closedir_variation2.phpt +++ b/ext/standard/tests/dir/closedir_variation2.phpt @@ -41,5 +41,5 @@ NULL Directory Handle: resource(%d) of type (Unknown) -- Close directory handle second time: -- -closedir(): supplied resource is not a valid Directory resource +closedir(): Argument #1 ($dir_handle) must be a valid Directory resource Directory Handle: resource(%d) of type (Unknown) diff --git a/ext/standard/tests/dir/dir_basic-win32-mb.phpt b/ext/standard/tests/dir/dir_basic-win32-mb.phpt index 74e2cda6f070b..040596d40d6e7 100644 --- a/ext/standard/tests/dir/dir_basic-win32-mb.phpt +++ b/ext/standard/tests/dir/dir_basic-win32-mb.phpt @@ -85,5 +85,5 @@ object(Directory)#%d (2) { } Test read after closing the dir: -Directory::read(): %s is not a valid Directory resource +Directory::read(): cannot use Directory resource after it has been closed Done diff --git a/ext/standard/tests/dir/dir_basic.phpt b/ext/standard/tests/dir/dir_basic.phpt index d474122ba243b..17a2b42bcdaea 100644 --- a/ext/standard/tests/dir/dir_basic.phpt +++ b/ext/standard/tests/dir/dir_basic.phpt @@ -79,5 +79,5 @@ object(Directory)#%d (2) { } Test read after closing the dir: -Directory::read(): supplied resource is not a valid Directory resource +Directory::read(): cannot use Directory resource after it has been closed Done diff --git a/ext/standard/tests/dir/rewinddir_variation2-win32-mb.phpt b/ext/standard/tests/dir/rewinddir_variation2-win32-mb.phpt index ab639c7f24f3c..7cb9f2616c5c9 100644 --- a/ext/standard/tests/dir/rewinddir_variation2-win32-mb.phpt +++ b/ext/standard/tests/dir/rewinddir_variation2-win32-mb.phpt @@ -42,4 +42,4 @@ resource(%d) of type (stream) string(%d) "%s" -- Call to rewinddir() -- -rewinddir(): %s is not a valid Directory resource +rewinddir(): Argument #1 ($dir_handle) must be a valid Directory resource diff --git a/ext/standard/tests/dir/rewinddir_variation2.phpt b/ext/standard/tests/dir/rewinddir_variation2.phpt index 654c68cecbe01..16d66da7f8287 100644 --- a/ext/standard/tests/dir/rewinddir_variation2.phpt +++ b/ext/standard/tests/dir/rewinddir_variation2.phpt @@ -36,4 +36,4 @@ resource(%d) of type (stream) string(%d) "%s" -- Call to rewinddir() -- -rewinddir(): supplied resource is not a valid Directory resource +rewinddir(): Argument #1 ($dir_handle) must be a valid Directory resource diff --git a/ext/standard/tests/directory/DirectoryClass_readonly_handle_by_pass_via_ArrayObject.phpt b/ext/standard/tests/directory/DirectoryClass_readonly_handle_by_pass_via_ArrayObject.phpt index cc57fc6933aa4..91416f2c71202 100644 --- a/ext/standard/tests/directory/DirectoryClass_readonly_handle_by_pass_via_ArrayObject.phpt +++ b/ext/standard/tests/directory/DirectoryClass_readonly_handle_by_pass_via_ArrayObject.phpt @@ -30,5 +30,5 @@ try { ?> --EXPECT-- resource(3) of type (stream) -TypeError: Directory::read(): Argument #1 must be a valid Directory resource +Error: Internal directory stream has been altered Error: Typed property Directory::$handle must not be accessed before initialization diff --git a/ext/standard/tests/directory/DirectoryClass_reflection_create_instance_no_construct.phpt b/ext/standard/tests/directory/DirectoryClass_reflection_create_instance_no_construct.phpt index 35b07591635fd..95999581f31c9 100644 --- a/ext/standard/tests/directory/DirectoryClass_reflection_create_instance_no_construct.phpt +++ b/ext/standard/tests/directory/DirectoryClass_reflection_create_instance_no_construct.phpt @@ -27,4 +27,4 @@ object(Directory)#2 (0) { ["handle"]=> uninitialized(mixed) } -Error: Unable to find my handle property +Error: Internal directory stream has been altered From 68665d3cb5dcda5b966fc9edd577581be763ee63 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Mon, 17 Mar 2025 14:23:41 +0000 Subject: [PATCH 14/14] ext/standard/dir.c: Use new PHP_Z_PARAM_STREAM_OR_NULL() ZPP specifier --- ext/standard/dir.c | 35 ++++++++----------- .../dir/closedir_variation2-win32-mb.phpt | 2 +- .../tests/dir/closedir_variation2.phpt | 2 +- .../dir/rewinddir_variation2-win32-mb.phpt | 2 +- .../tests/dir/rewinddir_variation2.phpt | 2 +- 5 files changed, 18 insertions(+), 25 deletions(-) diff --git a/ext/standard/dir.c b/ext/standard/dir.c index 845fcd03eeadc..b468681ac364a 100644 --- a/ext/standard/dir.c +++ b/ext/standard/dir.c @@ -157,25 +157,18 @@ PHP_FUNCTION(dir) /* }}} */ -static php_stream* php_dir_get_directory_stream_from_user_arg(zval *arg) +static php_stream* php_dir_get_directory_stream_from_user_arg(php_stream *dir_stream) { - zend_resource *res; - if (arg == NULL) { + if (dir_stream == NULL) { if (UNEXPECTED(DIRG(default_dir) == NULL)) { zend_type_error("No resource supplied"); return NULL; } - res = DIRG(default_dir); - } else { - ZEND_ASSERT(Z_TYPE_P(arg) == IS_RESOURCE); - res = Z_RES_P(arg); + zend_resource *res = DIRG(default_dir); + ZEND_ASSERT(res->type == php_file_le_stream()); + dir_stream = (php_stream*) res->ptr; } - if (UNEXPECTED(res->type != php_file_le_stream())) { - zend_argument_type_error(1, "must be a valid Directory resource"); - return NULL; - } - php_stream *dir_stream = (php_stream*) res->ptr; if (UNEXPECTED((dir_stream->flags & PHP_STREAM_FLAG_IS_DIR)) == 0) { zend_argument_type_error(1, "must be a valid Directory resource"); return NULL; @@ -209,14 +202,14 @@ static php_stream* php_dir_get_directory_stream_from_this(zval *this_z) /* {{{ Close directory connection identified by the dir_handle */ PHP_FUNCTION(closedir) { - zval *id = NULL; + php_stream *dirp = NULL; ZEND_PARSE_PARAMETERS_START(0, 1) Z_PARAM_OPTIONAL - Z_PARAM_RESOURCE_OR_NULL(id) + PHP_Z_PARAM_STREAM_OR_NULL(dirp) ZEND_PARSE_PARAMETERS_END(); - php_stream *dirp = php_dir_get_directory_stream_from_user_arg(id); + dirp = php_dir_get_directory_stream_from_user_arg(dirp); if (UNEXPECTED(dirp == NULL)) { RETURN_THROWS(); } @@ -249,14 +242,14 @@ PHP_METHOD(Directory, close) /* {{{ Rewind dir_handle back to the start */ PHP_FUNCTION(rewinddir) { - zval *id = NULL; + php_stream *dirp = NULL; ZEND_PARSE_PARAMETERS_START(0, 1) Z_PARAM_OPTIONAL - Z_PARAM_RESOURCE_OR_NULL(id) + PHP_Z_PARAM_STREAM_OR_NULL(dirp) ZEND_PARSE_PARAMETERS_END(); - php_stream *dirp = php_dir_get_directory_stream_from_user_arg(id); + dirp = php_dir_get_directory_stream_from_user_arg(dirp); if (UNEXPECTED(dirp == NULL)) { RETURN_THROWS(); } @@ -280,14 +273,14 @@ PHP_METHOD(Directory, rewind) /* {{{ Read directory entry from dir_handle */ PHP_FUNCTION(readdir) { - zval *id = NULL; + php_stream *dirp = NULL; ZEND_PARSE_PARAMETERS_START(0, 1) Z_PARAM_OPTIONAL - Z_PARAM_RESOURCE_OR_NULL(id) + PHP_Z_PARAM_STREAM_OR_NULL(dirp) ZEND_PARSE_PARAMETERS_END(); - php_stream *dirp = php_dir_get_directory_stream_from_user_arg(id); + dirp = php_dir_get_directory_stream_from_user_arg(dirp); if (UNEXPECTED(dirp == NULL)) { RETURN_THROWS(); } diff --git a/ext/standard/tests/dir/closedir_variation2-win32-mb.phpt b/ext/standard/tests/dir/closedir_variation2-win32-mb.phpt index 08842ae7f7b10..1ada5b7fb36c5 100644 --- a/ext/standard/tests/dir/closedir_variation2-win32-mb.phpt +++ b/ext/standard/tests/dir/closedir_variation2-win32-mb.phpt @@ -47,5 +47,5 @@ NULL Directory Handle: resource(%d) of type (Unknown) -- Close directory handle second time: -- -closedir(): Argument #1 ($dir_handle) must be a valid Directory resource +closedir(): Argument #1 ($dir_handle) must be an open stream resource Directory Handle: resource(%d) of type (Unknown) diff --git a/ext/standard/tests/dir/closedir_variation2.phpt b/ext/standard/tests/dir/closedir_variation2.phpt index e8518c3d3efcf..7898c9ae0ff13 100644 --- a/ext/standard/tests/dir/closedir_variation2.phpt +++ b/ext/standard/tests/dir/closedir_variation2.phpt @@ -41,5 +41,5 @@ NULL Directory Handle: resource(%d) of type (Unknown) -- Close directory handle second time: -- -closedir(): Argument #1 ($dir_handle) must be a valid Directory resource +closedir(): Argument #1 ($dir_handle) must be an open stream resource Directory Handle: resource(%d) of type (Unknown) diff --git a/ext/standard/tests/dir/rewinddir_variation2-win32-mb.phpt b/ext/standard/tests/dir/rewinddir_variation2-win32-mb.phpt index 7cb9f2616c5c9..4bd69b61ff7d6 100644 --- a/ext/standard/tests/dir/rewinddir_variation2-win32-mb.phpt +++ b/ext/standard/tests/dir/rewinddir_variation2-win32-mb.phpt @@ -42,4 +42,4 @@ resource(%d) of type (stream) string(%d) "%s" -- Call to rewinddir() -- -rewinddir(): Argument #1 ($dir_handle) must be a valid Directory resource +rewinddir(): Argument #1 ($dir_handle) must be an open stream resource diff --git a/ext/standard/tests/dir/rewinddir_variation2.phpt b/ext/standard/tests/dir/rewinddir_variation2.phpt index 16d66da7f8287..7ce3c522352d7 100644 --- a/ext/standard/tests/dir/rewinddir_variation2.phpt +++ b/ext/standard/tests/dir/rewinddir_variation2.phpt @@ -36,4 +36,4 @@ resource(%d) of type (stream) string(%d) "%s" -- Call to rewinddir() -- -rewinddir(): Argument #1 ($dir_handle) must be a valid Directory resource +rewinddir(): Argument #1 ($dir_handle) must be an open stream resource