summaryrefslogtreecommitdiff
path: root/Zend
diff options
context:
space:
mode:
authorAnatol Belski <ab@php.net>2013-10-17 10:40:43 +0200
committerAnatol Belski <ab@php.net>2013-10-17 10:40:43 +0200
commitcf6ab0e915f95f01a8db1ce7daf70ed36cf49c66 (patch)
treec4fd681e0b62ad67d2b2ef9101c5e59cae8f0bc2 /Zend
parente30b2aae5ad93405eee19bca6125ea872f409c82 (diff)
downloadphp-git-cf6ab0e915f95f01a8db1ce7daf70ed36cf49c66.tar.gz
applied and fixed the original patch
initial work on the patch import done
Diffstat (limited to 'Zend')
-rw-r--r--Zend/zend.c12
-rw-r--r--Zend/zend_virtual_cwd.c117
-rw-r--r--Zend/zend_virtual_cwd.h2
3 files changed, 81 insertions, 50 deletions
diff --git a/Zend/zend.c b/Zend/zend.c
index 11baf34c93..b98d264a3c 100644
--- a/Zend/zend.c
+++ b/Zend/zend.c
@@ -30,6 +30,7 @@
#include "zend_ini.h"
#include "zend_vm.h"
#include "zend_dtrace.h"
+#include "zend_virtual_cwd.h"
#ifdef ZTS
# define GLOBAL_FUNCTION_TABLE global_function_table
@@ -652,6 +653,8 @@ int zend_startup(zend_utility_functions *utility_functions, char **extensions TS
start_memory_manager(TSRMLS_C);
+ virtual_cwd_startup(); /* Could use shutdown to free the main cwd but it would just slow it down for CGI */
+
#if defined(__FreeBSD__) || defined(__DragonFly__)
/* FreeBSD and DragonFly floating point precision fix */
fpsetmask(0);
@@ -802,9 +805,14 @@ void zend_post_startup(TSRMLS_D) /* {{{ */
compiler_globals_ctor(compiler_globals, tsrm_ls);
}
free(EG(zend_constants));
+
+ virtual_cwd_deactivate(TSRMLS_C);
+
executor_globals_ctor(executor_globals, tsrm_ls);
global_persistent_list = &EG(persistent_list);
zend_copy_ini_directives(TSRMLS_C);
+#else
+ virtual_cwd_deactivate(TSRMLS_C);
#endif
}
/* }}} */
@@ -820,6 +828,9 @@ void zend_shutdown(TSRMLS_D) /* {{{ */
zend_destroy_rsrc_list(&EG(persistent_list) TSRMLS_CC);
zend_destroy_modules();
+ virtual_cwd_deactivate(TSRMLS_C);
+ virtual_cwd_shutdown();
+
zend_hash_destroy(GLOBAL_FUNCTION_TABLE);
zend_hash_destroy(GLOBAL_CLASS_TABLE);
@@ -910,6 +921,7 @@ ZEND_API char *get_zend_version(void) /* {{{ */
void zend_activate(TSRMLS_D) /* {{{ */
{
+ virtual_cwd_activate(TSRMLS_C);
gc_reset(TSRMLS_C);
init_compiler(TSRMLS_C);
init_executor(TSRMLS_C);
diff --git a/Zend/zend_virtual_cwd.c b/Zend/zend_virtual_cwd.c
index e535e5c8db..968390d353 100644
--- a/Zend/zend_virtual_cwd.c
+++ b/Zend/zend_virtual_cwd.c
@@ -30,6 +30,7 @@
#include <fcntl.h>
#include <time.h>
+#include "zend.h"
#include "zend_virtual_cwd.h"
#include "tsrm_strtok_r.h"
@@ -149,11 +150,12 @@ static int php_check_dots(const char *element, int n)
#define CWD_STATE_COPY(d, s) \
(d)->cwd_length = (s)->cwd_length; \
- (d)->cwd = (char *) malloc((s)->cwd_length+1); \
+ (d)->cwd = (char *) emalloc((s)->cwd_length+1); \
memcpy((d)->cwd, (s)->cwd, (s)->cwd_length+1);
#define CWD_STATE_FREE(s) \
- free((s)->cwd);
+ efree((s)->cwd); \
+ (s)->cwd = NULL;
#ifdef TSRM_WIN32
@@ -337,16 +339,15 @@ CWD_API int php_sys_stat_ex(const char *path, struct stat *buf, int lstat) /* {{
HANDLE hLink = NULL;
REPARSE_DATA_BUFFER * pbuffer;
unsigned int retlength = 0;
- TSRM_ALLOCA_FLAG(use_heap_large);
hLink = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, NULL);
if(hLink == INVALID_HANDLE_VALUE) {
return -1;
}
- pbuffer = (REPARSE_DATA_BUFFER *)tsrm_do_alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE, use_heap_large);
+ pbuffer = (REPARSE_DATA_BUFFER *)emalloc(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
if(!DeviceIoControl(hLink, FSCTL_GET_REPARSE_POINT, NULL, 0, pbuffer, MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &retlength, NULL)) {
- tsrm_free_alloca(pbuffer, use_heap_large);
+ efree(pbuffer);
CloseHandle(hLink);
return -1;
}
@@ -363,7 +364,7 @@ CWD_API int php_sys_stat_ex(const char *path, struct stat *buf, int lstat) /* {{
buf->st_mode |=;
}
#endif
- tsrm_free_alloca(pbuffer, use_heap_large);
+ efree(pbuffer);
} else {
buf->st_mode = (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? (S_IFDIR|S_IEXEC|(S_IEXEC>>3)|(S_IEXEC>>6)) : S_IFREG;
buf->st_mode |= (data.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? (S_IREAD|(S_IREAD>>3)|(S_IREAD>>6)) : (S_IREAD|(S_IREAD>>3)|(S_IREAD>>6)|S_IWRITE|(S_IWRITE>>3)|(S_IWRITE>>6));
@@ -424,12 +425,13 @@ static void cwd_globals_ctor(virtual_cwd_globals *cwd_g TSRMLS_DC) /* {{{ */
cwd_g->realpath_cache_size_limit = REALPATH_CACHE_SIZE;
cwd_g->realpath_cache_ttl = REALPATH_CACHE_TTL;
memset(cwd_g->realpath_cache, 0, sizeof(cwd_g->realpath_cache));
+ virtual_cwd_activate(TSRMLS_C);
}
/* }}} */
static void cwd_globals_dtor(virtual_cwd_globals *cwd_g TSRMLS_DC) /* {{{ */
{
- CWD_STATE_FREE(&cwd_g->cwd);
+/* CWD_STATE_FREE(&cwd_globals->cwd); */
realpath_cache_clean(TSRMLS_C);
}
/* }}} */
@@ -490,6 +492,24 @@ CWD_API void virtual_cwd_shutdown(void) /* {{{ */
}
/* }}} */
+CWD_API int virtual_cwd_activate(TSRMLS_D) /* {{{ */
+{
+ if (CWDG(cwd).cwd == NULL) {
+ CWD_STATE_COPY(&CWDG(cwd), &main_cwd_state);
+ }
+ return 0;
+}
+/* }}} */
+
+CWD_API int virtual_cwd_deactivate(TSRMLS_D) /* {{{ */
+{
+ if (CWDG(cwd).cwd != NULL) {
+ CWD_STATE_FREE(&CWDG(cwd));
+ }
+ return 0;
+}
+/* }}} */
+
CWD_API char *virtual_getcwd_ex(size_t *length TSRMLS_DC) /* {{{ */
{
cwd_state *state;
@@ -500,7 +520,7 @@ CWD_API char *virtual_getcwd_ex(size_t *length TSRMLS_DC) /* {{{ */
char *retval;
*length = 1;
- retval = (char *) malloc(2);
+ retval = (char *) emalloc(2);
if (retval == NULL) {
return NULL;
}
@@ -515,7 +535,7 @@ CWD_API char *virtual_getcwd_ex(size_t *length TSRMLS_DC) /* {{{ */
char *retval;
*length = state->cwd_length+1;
- retval = (char *) malloc(*length+1);
+ retval = (char *) emalloc(*length+1);
if (retval == NULL) {
return NULL;
}
@@ -527,7 +547,7 @@ CWD_API char *virtual_getcwd_ex(size_t *length TSRMLS_DC) /* {{{ */
}
#endif
*length = state->cwd_length;
- return strdup(state->cwd);
+ return estrdup(state->cwd);
}
/* }}} */
@@ -543,12 +563,12 @@ CWD_API char *virtual_getcwd(char *buf, size_t size TSRMLS_DC) /* {{{ */
return cwd;
}
if (length > size-1) {
- free(cwd);
+ efree(cwd);
errno = ERANGE; /* Is this OK? */
return NULL;
}
memcpy(buf, cwd, length+1);
- free(cwd);
+ efree(cwd);
return buf;
}
/* }}} */
@@ -754,13 +774,11 @@ static int tsrm_realpath_r(char *path, int start, int len, int *ll, time_t *t, i
#ifdef TSRM_WIN32
WIN32_FIND_DATA data;
HANDLE hFind;
- TSRM_ALLOCA_FLAG(use_heap_large)
#else
struct stat st;
#endif
realpath_cache_bucket *bucket;
char *tmp;
- TSRM_ALLOCA_FLAG(use_heap)
while (1) {
if (len <= start) {
@@ -860,7 +878,7 @@ static int tsrm_realpath_r(char *path, int start, int len, int *ll, time_t *t, i
FindClose(hFind);
}
- tmp = tsrm_do_alloca(len+1, use_heap);
+ tmp = emalloc(len+1);
memcpy(tmp, path, len+1);
if(save &&
@@ -887,12 +905,12 @@ static int tsrm_realpath_r(char *path, int start, int len, int *ll, time_t *t, i
return -1;
}
- pbuffer = (REPARSE_DATA_BUFFER *)tsrm_do_alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE, use_heap_large);
+ pbuffer = (REPARSE_DATA_BUFFER *)emalloc(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
if (pbuffer == NULL) {
return -1;
}
if(!DeviceIoControl(hLink, FSCTL_GET_REPARSE_POINT, NULL, 0, pbuffer, MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &retlength, NULL)) {
- tsrm_free_alloca(pbuffer, use_heap_large);
+ efree(pbuffer);
CloseHandle(hLink);
return -1;
}
@@ -908,7 +926,7 @@ static int tsrm_realpath_r(char *path, int start, int len, int *ll, time_t *t, i
printname_len + 1,
printname, MAX_PATH, NULL, NULL
)) {
- tsrm_free_alloca(pbuffer, use_heap_large);
+ efree(pbuffer);
return -1;
};
printname_len = pbuffer->MountPointReparseBuffer.PrintNameLength / sizeof(WCHAR);
@@ -920,7 +938,7 @@ static int tsrm_realpath_r(char *path, int start, int len, int *ll, time_t *t, i
substitutename_len + 1,
substitutename, MAX_PATH, NULL, NULL
)) {
- tsrm_free_alloca(pbuffer, use_heap_large);
+ efree(pbuffer);
return -1;
};
substitutename[substitutename_len] = 0;
@@ -934,7 +952,7 @@ static int tsrm_realpath_r(char *path, int start, int len, int *ll, time_t *t, i
printname_len + 1,
printname, MAX_PATH, NULL, NULL
)) {
- tsrm_free_alloca(pbuffer, use_heap_large);
+ efree(pbuffer);
return -1;
};
printname[pbuffer->MountPointReparseBuffer.PrintNameLength / sizeof(WCHAR)] = 0;
@@ -945,7 +963,7 @@ static int tsrm_realpath_r(char *path, int start, int len, int *ll, time_t *t, i
substitutename_len + 1,
substitutename, MAX_PATH, NULL, NULL
)) {
- tsrm_free_alloca(pbuffer, use_heap_large);
+ efree(pbuffer);
return -1;
};
substitutename[substitutename_len] = 0;
@@ -955,7 +973,7 @@ static int tsrm_realpath_r(char *path, int start, int len, int *ll, time_t *t, i
memcpy(substitutename, path, len + 1);
substitutename_len = len;
} else {
- tsrm_free_alloca(pbuffer, use_heap_large);
+ efree(pbuffer);
return -1;
}
@@ -999,21 +1017,21 @@ static int tsrm_realpath_r(char *path, int start, int len, int *ll, time_t *t, i
fprintf(stderr, "sub: %s ", substitutename);
fprintf(stderr, "resolved: %s ", path);
#endif
- tsrm_free_alloca(pbuffer, use_heap_large);
+ efree(pbuffer);
if(isabsolute == 1) {
if (!((j == 3) && (path[1] == ':') && (path[2] == '\\'))) {
/* use_realpath is 0 in the call below coz path is absolute*/
j = tsrm_realpath_r(path, 0, j, ll, t, 0, is_dir, &directory TSRMLS_CC);
if(j < 0) {
- tsrm_free_alloca(tmp, use_heap);
+ efree(tmp);
return -1;
}
}
}
else {
if(i + j >= MAXPATHLEN - 1) {
- tsrm_free_alloca(tmp, use_heap);
+ efree(tmp);
return -1;
}
@@ -1022,7 +1040,7 @@ static int tsrm_realpath_r(char *path, int start, int len, int *ll, time_t *t, i
path[i-1] = DEFAULT_SLASH;
j = tsrm_realpath_r(path, start, i + j, ll, t, use_realpath, is_dir, &directory TSRMLS_CC);
if(j < 0) {
- tsrm_free_alloca(tmp, use_heap);
+ efree(tmp);
return -1;
}
}
@@ -1043,7 +1061,7 @@ static int tsrm_realpath_r(char *path, int start, int len, int *ll, time_t *t, i
#elif defined(NETWARE)
save = 0;
- tmp = tsrm_do_alloca(len+1, use_heap);
+ tmp = emalloc(len+1);
memcpy(tmp, path, len+1);
#else
if (save && php_sys_lstat(path, &st) < 0) {
@@ -1055,25 +1073,25 @@ static int tsrm_realpath_r(char *path, int start, int len, int *ll, time_t *t, i
save = 0;
}
- tmp = tsrm_do_alloca(len+1, use_heap);
+ tmp = emalloc(len+1);
memcpy(tmp, path, len+1);
if (save && S_ISLNK(st.st_mode)) {
if (++(*ll) > LINK_MAX || (j = php_sys_readlink(tmp, path, MAXPATHLEN)) < 0) {
/* too many links or broken symlinks */
- tsrm_free_alloca(tmp, use_heap);
+ efree(tmp);
return -1;
}
path[j] = 0;
if (IS_ABSOLUTE_PATH(path, j)) {
j = tsrm_realpath_r(path, 1, j, ll, t, use_realpath, is_dir, &directory TSRMLS_CC);
if (j < 0) {
- tsrm_free_alloca(tmp, use_heap);
+ efree(tmp);
return -1;
}
} else {
if (i + j >= MAXPATHLEN-1) {
- tsrm_free_alloca(tmp, use_heap);
+ efree(tmp);
return -1; /* buffer overflow */
}
memmove(path+i, path, j+1);
@@ -1081,7 +1099,7 @@ static int tsrm_realpath_r(char *path, int start, int len, int *ll, time_t *t, i
path[i-1] = DEFAULT_SLASH;
j = tsrm_realpath_r(path, start, i + j, ll, t, use_realpath, is_dir, &directory TSRMLS_CC);
if (j < 0) {
- tsrm_free_alloca(tmp, use_heap);
+ efree(tmp);
return -1;
}
}
@@ -1096,7 +1114,7 @@ static int tsrm_realpath_r(char *path, int start, int len, int *ll, time_t *t, i
}
if (is_dir && !directory) {
/* not a directory */
- tsrm_free_alloca(tmp, use_heap);
+ efree(tmp);
return -1;
}
}
@@ -1112,7 +1130,7 @@ static int tsrm_realpath_r(char *path, int start, int len, int *ll, time_t *t, i
}
#ifdef TSRM_WIN32
if (j < 0 || j + len - i >= MAXPATHLEN-1) {
- tsrm_free_alloca(tmp, use_heap);
+ efree(tmp);
return -1;
}
if (save) {
@@ -1127,7 +1145,7 @@ static int tsrm_realpath_r(char *path, int start, int len, int *ll, time_t *t, i
}
#else
if (j < 0 || j + len - i >= MAXPATHLEN-1) {
- tsrm_free_alloca(tmp, use_heap);
+ efree(tmp);
return -1;
}
memcpy(path+j, tmp+i, len-i+1);
@@ -1140,7 +1158,7 @@ static int tsrm_realpath_r(char *path, int start, int len, int *ll, time_t *t, i
realpath_cache_add(tmp, len, path, j, directory, *t TSRMLS_CC);
}
- tsrm_free_alloca(tmp, use_heap);
+ efree(tmp);
return j;
}
}
@@ -1316,7 +1334,7 @@ verify:
CWD_STATE_COPY(&old_state, state);
state->cwd_length = path_length;
- tmp = realloc(state->cwd, state->cwd_length+1);
+ tmp = erealloc(state->cwd, state->cwd_length+1);
if (tmp == NULL) {
#if VIRTUAL_CWD_DEBUG
fprintf (stderr, "Out of memory\n");
@@ -1336,7 +1354,7 @@ verify:
}
} else {
state->cwd_length = path_length;
- tmp = realloc(state->cwd, state->cwd_length+1);
+ tmp = erealloc(state->cwd, state->cwd_length+1);
if (tmp == NULL) {
#if VIRTUAL_CWD_DEBUG
fprintf (stderr, "Out of memory\n");
@@ -1367,7 +1385,6 @@ CWD_API int virtual_chdir_file(const char *path, int (*p_chdir)(const char *path
int length = strlen(path);
char *temp;
int retval;
- TSRM_ALLOCA_FLAG(use_heap)
if (length == 0) {
return 1; /* Can't cd to empty string */
@@ -1384,14 +1401,14 @@ CWD_API int virtual_chdir_file(const char *path, int (*p_chdir)(const char *path
if (length == COPY_WHEN_ABSOLUTE(path) && IS_ABSOLUTE_PATH(path, length+1)) { /* Also use trailing slash if this is absolute */
length++;
}
- temp = (char *) tsrm_do_alloca(length+1, use_heap);
+ temp = (char *) emalloc(length+1);
memcpy(temp, path, length);
temp[length] = 0;
#if VIRTUAL_CWD_DEBUG
fprintf (stderr, "Changing directory to %s\n", temp);
#endif
retval = p_chdir(temp TSRMLS_CC);
- tsrm_free_alloca(temp, use_heap);
+ efree(temp);
return retval;
}
/* }}} */
@@ -1404,7 +1421,7 @@ CWD_API char *virtual_realpath(const char *path, char *real_path TSRMLS_DC) /* {
/* realpath("") returns CWD */
if (!*path) {
- new_state.cwd = (char*)malloc(1);
+ new_state.cwd = (char*)emalloc(1);
if (new_state.cwd == NULL) {
retval = NULL;
goto end;
@@ -1417,7 +1434,7 @@ CWD_API char *virtual_realpath(const char *path, char *real_path TSRMLS_DC) /* {
} else if (!IS_ABSOLUTE_PATH(path, strlen(path))) {
CWD_STATE_COPY(&new_state, &CWDG(cwd));
} else {
- new_state.cwd = (char*)malloc(1);
+ new_state.cwd = (char*)emalloc(1);
if (new_state.cwd == NULL) {
retval = NULL;
goto end;
@@ -1882,7 +1899,7 @@ CWD_API FILE *virtual_popen(const char *command, const char *type TSRMLS_DC) /*
dir_length = CWDG(cwd).cwd_length;
dir = CWDG(cwd).cwd;
- ptr = command_line = (char *) malloc(command_length + sizeof("cd '' ; ") + dir_length + extra+1+1);
+ ptr = command_line = (char *) emalloc(command_length + sizeof("cd '' ; ") + dir_length + extra+1+1);
if (!command_line) {
return NULL;
}
@@ -1916,7 +1933,7 @@ CWD_API FILE *virtual_popen(const char *command, const char *type TSRMLS_DC) /*
memcpy(ptr, command, command_length+1);
retval = popen(command_line, type);
- free(command_line);
+ efree(command_line);
return retval;
}
/* }}} */
@@ -1929,7 +1946,7 @@ CWD_API char *tsrm_realpath(const char *path, char *real_path TSRMLS_DC) /* {{{
/* realpath("") returns CWD */
if (!*path) {
- new_state.cwd = (char*)malloc(1);
+ new_state.cwd = (char*)emalloc(1);
if (new_state.cwd == NULL) {
return NULL;
}
@@ -1940,10 +1957,10 @@ CWD_API char *tsrm_realpath(const char *path, char *real_path TSRMLS_DC) /* {{{
}
} else if (!IS_ABSOLUTE_PATH(path, strlen(path)) &&
VCWD_GETCWD(cwd, MAXPATHLEN)) {
- new_state.cwd = strdup(cwd);
+ new_state.cwd = estrdup(cwd);
new_state.cwd_length = strlen(cwd);
} else {
- new_state.cwd = (char*)malloc(1);
+ new_state.cwd = (char*)emalloc(1);
if (new_state.cwd == NULL) {
return NULL;
}
@@ -1952,7 +1969,7 @@ CWD_API char *tsrm_realpath(const char *path, char *real_path TSRMLS_DC) /* {{{
}
if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH TSRMLS_CC)) {
- free(new_state.cwd);
+ efree(new_state.cwd);
return NULL;
}
@@ -1960,7 +1977,7 @@ CWD_API char *tsrm_realpath(const char *path, char *real_path TSRMLS_DC) /* {{{
int copy_len = new_state.cwd_length>MAXPATHLEN-1 ? MAXPATHLEN-1 : new_state.cwd_length;
memcpy(real_path, new_state.cwd, copy_len);
real_path[copy_len] = '\0';
- free(new_state.cwd);
+ efree(new_state.cwd);
return real_path;
} else {
return new_state.cwd;
diff --git a/Zend/zend_virtual_cwd.h b/Zend/zend_virtual_cwd.h
index 72c4424670..a6ac0aeef7 100644
--- a/Zend/zend_virtual_cwd.h
+++ b/Zend/zend_virtual_cwd.h
@@ -151,6 +151,8 @@ typedef int (*verify_path_func)(const cwd_state *);
CWD_API void virtual_cwd_startup(void);
CWD_API void virtual_cwd_shutdown(void);
+CWD_API int virtual_cwd_activate(TSRMLS_D);
+CWD_API int virtual_cwd_deactivate(TSRMLS_D);
CWD_API char *virtual_getcwd_ex(size_t *length TSRMLS_DC);
CWD_API char *virtual_getcwd(char *buf, size_t size TSRMLS_DC);
CWD_API int virtual_chdir(const char *path TSRMLS_DC);