Skip to content

Commit 6a819bb

Browse files
author
Julien Pauli
committed
Fix for #66048
1 parent c6b986b commit 6a819bb

File tree

5 files changed

+42
-41
lines changed

5 files changed

+42
-41
lines changed

ext/standard/basic_functions.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -4740,11 +4740,11 @@ PHP_FUNCTION(error_clear_last)
47404740
PG(last_error_type) = 0;
47414741
PG(last_error_lineno) = 0;
47424742

4743-
free(PG(last_error_message));
4743+
efree(PG(last_error_message));
47444744
PG(last_error_message) = NULL;
47454745

47464746
if (PG(last_error_file)) {
4747-
free(PG(last_error_file));
4747+
efree(PG(last_error_file));
47484748
PG(last_error_file) = NULL;
47494749
}
47504750
}

main/main.c

+25-14
Original file line numberDiff line numberDiff line change
@@ -990,11 +990,11 @@ static void php_error_cb(int type, const char *error_filename, const uint error_
990990
HANDLE_BLOCK_INTERRUPTIONS();
991991
#endif
992992
if (PG(last_error_message)) {
993-
free(PG(last_error_message));
993+
efree(PG(last_error_message));
994994
PG(last_error_message) = NULL;
995995
}
996996
if (PG(last_error_file)) {
997-
free(PG(last_error_file));
997+
efree(PG(last_error_file));
998998
PG(last_error_file) = NULL;
999999
}
10001000
#ifdef ZEND_SIGNALS
@@ -1004,8 +1004,8 @@ static void php_error_cb(int type, const char *error_filename, const uint error_
10041004
error_filename = "Unknown";
10051005
}
10061006
PG(last_error_type) = type;
1007-
PG(last_error_message) = strdup(buffer);
1008-
PG(last_error_file) = strdup(error_filename);
1007+
PG(last_error_message) = estrdup(buffer);
1008+
PG(last_error_file) = estrdup(error_filename);
10091009
PG(last_error_lineno) = error_lineno;
10101010
}
10111011

@@ -1394,6 +1394,25 @@ static zval *php_get_configuration_directive_for_zend(zend_string *name)
13941394
}
13951395
/* }}} */
13961396

1397+
/* {{{ php_free_request_globals
1398+
*/
1399+
static void php_free_request_globals(void)
1400+
{
1401+
if (PG(last_error_message)) {
1402+
efree(PG(last_error_message));
1403+
PG(last_error_message) = NULL;
1404+
}
1405+
if (PG(last_error_file)) {
1406+
efree(PG(last_error_file));
1407+
PG(last_error_file) = NULL;
1408+
}
1409+
if (PG(php_sys_temp_dir)) {
1410+
efree(PG(php_sys_temp_dir));
1411+
PG(php_sys_temp_dir) = NULL;
1412+
}
1413+
}
1414+
/* }}} */
1415+
13971416
/* {{{ php_message_handler_for_zend
13981417
*/
13991418
static void php_message_handler_for_zend(zend_long message, const void *data)
@@ -1792,15 +1811,8 @@ void php_request_shutdown(void *dummy)
17921811
}
17931812
} zend_end_try();
17941813

1795-
/* 8. free last error information */
1796-
if (PG(last_error_message)) {
1797-
free(PG(last_error_message));
1798-
PG(last_error_message) = NULL;
1799-
}
1800-
if (PG(last_error_file)) {
1801-
free(PG(last_error_file));
1802-
PG(last_error_file) = NULL;
1803-
}
1814+
/* 8. free request-bound globals */
1815+
php_free_request_globals();
18041816

18051817
/* 9. Shutdown scanner/executor/compiler and restore ini entries */
18061818
zend_deactivate();
@@ -2350,7 +2362,6 @@ void php_module_shutdown(void)
23502362
#endif
23512363

23522364
php_output_shutdown();
2353-
php_shutdown_temporary_directory();
23542365

23552366
module_initialized = 0;
23562367

main/php_globals.h

