From 9053800e85b5a63c87638edb069065f0b66ea0fc Mon Sep 17 00:00:00 2001 From: Stefan Neufeind Date: Sat, 25 Jan 2014 10:02:11 +0100 Subject: [PATCH 1/3] Add suffix to tempnam() Implementation of this RFC: https://wiki.php.net/rfc/tempnam-suffix --- ext/standard/basic_functions.c | 3 +- ext/standard/file.c | 10 +-- ext/standard/tests/file/tempnam_suffix.phpt | 81 +++++++++++++++++++++ main/php_open_temporary_file.c | 20 ++--- main/php_open_temporary_file.h | 6 +- main/rfc1867.c | 2 +- main/streams/php_stream_plain_wrapper.h | 4 +- main/streams/plain_wrapper.c | 6 +- 8 files changed, 107 insertions(+), 25 deletions(-) create mode 100644 ext/standard/tests/file/tempnam_suffix.phpt diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 6409ff37d8556..a352d79446c41 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -1094,9 +1094,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_file, 0, 0, 1) ZEND_ARG_INFO(0, context) ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_INFO(arginfo_tempnam, 0) +ZEND_BEGIN_ARG_INFO_EX(arginfo_tempnam, 0, 0, 2) ZEND_ARG_INFO(0, dir) ZEND_ARG_INFO(0, prefix) + ZEND_ARG_INFO(0, suffix) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO(arginfo_tmpfile, 0) diff --git a/ext/standard/file.c b/ext/standard/file.c index 0dab6f284a8a6..e8c06422c4e35 100644 --- a/ext/standard/file.c +++ b/ext/standard/file.c @@ -799,18 +799,18 @@ PHP_FUNCTION(file) } /* }}} */ -/* {{{ proto string tempnam(string dir, string prefix) +/* {{{ proto string tempnam(string dir, string prefix [, string suffix]) Create a unique filename in a directory */ PHP_FUNCTION(tempnam) { - char *dir, *prefix; - int dir_len, prefix_len; + char *dir, *prefix, *suffix = ""; + int dir_len, prefix_len, suffix_len = 0; size_t p_len; char *opened_path; char *p; int fd; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ps", &dir, &dir_len, &prefix, &prefix_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ps|s", &dir, &dir_len, &prefix, &prefix_len, &suffix, &suffix_len) == FAILURE) { return; } @@ -825,7 +825,7 @@ PHP_FUNCTION(tempnam) RETVAL_FALSE; - if ((fd = php_open_temporary_fd_ex(dir, p, &opened_path, 1 TSRMLS_CC)) >= 0) { + if ((fd = php_open_temporary_fd_ex(dir, p, &opened_path, suffix, 1 TSRMLS_CC)) >= 0) { close(fd); RETVAL_STRING(opened_path, 0); } diff --git a/ext/standard/tests/file/tempnam_suffix.phpt b/ext/standard/tests/file/tempnam_suffix.phpt new file mode 100644 index 0000000000000..5bbac6e066135 --- /dev/null +++ b/ext/standard/tests/file/tempnam_suffix.phpt @@ -0,0 +1,81 @@ +--TEST-- +Test tempnam() function: usage variations - specify a suffix +--SKIPIF-- + +--FILE-- + "; + print($file_name); + echo "\n"; + + echo "File permissions are => "; + printf("%o", fileperms($file_name) ); + echo "\n"; + + echo "File created in => "; + $file_dir = dirname($file_name); + + if ($file_dir == sys_get_temp_dir()) { + echo "temp dir\n"; + } + else if ($file_dir == $file_path) { + echo "directory specified\n"; + } + else { + echo "unknown location\n"; + } + + } + else { + echo "-- File is not created --\n"; + } + + unlink($file_name); +} + +rmdir($file_path); +echo "\n*** Done ***\n"; +?> +--EXPECTF-- +*** Testing tempnam() with obscure suffixes *** +-- Iteration 0 -- +File name is => %s/prefix%s +File permissions are => 100600 +File created in => directory specified +-- Iteration 1 -- +File name is => %s/prefix%s +File permissions are => 100600 +File created in => directory specified +-- Iteration 2 -- +File name is => %s/prefix%s.png +File permissions are => 100600 +File created in => directory specified + +*** Done *** + diff --git a/main/php_open_temporary_file.c b/main/php_open_temporary_file.c index ebe5350ef26ca..67d158e0d7934 100644 --- a/main/php_open_temporary_file.c +++ b/main/php_open_temporary_file.c @@ -94,7 +94,7 @@ * SUCH DAMAGE. */ -static int php_do_open_temporary_file(const char *path, const char *pfx, char **opened_path_p TSRMLS_DC) +static int php_do_open_temporary_file(const char *path, const char *pfx, const char *suffix, char **opened_path_p TSRMLS_DC) { char *trailing_slash; char *opened_path; @@ -138,7 +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) { + if (spprintf(&opened_path, 0, "%s%s%sXXXXXX%s", new_state.cwd, trailing_slash, pfx, suffix) >= MAXPATHLEN) { efree(opened_path); efree(new_state.cwd); return -1; @@ -158,7 +158,7 @@ static int php_do_open_temporary_file(const char *path, const char *pfx, char ** } #elif defined(HAVE_MKSTEMP) - fd = mkstemp(opened_path); + fd = mkstemps(opened_path, strlen(suffix)); #else if (mktemp(opened_path)) { fd = VCWD_OPEN(opened_path, open_flags); @@ -264,7 +264,7 @@ PHPAPI const char* php_get_temporary_directory(TSRMLS_D) * 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 TSRMLS_DC) +PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, char **opened_path_p, const char *suffix, zend_bool open_basedir_check TSRMLS_DC) { int fd; const char *temp_dir; @@ -281,14 +281,14 @@ PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, char **ope temp_dir = php_get_temporary_directory(TSRMLS_C); if (temp_dir && *temp_dir != '\0' && (!open_basedir_check || !php_check_open_basedir(temp_dir TSRMLS_CC))) { - return php_do_open_temporary_file(temp_dir, pfx, opened_path_p TSRMLS_CC); + return php_do_open_temporary_file(temp_dir, pfx, suffix, opened_path_p TSRMLS_CC); } else { return -1; } } /* Try the directory given as parameter. */ - fd = php_do_open_temporary_file(dir, pfx, opened_path_p TSRMLS_CC); + fd = php_do_open_temporary_file(dir, pfx, suffix, opened_path_p TSRMLS_CC); if (fd == -1) { /* Use default temporary directory. */ goto def_tmp; @@ -296,15 +296,15 @@ PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, char **ope return fd; } -PHPAPI int php_open_temporary_fd(const char *dir, const char *pfx, char **opened_path_p TSRMLS_DC) +PHPAPI int php_open_temporary_fd(const char *dir, const char *pfx, const char *suffix, char **opened_path_p TSRMLS_DC) { - return php_open_temporary_fd_ex(dir, pfx, opened_path_p, 0 TSRMLS_CC); + return php_open_temporary_fd_ex(dir, pfx, opened_path_p, suffix, 0 TSRMLS_CC); } -PHPAPI FILE *php_open_temporary_file(const char *dir, const char *pfx, char **opened_path_p TSRMLS_DC) +PHPAPI FILE *php_open_temporary_file(const char *dir, const char *pfx, const char *suffix, char **opened_path_p TSRMLS_DC) { FILE *fp; - int fd = php_open_temporary_fd(dir, pfx, opened_path_p TSRMLS_CC); + int fd = php_open_temporary_fd(dir, pfx, suffix, opened_path_p TSRMLS_CC); if (fd == -1) { return NULL; diff --git a/main/php_open_temporary_file.h b/main/php_open_temporary_file.h index 873bffcb0aa77..0e80f6488faa9 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 TSRMLS_DC); -PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, char **opened_path_p, zend_bool open_basedir_check TSRMLS_DC); -PHPAPI int php_open_temporary_fd(const char *dir, const char *pfx, char **opened_path_p TSRMLS_DC); +PHPAPI FILE *php_open_temporary_file(const char *dir, const char *pfx, const char *suffix, char **opened_path_p TSRMLS_DC); +PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, char **opened_path_p, const char *suffix, zend_bool open_basedir_check TSRMLS_DC); +PHPAPI int php_open_temporary_fd(const char *dir, const char *pfx, const char *suffix, char **opened_path_p TSRMLS_DC); PHPAPI const char *php_get_temporary_directory(TSRMLS_D); PHPAPI void php_shutdown_temporary_directory(void); END_EXTERN_C() diff --git a/main/rfc1867.c b/main/rfc1867.c index b1011e21cbc45..8effe61f6b846 100644 --- a/main/rfc1867.c +++ b/main/rfc1867.c @@ -1003,7 +1003,7 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */ /* in non-debug mode we have no problem with 0-length files */ { #endif - fd = php_open_temporary_fd_ex(PG(upload_tmp_dir), "php", &temp_filename, 1 TSRMLS_CC); + fd = php_open_temporary_fd_ex(PG(upload_tmp_dir), "php", &temp_filename, "", 1 TSRMLS_CC); upload_cnt--; if (fd == -1) { sapi_module.sapi_error(E_WARNING, "File upload error - unable to create a temporary file"); diff --git a/main/streams/php_stream_plain_wrapper.h b/main/streams/php_stream_plain_wrapper.h index 4370867995cba..37ad6b8472c08 100644 --- a/main/streams/php_stream_plain_wrapper.h +++ b/main/streams/php_stream_plain_wrapper.h @@ -45,8 +45,8 @@ 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 TSRMLS_DC); #define php_stream_fopen_tmpfile() _php_stream_fopen_tmpfile(0 STREAMS_CC TSRMLS_CC) -PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char *pfx, char **opened_path STREAMS_DC TSRMLS_DC); -#define php_stream_fopen_temporary_file(dir, pfx, opened_path) _php_stream_fopen_temporary_file((dir), (pfx), (opened_path) STREAMS_CC TSRMLS_CC) +PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char *pfx, const char *suffix, char **opened_path STREAMS_DC TSRMLS_DC); +#define php_stream_fopen_temporary_file(dir, pfx, opened_path, suffix) _php_stream_fopen_temporary_file((dir), (pfx), (suffix), (opened_path) STREAMS_CC TSRMLS_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 diff --git a/main/streams/plain_wrapper.c b/main/streams/plain_wrapper.c index 6ddfc74a110fd..95d2a21f4d440 100644 --- a/main/streams/plain_wrapper.c +++ b/main/streams/plain_wrapper.c @@ -183,9 +183,9 @@ static php_stream *_php_stream_fopen_from_file_int(FILE *file, const char *mode 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 STREAMS_DC TSRMLS_DC) +PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char *pfx, const char *suffix, char **opened_path STREAMS_DC TSRMLS_DC) { - int fd = php_open_temporary_fd(dir, pfx, opened_path TSRMLS_CC); + int fd = php_open_temporary_fd(dir, pfx, suffix, opened_path TSRMLS_CC); if (fd != -1) { php_stream *stream = php_stream_fopen_from_fd_int_rel(fd, "r+b", NULL); @@ -204,7 +204,7 @@ PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char PHPAPI php_stream *_php_stream_fopen_tmpfile(int dummy STREAMS_DC TSRMLS_DC) { char *opened_path = NULL; - int fd = php_open_temporary_fd(NULL, "php", &opened_path TSRMLS_CC); + int fd = php_open_temporary_fd(NULL, "php", "", &opened_path TSRMLS_CC); if (fd != -1) { php_stream *stream = php_stream_fopen_from_fd_int_rel(fd, "r+b", NULL); From c30e3edb0bc108c082d18eedc9b6d155db4a929d Mon Sep 17 00:00:00 2001 From: Gernot Vormayr Date: Thu, 30 Jan 2014 21:12:14 +0100 Subject: [PATCH 2/3] Fixed test to pass --- ext/standard/tests/file/tempnam_suffix.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/standard/tests/file/tempnam_suffix.phpt b/ext/standard/tests/file/tempnam_suffix.phpt index 5bbac6e066135..100291a80df22 100644 --- a/ext/standard/tests/file/tempnam_suffix.phpt +++ b/ext/standard/tests/file/tempnam_suffix.phpt @@ -63,7 +63,7 @@ rmdir($file_path); echo "\n*** Done ***\n"; ?> --EXPECTF-- -*** Testing tempnam() with obscure suffixes *** +*** Testing tempnam() with suffixes *** -- Iteration 0 -- File name is => %s/prefix%s File permissions are => 100600 From cde43a101e4b1830cf40dc91eaceba0b383c8516 Mon Sep 17 00:00:00 2001 From: Stefan Neufeind Date: Thu, 20 Feb 2014 12:09:43 +0100 Subject: [PATCH 3/3] Fixed function-call in gd.c --- ext/gd/gd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/gd/gd.c b/ext/gd/gd.c index 2a704d7a85bc1..b88eb44a9a903 100644 --- a/ext/gd/gd.c +++ b/ext/gd/gd.c @@ -2639,7 +2639,7 @@ static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char char buf[4096]; char *path; - tmp = php_open_temporary_file(NULL, NULL, &path TSRMLS_CC); + tmp = php_open_temporary_file(NULL, NULL, NULL, &path TSRMLS_CC); if (tmp == NULL) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open temporary file"); RETURN_FALSE;