From 9b026753b49895f5db9f8f1a9cc61eb0eaa2dd22 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Thu, 16 Oct 2025 21:54:09 +0200 Subject: [PATCH 1/2] Fix GH-20189: Tempnam notice message incorrect The notice was outputted before the creation of the file even took place. Therefore, if the creation of the file fails, the notice is outputted while it shouldn't have been. This changes one test which indeed suffered from this issue due to the open_basedir restriction. --- ext/standard/tests/file/bug52624.phpt | 2 -- main/php_open_temporary_file.c | 14 ++++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/ext/standard/tests/file/bug52624.phpt b/ext/standard/tests/file/bug52624.phpt index c5e7a9cb0ec1c..ee61eb90e6f45 100644 --- a/ext/standard/tests/file/bug52624.phpt +++ b/ext/standard/tests/file/bug52624.phpt @@ -9,6 +9,4 @@ echo tempnam("directory_that_not_exists", "prefix_"); ?> --EXPECTF-- -Notice: tempnam(): file created in the system's temporary directory in %sbug52624.php on line %d - Warning: tempnam(): open_basedir restriction in effect. File(%s) is not within the allowed path(s): (%s) in %s on line %d diff --git a/main/php_open_temporary_file.c b/main/php_open_temporary_file.c index 608f3ecf73bae..332ce7c08329f 100644 --- a/main/php_open_temporary_file.c +++ b/main/php_open_temporary_file.c @@ -303,7 +303,16 @@ PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, zend_strin if (temp_dir && *temp_dir != '\0' && (!(flags & PHP_TMP_FILE_OPEN_BASEDIR_CHECK_ON_FALLBACK) || !php_check_open_basedir(temp_dir))) { - return php_do_open_temporary_file(temp_dir, pfx, opened_path_p); + fd = php_do_open_temporary_file(temp_dir, pfx, opened_path_p); + if (!(flags & PHP_TMP_FILE_SILENT) && dir && *dir != '\0') { + /* Default temporary directory fallback. */ + if (UNEXPECTED(fd == -1)) { + /* TODO: decide whether we should notice that even the fallback failed? */ + } else { + php_error_docref(NULL, E_NOTICE, "file created in the system's temporary directory"); + } + } + return fd; } else { return -1; } @@ -317,9 +326,6 @@ PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, zend_strin fd = php_do_open_temporary_file(dir, pfx, opened_path_p); if (fd == -1) { /* Use default temporary directory. */ - if (!(flags & PHP_TMP_FILE_SILENT)) { - php_error_docref(NULL, E_NOTICE, "file created in the system's temporary directory"); - } goto def_tmp; } return fd; From 48b0aaa297ab91d39020a8d989781914fb9f1884 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Thu, 16 Oct 2025 22:51:14 +0200 Subject: [PATCH 2/2] Fix win32 test: Remove notice that was incorrectly outputted The function returned false, this should've never been outputted in the first place. --- ext/standard/tests/file/tempnam_variation3-win32.phpt | 2 -- 1 file changed, 2 deletions(-) diff --git a/ext/standard/tests/file/tempnam_variation3-win32.phpt b/ext/standard/tests/file/tempnam_variation3-win32.phpt index 5bc4f5ec884d1..68a77e57aa8ca 100644 --- a/ext/standard/tests/file/tempnam_variation3-win32.phpt +++ b/ext/standard/tests/file/tempnam_variation3-win32.phpt @@ -97,8 +97,6 @@ OK -- Iteration 3 -- OK -- Iteration 4 -- - -Notice: tempnam(): file created in the system's temporary directory in %stempnam_variation3-win32.php on line %d Failed, not created in the correct directory %s vs %s 0 -- Iteration 5 --