+2
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ struct _php_core_globals {
142142
char *last_error_file;
143143
int last_error_lineno;
144144

145+
char *php_sys_temp_dir;
146+
145147
char *disable_functions;
146148
char *disable_classes;
147149
zend_bool allow_url_include;

main/php_open_temporary_file.c

+13-24
Original file line numberDiff line numberDiff line change
@@ -171,25 +171,14 @@ static int php_do_open_temporary_file(const char *path, const char *pfx, zend_st
171171
}
172172
/* }}} */
173173

174-
/* Cache the chosen temporary directory. */
175-
static char* temporary_directory;
176-
177-
PHPAPI void php_shutdown_temporary_directory(void)
178-
{
179-
if (temporary_directory) {
180-
free(temporary_directory);
181-
temporary_directory = NULL;
182-
}
183-
}
184-
185174
/*
186175
* Determine where to place temporary files.
187176
*/
188177
PHPAPI const char* php_get_temporary_directory(void)
189178
{
190179
/* Did we determine the temporary directory already? */
191-
if (temporary_directory) {
192-
return temporary_directory;
180+
if (PG(php_sys_temp_dir)) {
181+
return PG(php_sys_temp_dir);
193182
}
194183

195184
/* Is there a temporary directory "sys_temp_dir" in .ini defined? */
@@ -198,11 +187,11 @@ PHPAPI const char* php_get_temporary_directory(void)
198187
if (sys_temp_dir) {
199188
int len = (int)strlen(sys_temp_dir);
200189
if (len >= 2 && sys_temp_dir[len - 1] == DEFAULT_SLASH) {
201-
temporary_directory = zend_strndup(sys_temp_dir, len - 1);
202-
return temporary_directory;
190+
PG(php_sys_temp_dir) = estrndup(sys_temp_dir, len - 1);
191+
return PG(php_sys_temp_dir);
203192
} else if (len >= 1 && sys_temp_dir[len - 1] != DEFAULT_SLASH) {
204-
temporary_directory = zend_strndup(sys_temp_dir, len);
205-
return temporary_directory;
193+
PG(php_sys_temp_dir) = estrndup(sys_temp_dir, len);
194+
return PG(php_sys_temp_dir);
206195
}
207196
}
208197
}
@@ -232,24 +221,24 @@ PHPAPI const char* php_get_temporary_directory(void)
232221
int len = strlen(s);
233222

234223
if (s[len - 1] == DEFAULT_SLASH) {
235-
temporary_directory = zend_strndup(s, len - 1);
224+
PG(php_sys_temp_dir) = estrndup(s, len - 1);
236225
} else {
237-
temporary_directory = zend_strndup(s, len);
226+
PG(php_sys_temp_dir) = estrndup(s, len);
238227
}
239228

240-
return temporary_directory;
229+
return PG(php_sys_temp_dir);
241230
}
242231
}
243232
#ifdef P_tmpdir
244233
/* Use the standard default temporary directory. */
245234
if (P_tmpdir) {
246-
temporary_directory = strdup(P_tmpdir);
247-
return temporary_directory;
235+
PG(php_sys_temp_dir) = estrdup(P_tmpdir);
236+
return PG(php_sys_temp_dir);
248237
}
249238
#endif
250239
/* Shouldn't ever(!) end up here ... last ditch default. */
251-
temporary_directory = strdup("/tmp");
252-
return temporary_directory;
240+
PG(php_sys_temp_dir) = estrdup("/tmp");
241+
return PG(php_sys_temp_dir);
253242
#endif
254243
}
255244

main/php_open_temporary_file.h

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ PHPAPI FILE *php_open_temporary_file(const char *dir, const char *pfx, zend_stri
2626
PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, zend_string **opened_path_p, zend_bool open_basedir_check);
2727
PHPAPI int php_open_temporary_fd(const char *dir, const char *pfx, zend_string **opened_path_p);
2828
PHPAPI const char *php_get_temporary_directory(void);
29-
PHPAPI void php_shutdown_temporary_directory(void);
3029
END_EXTERN_C()
3130

3231
#endif /* PHP_OPEN_TEMPORARY_FILE_H */

0 commit comments

Comments
 (0)