diff options
| author | Dmitry Stogov <dmitry@zend.com> | 2015-03-04 02:05:28 +0300 |
|---|---|---|
| committer | Dmitry Stogov <dmitry@zend.com> | 2015-03-04 02:05:28 +0300 |
| commit | 2fa8d67a5ce59ba9ba6192481e3c2522c3ff5542 (patch) | |
| tree | 9d9d57215f756c387722e74d7d1e1c2de3276a1c /main | |
| parent | 2841aa95db84f3563c94c90f84bf7f47ba159a2d (diff) | |
| download | php-git-2fa8d67a5ce59ba9ba6192481e3c2522c3ff5542.tar.gz | |
Use zend_string* instead of char* for opened_patch handling. Avoid reallocations and improve string reuse.
Diffstat (limited to 'main')
| -rw-r--r-- | main/fopen_wrappers.c | 29 | ||||
| -rw-r--r-- | main/fopen_wrappers.h | 4 | ||||
| -rw-r--r-- | main/main.c | 11 | ||||
| -rw-r--r-- | main/php_ini.c | 16 | ||||
| -rw-r--r-- | main/php_open_temporary_file.c | 20 | ||||
| -rw-r--r-- | main/php_open_temporary_file.h | 6 | ||||
| -rw-r--r-- | main/php_streams.h | 6 | ||||
| -rw-r--r-- | main/rfc1867.c | 35 | ||||
| -rw-r--r-- | main/streams/cast.c | 4 | ||||
| -rw-r--r-- | main/streams/glob_wrapper.c | 4 | ||||
| -rw-r--r-- | main/streams/memory.c | 2 | ||||
| -rw-r--r-- | main/streams/php_stream_plain_wrapper.h | 8 | ||||
| -rw-r--r-- | main/streams/plain_wrapper.c | 54 | ||||
| -rw-r--r-- | main/streams/streams.c | 16 | ||||
| -rw-r--r-- | main/streams/userspace.c | 10 |
15 files changed, 113 insertions, 112 deletions
diff --git a/main/fopen_wrappers.c b/main/fopen_wrappers.c index 9414ebdf36..c516481298 100644 --- a/main/fopen_wrappers.c +++ b/main/fopen_wrappers.c @@ -340,7 +340,7 @@ PHPAPI int php_check_open_basedir_ex(const char *path, int warn) /* {{{ php_fopen_and_set_opened_path */ -static FILE *php_fopen_and_set_opened_path(const char *path, const char *mode, char **opened_path) +static FILE *php_fopen_and_set_opened_path(const char *path, const char *mode, zend_string **opened_path) { FILE *fp; @@ -349,7 +349,12 @@ static FILE *php_fopen_and_set_opened_path(const char *path, const char *mode, c } fp = VCWD_FOPEN(path, mode); if (fp && opened_path) { - *opened_path = expand_filepath_with_mode(path, NULL, NULL, 0, CWD_EXPAND); + //TODO :avoid reallocation + char *tmp = expand_filepath_with_mode(path, NULL, NULL, 0, CWD_EXPAND); + if (tmp) { + *opened_path = zend_string_init(tmp, strlen(tmp), 0); + efree(tmp); + } } return fp; } @@ -361,7 +366,7 @@ PHPAPI int php_fopen_primary_script(zend_file_handle *file_handle) { char *path_info; char *filename = NULL; - char *resolved_path = NULL; + zend_string *resolved_path = NULL; int length; zend_bool orig_display_errors; @@ -448,7 +453,7 @@ PHPAPI int php_fopen_primary_script(zend_file_handle *file_handle) } return FAILURE; } - efree(resolved_path); + zend_string_release(resolved_path); orig_display_errors = PG(display_errors); PG(display_errors) = 0; @@ -481,7 +486,7 @@ PHPAPI int php_fopen_primary_script(zend_file_handle *file_handle) /* {{{ php_resolve_path * Returns the realpath for given filename according to include path */ -PHPAPI char *php_resolve_path(const char *filename, int filename_length, const char *path) +PHPAPI zend_string *php_resolve_path(const char *filename, int filename_length, const char *path) { char resolved_path[MAXPATHLEN]; char trypath[MAXPATHLEN]; @@ -499,7 +504,7 @@ PHPAPI char *php_resolve_path(const char *filename, int filename_length, const c wrapper = php_stream_locate_url_wrapper(filename, &actual_path, STREAM_OPEN_FOR_INCLUDE); if (wrapper == &php_plain_files_wrapper) { if (tsrm_realpath(actual_path, resolved_path)) { - return estrdup(resolved_path); + return zend_string_init(resolved_path, strlen(resolved_path), 0); } } return NULL; @@ -512,7 +517,7 @@ PHPAPI char *php_resolve_path(const char *filename, int filename_length, const c !path || !*path) { if (tsrm_realpath(filename, resolved_path)) { - return estrdup(resolved_path); + return zend_string_init(resolved_path, strlen(resolved_path), 0); } else { return NULL; } @@ -562,14 +567,14 @@ PHPAPI char *php_resolve_path(const char *filename, int filename_length, const c php_stream_statbuf ssb; if (SUCCESS == wrapper->wops->url_stat(wrapper, trypath, 0, &ssb, NULL)) { - return estrdup(trypath); + return zend_string_init(trypath, strlen(trypath), 0); } } continue; } } if (tsrm_realpath(actual_path, resolved_path)) { - return estrdup(resolved_path); + return zend_string_init(resolved_path, strlen(resolved_path), 0); } } /* end provided path */ @@ -598,7 +603,7 @@ PHPAPI char *php_resolve_path(const char *filename, int filename_length, const c php_stream_statbuf ssb; if (SUCCESS == wrapper->wops->url_stat(wrapper, trypath, 0, &ssb, NULL)) { - return estrdup(trypath); + return zend_string_init(trypath, strlen(trypath), 0); } } return NULL; @@ -606,7 +611,7 @@ PHPAPI char *php_resolve_path(const char *filename, int filename_length, const c } if (tsrm_realpath(actual_path, resolved_path)) { - return estrdup(resolved_path); + return zend_string_init(resolved_path, strlen(resolved_path), 0); } } } @@ -619,7 +624,7 @@ PHPAPI char *php_resolve_path(const char *filename, int filename_length, const c * Tries to open a file with a PATH-style list of directories. * If the filename starts with "." or "/", the path is ignored. */ -PHPAPI FILE *php_fopen_with_path(const char *filename, const char *mode, const char *path, char **opened_path) +PHPAPI FILE *php_fopen_with_path(const char *filename, const char *mode, const char *path, zend_string **opened_path) { char *pathbuf, *ptr, *end; const char *exec_fname; diff --git a/main/fopen_wrappers.h b/main/fopen_wrappers.h index e54a499603..e693b03dba 100644 --- a/main/fopen_wrappers.h +++ b/main/fopen_wrappers.h @@ -46,9 +46,9 @@ PHPAPI int php_check_specific_open_basedir(const char *basedir, const char *path PHPAPI int php_check_safe_mode_include_dir(const char *path); -PHPAPI char *php_resolve_path(const char *filename, int filename_len, const char *path); +PHPAPI zend_string *php_resolve_path(const char *filename, int filename_len, const char *path); -PHPAPI FILE *php_fopen_with_path(const char *filename, const char *mode, const char *path, char **opened_path); +PHPAPI FILE *php_fopen_with_path(const char *filename, const char *mode, const char *path, zend_string **opened_path); PHPAPI char *php_strip_url_passwd(char *path); diff --git a/main/main.c b/main/main.c index 3dcba48779..f5c04099ea 100644 --- a/main/main.c +++ b/main/main.c @@ -1350,7 +1350,7 @@ PHP_FUNCTION(set_time_limit) /* {{{ php_fopen_wrapper_for_zend */ -static FILE *php_fopen_wrapper_for_zend(const char *filename, char **opened_path) +static FILE *php_fopen_wrapper_for_zend(const char *filename, zend_string **opened_path) { return php_stream_open_wrapper_as_file((char *)filename, "rb", USE_PATH|IGNORE_URL_WIN|REPORT_ERRORS|STREAM_OPEN_FOR_INCLUDE, opened_path); } @@ -1428,7 +1428,7 @@ PHPAPI int php_stream_open_for_zend_ex(const char *filename, zend_file_handle *h } /* }}} */ -static char *php_resolve_path_for_zend(const char *filename, int filename_len) /* {{{ */ +static zend_string *php_resolve_path_for_zend(const char *filename, int filename_len) /* {{{ */ { return php_resolve_path(filename, filename_len, PG(include_path)); } @@ -2490,12 +2490,9 @@ PHPAPI int php_execute_script(zend_file_handle *primary_file) primary_file->opened_path == NULL && primary_file->type != ZEND_HANDLE_FILENAME ) { - int realfile_len; - if (expand_filepath(primary_file->filename, realfile)) { - realfile_len = (int)strlen(realfile); - zend_hash_str_add_empty_element(&EG(included_files), realfile, realfile_len); - primary_file->opened_path = estrndup(realfile, realfile_len); + primary_file->opened_path = zend_string_init(realfile, strlen(realfile), 0); + zend_hash_add_empty_element(&EG(included_files), primary_file->opened_path); } } diff --git a/main/php_ini.c b/main/php_ini.c index 9c16aa327d..7914244147 100644 --- a/main/php_ini.c +++ b/main/php_ini.c @@ -389,6 +389,7 @@ int php_init_config(void) char *open_basedir; int free_ini_search_path = 0; zend_file_handle fh; + zend_string *opened_path = NULL; zend_hash_init(&configuration_hash, 8, NULL, config_zval_dtor, 1); @@ -555,7 +556,8 @@ int php_init_config(void) if (!((statbuf.st_mode & S_IFMT) == S_IFDIR)) { fh.handle.fp = VCWD_FOPEN(php_ini_file_name, "r"); if (fh.handle.fp) { - fh.filename = php_ini_opened_path = expand_filepath(php_ini_file_name, NULL); + fh.filename = expand_filepath(php_ini_file_name, NULL); + opened_path = zend_string_init(fh.filename, strlen(fh.filename), 0); } } } @@ -566,18 +568,18 @@ int php_init_config(void) const char *fmt = "php-%s.ini"; char *ini_fname; spprintf(&ini_fname, 0, fmt, sapi_module.name); - fh.handle.fp = php_fopen_with_path(ini_fname, "r", php_ini_search_path, &php_ini_opened_path); + fh.handle.fp = php_fopen_with_path(ini_fname, "r", php_ini_search_path, &opened_path); efree(ini_fname); if (fh.handle.fp) { - fh.filename = php_ini_opened_path; + fh.filename = opened_path->val; } } /* If still no ini file found, search for php.ini file in search path */ if (!fh.handle.fp) { - fh.handle.fp = php_fopen_with_path("php.ini", "r", php_ini_search_path, &php_ini_opened_path); + fh.handle.fp = php_fopen_with_path("php.ini", "r", php_ini_search_path, &opened_path); if (fh.handle.fp) { - fh.filename = php_ini_opened_path; + fh.filename = opened_path->val; } } } @@ -599,8 +601,8 @@ int php_init_config(void) ZVAL_NEW_STR(&tmp, zend_string_init(fh.filename, strlen(fh.filename), 1)); zend_hash_str_update(&configuration_hash, "cfg_file_path", sizeof("cfg_file_path")-1, &tmp); - if (php_ini_opened_path) { - efree(php_ini_opened_path); + if (opened_path) { + zend_string_release(opened_path); } php_ini_opened_path = zend_strndup(Z_STRVAL(tmp), Z_STRLEN(tmp)); } diff --git a/main/php_open_temporary_file.c b/main/php_open_temporary_file.c index 9bb5bd77ee..a88c823eed 100644 --- a/main/php_open_temporary_file.c +++ b/main/php_open_temporary_file.c @@ -94,10 +94,10 @@ * SUCH DAMAGE. */ -static int php_do_open_temporary_file(const char *path, const char *pfx, char **opened_path_p) +static int php_do_open_temporary_file(const char *path, const char *pfx, zend_string **opened_path_p) { char *trailing_slash; - char *opened_path; + char opened_path[MAXPATHLEN]; char cwd[MAXPATHLEN]; cwd_state new_state; int fd = -1; @@ -138,8 +138,7 @@ static int php_do_open_temporary_file(const char *path, const char *pfx, char ** trailing_slash = "/"; } - if (spprintf(&opened_path, 0, "%s%s%sXXXXXX", new_state.cwd, trailing_slash, pfx) >= MAXPATHLEN) { - efree(opened_path); + if (snprintf(opened_path, MAXPATHLEN, "%s%s%sXXXXXX", new_state.cwd, trailing_slash, pfx) >= MAXPATHLEN) { efree(new_state.cwd); return -1; } @@ -150,7 +149,6 @@ static int php_do_open_temporary_file(const char *path, const char *pfx, char ** /* Some versions of windows set the temp file to be read-only, * which means that opening it will fail... */ if (VCWD_CHMOD(opened_path, 0600)) { - efree(opened_path); efree(new_state.cwd); return -1; } @@ -165,10 +163,8 @@ static int php_do_open_temporary_file(const char *path, const char *pfx, char ** } #endif - if (fd == -1 || !opened_path_p) { - efree(opened_path); - } else { - *opened_path_p = opened_path; + if (fd != -1 && opened_path_p) { + *opened_path_p = zend_string_init(opened_path, strlen(opened_path), 0); } efree(new_state.cwd); return fd; @@ -264,7 +260,7 @@ PHPAPI const char* php_get_temporary_directory(void) * This function should do its best to return a file pointer to a newly created * unique file, on every platform. */ -PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, char **opened_path_p, zend_bool open_basedir_check) +PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, zend_string **opened_path_p, zend_bool open_basedir_check) { int fd; const char *temp_dir; @@ -296,12 +292,12 @@ def_tmp: return fd; } -PHPAPI int php_open_temporary_fd(const char *dir, const char *pfx, char **opened_path_p) +PHPAPI int php_open_temporary_fd(const char *dir, const char *pfx, zend_string **opened_path_p) { return php_open_temporary_fd_ex(dir, pfx, opened_path_p, 0); } -PHPAPI FILE *php_open_temporary_file(const char *dir, const char *pfx, char **opened_path_p) +PHPAPI FILE *php_open_temporary_file(const char *dir, const char *pfx, zend_string **opened_path_p) { FILE *fp; int fd = php_open_temporary_fd(dir, pfx, opened_path_p); diff --git a/main/php_open_temporary_file.h b/main/php_open_temporary_file.h index e63cc599f0..912f4a8adc 100644 --- a/main/php_open_temporary_file.h +++ b/main/php_open_temporary_file.h @@ -22,9 +22,9 @@ #define PHP_OPEN_TEMPORARY_FILE_H BEGIN_EXTERN_C() -PHPAPI FILE *php_open_temporary_file(const char *dir, const char *pfx, char **opened_path_p); -PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, char **opened_path_p, zend_bool open_basedir_check); -PHPAPI int php_open_temporary_fd(const char *dir, const char *pfx, char **opened_path_p); +PHPAPI FILE *php_open_temporary_file(const char *dir, const char *pfx, zend_string **opened_path_p); +PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, zend_string **opened_path_p, zend_bool open_basedir_check); +PHPAPI int php_open_temporary_fd(const char *dir, const char *pfx, zend_string **opened_path_p); PHPAPI const char *php_get_temporary_directory(void); PHPAPI void php_shutdown_temporary_directory(void); END_EXTERN_C() diff --git a/main/php_streams.h b/main/php_streams.h index 3cd0c047cd..3035bd560b 100644 --- a/main/php_streams.h +++ b/main/php_streams.h @@ -134,7 +134,7 @@ typedef struct _php_stream_ops { typedef struct _php_stream_wrapper_ops { /* open/create a wrapped stream */ php_stream *(*stream_opener)(php_stream_wrapper *wrapper, const char *filename, const char *mode, - int options, char **opened_path, php_stream_context *context STREAMS_DC); + int options, zend_string **opened_path, php_stream_context *context STREAMS_DC); /* close/destroy a wrapped stream */ int (*stream_closer)(php_stream_wrapper *wrapper, php_stream *stream); /* stat a wrapped stream */ @@ -143,7 +143,7 @@ typedef struct _php_stream_wrapper_ops { int (*url_stat)(php_stream_wrapper *wrapper, const char *url, int flags, php_stream_statbuf *ssb, php_stream_context *context); /* open a "directory" stream */ php_stream *(*dir_opener)(php_stream_wrapper *wrapper, const char *filename, const char *mode, - int options, char **opened_path, php_stream_context *context STREAMS_DC); + int options, zend_string **opened_path, php_stream_context *context STREAMS_DC); const char *label; @@ -554,7 +554,7 @@ PHPAPI int php_register_url_stream_wrapper(const char *protocol, php_stream_wrap PHPAPI int php_unregister_url_stream_wrapper(const char *protocol); PHPAPI int php_register_url_stream_wrapper_volatile(const char *protocol, php_stream_wrapper *wrapper); PHPAPI int php_unregister_url_stream_wrapper_volatile(const char *protocol); -PHPAPI php_stream *_php_stream_open_wrapper_ex(const char *path, const char *mode, int options, char **opened_path, php_stream_context *context STREAMS_DC); +PHPAPI php_stream *_php_stream_open_wrapper_ex(const char *path, const char *mode, int options, zend_string **opened_path, php_stream_context *context STREAMS_DC); PHPAPI php_stream_wrapper *php_stream_locate_url_wrapper(const char *path, const char **path_for_open, int options); PHPAPI const char *php_stream_locate_eol(php_stream *stream, zend_string *buf); diff --git a/main/rfc1867.c b/main/rfc1867.c index a1d8232dd9..493f70b65e 100644 --- a/main/rfc1867.c +++ b/main/rfc1867.c @@ -191,16 +191,16 @@ static void register_http_post_files_variable_ex(char *var, zval *val, zval *htt static int unlink_filename(zval *el) /* {{{ */ { - char *filename = (char*)Z_PTR_P(el); - VCWD_UNLINK(filename); + zend_string *filename = Z_STR_P(el); + VCWD_UNLINK(filename->val); return 0; } /* }}} */ static void free_filename(zval *el) { - char *filename = (char*)Z_PTR_P(el); - efree(filename); + zend_string *filename = Z_STR_P(el); + zend_string_release(filename); } void destroy_uploaded_files_hash(void) /* {{{ */ @@ -685,7 +685,8 @@ static char *multipart_buffer_read_body(multipart_buffer *self, size_t *len) SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */ { char *boundary, *s = NULL, *boundary_end = NULL, *start_arr = NULL, *array_index = NULL; - char *temp_filename = NULL, *lbuf = NULL, *abuf = NULL; + char *lbuf = NULL, *abuf = NULL; + zend_string *temp_filename = NULL; int boundary_len = 0, cancel_upload = 0, is_arr_upload = 0, array_len = 0; int64_t total_bytes = 0, max_file_size = 0; int skip_upload = 0, anonindex = 0, is_anonymous; @@ -980,7 +981,7 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */ event_file_start.name = param; event_file_start.filename = &filename; if (php_rfc1867_callback(MULTIPART_EVENT_FILE_START, &event_file_start, &event_extra_data) == FAILURE) { - temp_filename = ""; + temp_filename = NULL; efree(param); efree(filename); continue; @@ -1095,7 +1096,7 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */ multipart_event_file_end event_file_end; event_file_end.post_bytes_processed = SG(read_post_bytes); - event_file_end.temp_filename = temp_filename; + event_file_end.temp_filename = temp_filename->val; event_file_end.cancel_upload = cancel_upload; if (php_rfc1867_callback(MULTIPART_EVENT_FILE_END, &event_file_end, &event_extra_data) == FAILURE) { cancel_upload = UPLOAD_ERROR_X; @@ -1105,13 +1106,13 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */ if (cancel_upload) { if (temp_filename) { if (cancel_upload != UPLOAD_ERROR_E) { /* file creation failed */ - unlink(temp_filename); + unlink(temp_filename->val); } - efree(temp_filename); + zend_string_release(temp_filename); } - temp_filename = ""; + temp_filename = NULL; } else { - zend_hash_str_add_ptr(SG(rfc1867_uploaded_files), temp_filename, strlen(temp_filename), temp_filename); + zend_hash_add_ptr(SG(rfc1867_uploaded_files), temp_filename, temp_filename); } /* is_arr_upload is true when name of file upload field @@ -1211,7 +1212,11 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */ /* if param is of form xxx[.*] this will cut it to xxx */ if (!is_anonymous) { - ZVAL_STRING(&zfilename, temp_filename); + if (temp_filename) { + ZVAL_STR_COPY(&zfilename, temp_filename); + } else { + ZVAL_EMPTY_STRING(&zfilename); + } safe_php_register_variable_ex(param, &zfilename, NULL, 1); } @@ -1222,7 +1227,11 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */ snprintf(lbuf, llen, "%s[tmp_name]", param); } add_protected_variable(lbuf); - ZVAL_STRING(&zfilename, temp_filename); + if (temp_filename) { + ZVAL_STR_COPY(&zfilename, temp_filename); + } else { + ZVAL_EMPTY_STRING(&zfilename); + } register_http_post_files_variable_ex(lbuf, &zfilename, &PG(http_globals)[TRACK_VARS_FILES], 1); } diff --git a/main/streams/cast.c b/main/streams/cast.c index da8c6f1de8..bc9cc805e8 100644 --- a/main/streams/cast.c +++ b/main/streams/cast.c @@ -342,7 +342,7 @@ exit_success: /* }}} */ /* {{{ php_stream_open_wrapper_as_file */ -PHPAPI FILE * _php_stream_open_wrapper_as_file(char *path, char *mode, int options, char **opened_path STREAMS_DC) +PHPAPI FILE * _php_stream_open_wrapper_as_file(char *path, char *mode, int options, zend_string **opened_path STREAMS_DC) { FILE *fp = NULL; php_stream *stream = NULL; @@ -356,7 +356,7 @@ PHPAPI FILE * _php_stream_open_wrapper_as_file(char *path, char *mode, int optio if (php_stream_cast(stream, PHP_STREAM_AS_STDIO|PHP_STREAM_CAST_TRY_HARD|PHP_STREAM_CAST_RELEASE, (void**)&fp, REPORT_ERRORS) == FAILURE) { php_stream_close(stream); if (opened_path && *opened_path) { - efree(*opened_path); + zend_string_release(*opened_path); } return NULL; } diff --git a/main/streams/glob_wrapper.c b/main/streams/glob_wrapper.c index 3926fc6540..4dffc27ade 100644 --- a/main/streams/glob_wrapper.c +++ b/main/streams/glob_wrapper.c @@ -207,7 +207,7 @@ php_stream_ops php_glob_stream_ops = { /* {{{ php_glob_stream_opener */ static php_stream *php_glob_stream_opener(php_stream_wrapper *wrapper, const char *path, const char *mode, - int options, char **opened_path, php_stream_context *context STREAMS_DC) + int options, zend_string **opened_path, php_stream_context *context STREAMS_DC) { glob_s_t *pglob; int ret; @@ -220,7 +220,7 @@ static php_stream *php_glob_stream_opener(php_stream_wrapper *wrapper, const cha if (!strncmp(path, "glob://", sizeof("glob://")-1)) { path += sizeof("glob://")-1; if (opened_path) { - *opened_path = estrdup(path); + *opened_path = zend_string_init(path, strlen(path), 0); } } diff --git a/main/streams/memory.c b/main/streams/memory.c index 2016a9898c..9a9b11fe07 100644 --- a/main/streams/memory.c +++ b/main/streams/memory.c @@ -619,7 +619,7 @@ PHPAPI php_stream_ops php_stream_rfc2397_ops = { }; static php_stream * php_stream_url_wrap_rfc2397(php_stream_wrapper *wrapper, const char *path, - const char *mode, int options, char **opened_path, + const char *mode, int options, zend_string **opened_path, php_stream_context *context STREAMS_DC) /* {{{ */ { php_stream *stream; diff --git a/main/streams/php_stream_plain_wrapper.h b/main/streams/php_stream_plain_wrapper.h index cdf91084ab..61fc09542b 100644 --- a/main/streams/php_stream_plain_wrapper.h +++ b/main/streams/php_stream_plain_wrapper.h @@ -27,10 +27,10 @@ PHPAPI extern php_stream_wrapper php_plain_files_wrapper; BEGIN_EXTERN_C() /* like fopen, but returns a stream */ -PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, char **opened_path, int options STREAMS_DC); +PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, zend_string **opened_path, int options STREAMS_DC); #define php_stream_fopen(filename, mode, opened) _php_stream_fopen((filename), (mode), (opened), 0 STREAMS_CC) -PHPAPI php_stream *_php_stream_fopen_with_path(const char *filename, const char *mode, const char *path, char **opened_path, int options STREAMS_DC); +PHPAPI php_stream *_php_stream_fopen_with_path(const char *filename, const char *mode, const char *path, zend_string **opened_path, int options STREAMS_DC); #define php_stream_fopen_with_path(filename, mode, path, opened) _php_stream_fopen_with_path((filename), (mode), (path), (opened), 0 STREAMS_CC) PHPAPI php_stream *_php_stream_fopen_from_file(FILE *file, const char *mode STREAMS_DC); @@ -45,13 +45,13 @@ PHPAPI php_stream *_php_stream_fopen_from_pipe(FILE *file, const char *mode STRE PHPAPI php_stream *_php_stream_fopen_tmpfile(int dummy STREAMS_DC); #define php_stream_fopen_tmpfile() _php_stream_fopen_tmpfile(0 STREAMS_CC) -PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char *pfx, char **opened_path STREAMS_DC); +PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char *pfx, zend_string **opened_path STREAMS_DC); #define php_stream_fopen_temporary_file(dir, pfx, opened_path) _php_stream_fopen_temporary_file((dir), (pfx), (opened_path) STREAMS_CC) /* This is a utility API for extensions that are opening a stream, converting it * to a FILE* and then closing it again. Be warned that fileno() on the result * will most likely fail on systems with fopencookie. */ -PHPAPI FILE * _php_stream_open_wrapper_as_file(char * path, char * mode, int options, char **opened_path STREAMS_DC); +PHPAPI FILE * _php_stream_open_wrapper_as_file(char * path, char * mode, int options, zend_string **opened_path STREAMS_DC); #define php_stream_open_wrapper_as_file(path, mode, options, opened_path) _php_stream_open_wrapper_as_file((path), (mode), (options), (opened_path) STREAMS_CC) END_EXTERN_C() diff --git a/main/streams/plain_wrapper.c b/main/streams/plain_wrapper.c index fe256b767e..83bd059c34 100644 --- a/main/streams/plain_wrapper.c +++ b/main/streams/plain_wrapper.c @@ -124,7 +124,7 @@ typedef struct { unsigned _reserved:29; int lock_flag; /* stores the lock state */ - char *temp_file_name; /* if non-null, this is the path to a temporary file that + zend_string *temp_name; /* if non-null, this is the path to a temporary file that * is to be deleted when the stream is closed */ #if HAVE_FLUSHIO char last_op; @@ -168,7 +168,7 @@ static php_stream *_php_stream_fopen_from_fd_int(int fd, const char *mode, const self->is_pipe = 0; self->lock_flag = LOCK_UN; self->is_process_pipe = 0; - self->temp_file_name = NULL; + self->temp_name = NULL; self->fd = fd; return php_stream_alloc_rel(&php_stream_stdio_ops, self, persistent_id, mode); @@ -184,15 +184,15 @@ static php_stream *_php_stream_fopen_from_file_int(FILE *file, const char *mode self->is_pipe = 0; self->lock_flag = LOCK_UN; self->is_process_pipe = 0; - self->temp_file_name = NULL; + self->temp_name = NULL; self->fd = fileno(file); return php_stream_alloc_rel(&php_stream_stdio_ops, self, 0, mode); } -PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char *pfx, char **opened_path_ptr STREAMS_DC) +PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char *pfx, zend_string **opened_path_ptr STREAMS_DC) { - char *opened_path = NULL; + zend_string *opened_path = NULL; int fd; fd = php_open_temporary_fd(dir, pfx, &opened_path); @@ -207,9 +207,9 @@ PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char if (stream) { php_stdio_stream_data *self = (php_stdio_stream_data*)stream->abstract; stream->wrapper = &php_plain_files_wrapper; - stream->orig_path = estrdup(opened_path); + stream->orig_path = estrndup(opened_path->val, opened_path->len); - self->temp_file_name = opened_path; + self->temp_name = opened_path; self->lock_flag = LOCK_UN; return stream; @@ -311,7 +311,7 @@ PHPAPI php_stream *_php_stream_fopen_from_pipe(FILE *file, const char *mode STRE self->lock_flag = LOCK_UN; self->is_process_pipe = 1; self->fd = fileno(file); - self->temp_file_name = NULL; + self->temp_name = NULL; stream = php_stream_alloc_rel(&php_stream_stdio_ops, self, 0, mode); stream->flags |= PHP_STREAM_FLAG_NO_SEEK; @@ -450,11 +450,11 @@ static int php_stdiop_close(php_stream *stream, int close_handle) } else { return 0; /* everything should be closed already -> success */ } - if (data->temp_file_name) { - unlink(data->temp_file_name); + if (data->temp_name) { + unlink(data->temp_name->val); /* temporary streams are never persistent */ - efree(data->temp_file_name); - data->temp_file_name = NULL; + zend_string_release(data->temp_name); + data->temp_name = NULL; } } else { ret = 0; @@ -890,7 +890,7 @@ static php_stream_ops php_plain_files_dirstream_ops = { }; static php_stream *php_plain_files_dir_opener(php_stream_wrapper *wrapper, const char *path, const char *mode, - int options, char **opened_path, php_stream_context *context STREAMS_DC) + int options, zend_string **opened_path, php_stream_context *context STREAMS_DC) { DIR *dir = NULL; php_stream *stream = NULL; @@ -928,9 +928,9 @@ static php_stream *php_plain_files_dir_opener(php_stream_wrapper *wrapper, const /* }}} */ /* {{{ php_stream_fopen */ -PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, char **opened_path, int options STREAMS_DC) +PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, zend_string **opened_path, int options STREAMS_DC) { - char *realpath = NULL; + char realpath[MAXPATHLEN]; int open_flags; int fd; php_stream *ret; @@ -945,9 +945,9 @@ PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, cha } if (options & STREAM_ASSUME_REALPATH) { - realpath = estrdup(filename); + strlcpy(realpath, filename, sizeof(realpath)); } else { - if ((realpath = expand_filepath(filename, NULL)) == NULL) { + if (expand_filepath(filename, realpath) == NULL) { return NULL; } } @@ -957,15 +957,12 @@ PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, cha switch (php_stream_from_persistent_id(persistent_id, &ret)) { case PHP_STREAM_PERSISTENT_SUCCESS: if (opened_path) { - *opened_path = realpath; - realpath = NULL; + //TODO: avoid reallocation??? + *opened_path = zend_string_init(realpath, strlen(realpath), 0); } /* fall through */ case PHP_STREAM_PERSISTENT_FAILURE: - if (realpath) { - efree(realpath); - } efree(persistent_id);; return ret; } @@ -983,11 +980,7 @@ PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, cha if (ret) { if (opened_path) { - *opened_path = realpath; - realpath = NULL; - } - if (realpath) { - efree(realpath); + *opened_path = zend_string_init(realpath, strlen(realpath), 0); } if (persistent_id) { efree(persistent_id); @@ -1005,7 +998,7 @@ PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, cha r = do_fstat(self, 0); if ((r == 0 && !S_ISREG(self->sb.st_mode))) { if (opened_path) { - efree(*opened_path); + zend_string_release(*opened_path); *opened_path = NULL; } php_stream_close(ret); @@ -1018,7 +1011,6 @@ PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, cha } close(fd); } - efree(realpath); if (persistent_id) { efree(persistent_id); } @@ -1028,7 +1020,7 @@ PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, cha static php_stream *php_plain_files_stream_opener(php_stream_wrapper *wrapper, const char *path, const char *mode, - int options, char **opened_path, php_stream_context *context STREAMS_DC) + int options, zend_string **opened_path, php_stream_context *context STREAMS_DC) { if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(path)) { return NULL; @@ -1393,7 +1385,7 @@ PHPAPI php_stream_wrapper php_plain_files_wrapper = { }; /* {{{ php_stream_fopen_with_path */ -PHPAPI php_stream *_php_stream_fopen_with_path(const char *filename, const char *mode, const char *path, char **opened_path, int options STREAMS_DC) +PHPAPI php_stream *_php_stream_fopen_with_path(const char *filename, const char *mode, const char *path, zend_string **opened_path, int options STREAMS_DC) { /* code ripped off from fopen_wrappers.c */ char *pathbuf, *end; diff --git a/main/streams/streams.c b/main/streams/streams.c index 7821c44c2e..16149fbc92 100644 --- a/main/streams/streams.c +++ b/main/streams/streams.c @@ -2015,13 +2015,13 @@ PHPAPI php_stream_dirent *_php_stream_readdir(php_stream *dirstream, php_stream_ /* {{{ php_stream_open_wrapper_ex */ PHPAPI php_stream *_php_stream_open_wrapper_ex(const char *path, const char *mode, int options, - char **opened_path, php_stream_context *context STREAMS_DC) + zend_string **opened_path, php_stream_context *context STREAMS_DC) { php_stream *stream = NULL; php_stream_wrapper *wrapper = NULL; const char *path_to_open; int persistent = options & STREAM_OPEN_PERSISTENT; - char *resolved_path = NULL; + zend_string *resolved_path = NULL; char *copy_of_path = NULL; if (opened_path) { @@ -2036,7 +2036,7 @@ PHPAPI php_stream *_php_stream_open_wrapper_ex(const char *path, const char *mod if (options & USE_PATH) { resolved_path = zend_resolve_path(path, (int)strlen(path)); if (resolved_path) { - path = resolved_path; + path = resolved_path->val; /* we've found this file, don't re-check include_path or run realpath */ options |= STREAM_ASSUME_REALPATH; options &= ~USE_PATH; @@ -2049,7 +2049,7 @@ PHPAPI php_stream *_php_stream_open_wrapper_ex(const char *path, const char *mod if (options & STREAM_USE_URL && (!wrapper || !wrapper->is_url)) { php_error_docref(NULL, E_WARNING, "This function may only be used against URLs"); if (resolved_path) { - efree(resolved_path); + zend_string_release(resolved_path); } return NULL; } @@ -2102,7 +2102,7 @@ PHPAPI php_stream *_php_stream_open_wrapper_ex(const char *path, const char *mod ? PHP_STREAM_PREFER_STDIO : PHP_STREAM_NO_PREFERENCE)) { case PHP_STREAM_UNCHANGED: if (resolved_path) { - efree(resolved_path); + zend_string_release(resolved_path); } return stream; case PHP_STREAM_RELEASED: @@ -2111,7 +2111,7 @@ PHPAPI php_stream *_php_stream_open_wrapper_ex(const char *path, const char *mod } newstream->orig_path = pestrdup(path, persistent); if (resolved_path) { - efree(resolved_path); + zend_string_release(resolved_path); } return newstream; default: @@ -2141,7 +2141,7 @@ PHPAPI php_stream *_php_stream_open_wrapper_ex(const char *path, const char *mod if (stream == NULL && (options & REPORT_ERRORS)) { php_stream_display_wrapper_errors(wrapper, path, "failed to open stream"); if (opened_path && *opened_path) { - efree(*opened_path); + zend_string_release(*opened_path); *opened_path = NULL; } } @@ -2152,7 +2152,7 @@ PHPAPI php_stream *_php_stream_open_wrapper_ex(const char *path, const char *mod } #endif if (resolved_path) { - efree(resolved_path); + zend_string_release(resolved_path); } return stream; } diff --git a/main/streams/userspace.c b/main/streams/userspace.c index 8d40ec3e12..33dbbf8d67 100644 --- a/main/streams/userspace.c +++ b/main/streams/userspace.c @@ -45,7 +45,7 @@ struct php_user_stream_wrapper { php_stream_wrapper wrapper; }; -static php_stream *user_wrapper_opener(php_stream_wrapper *wrapper, const char *filename, const char *mode, int options, char **opened_path, php_stream_context *context STREAMS_DC); +static php_stream *user_wrapper_opener(php_stream_wrapper *wrapper, const char *filename, const char *mode, int options, zend_string **opened_path, php_stream_context *context STREAMS_DC); static int user_wrapper_stat_url(php_stream_wrapper *wrapper, const char *url, int flags, php_stream_statbuf *ssb, php_stream_context *context); static int user_wrapper_unlink(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context); static int user_wrapper_rename(php_stream_wrapper *wrapper, const char *url_from, const char *url_to, int options, php_stream_context *context); @@ -53,7 +53,7 @@ static int user_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url, int static int user_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context); static int user_wrapper_metadata(php_stream_wrapper *wrapper, const char *url, int option, void *value, php_stream_context *context); static php_stream *user_wrapper_opendir(php_stream_wrapper *wrapper, const char *filename, const char *mode, - int options, char **opened_path, php_stream_context *context STREAMS_DC); + int options, zend_string **opened_path, php_stream_context *context STREAMS_DC); static php_stream_wrapper_ops user_stream_wops = { user_wrapper_opener, @@ -325,7 +325,7 @@ static void user_stream_create_object(struct php_user_stream_wrapper *uwrap, php } static php_stream *user_wrapper_opener(php_stream_wrapper *wrapper, const char *filename, const char *mode, - int options, char **opened_path, php_stream_context *context STREAMS_DC) + int options, zend_string **opened_path, php_stream_context *context STREAMS_DC) { struct php_user_stream_wrapper *uwrap = (struct php_user_stream_wrapper*)wrapper->abstract; php_userstream_data_t *us; @@ -385,7 +385,7 @@ static php_stream *user_wrapper_opener(php_stream_wrapper *wrapper, const char * /* if the opened path is set, copy it out */ if (Z_ISREF(args[3]) && Z_TYPE_P(Z_REFVAL(args[3])) == IS_STRING && opened_path) { - *opened_path = estrndup(Z_STRVAL_P(Z_REFVAL(args[3])), Z_STRLEN_P(Z_REFVAL(args[3]))); + *opened_path = zend_string_copy(Z_STR_P(Z_REFVAL(args[3]))); } /* set wrapper data to be a reference to our object */ @@ -415,7 +415,7 @@ static php_stream *user_wrapper_opener(php_stream_wrapper *wrapper, const char * } static php_stream *user_wrapper_opendir(php_stream_wrapper *wrapper, const char *filename, const char *mode, - int options, char **opened_path, php_stream_context *context STREAMS_DC) + int options, zend_string **opened_path, php_stream_context *context STREAMS_DC) { struct php_user_stream_wrapper *uwrap = (struct php_user_stream_wrapper*)wrapper->abstract; php_userstream_data_t *us; |
