From d7007dd3ccbac2fcd4aac4b9d26806c7285e593e Mon Sep 17 00:00:00 2001 From: ptitSeb Date: Tue, 14 Jan 2025 20:37:47 +0100 Subject: [PATCH] Small changes on envv and arv managment --- src/core.c | 11 ++++++----- src/tools/box32stack.c | 3 ++- src/tools/box64stack.c | 3 ++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/core.c b/src/core.c index 967af6b6f3..a6972f8b5f 100644 --- a/src/core.c +++ b/src/core.c @@ -1388,16 +1388,17 @@ int GatherEnv(char*** dest, char** env, char* prog) { char** p = env; int idx = 0; + int _prog = 0; while(*p) { if(strncmp(*p, "_=", 2)==0) { - // ignore + _prog = 1; } else { (*dest)[idx++] = box_strdup(*p); } ++p; } // add "_=prog" at the end... - if(prog) { + if(_prog && prog) { int l = strlen(prog); char tmp[l+3]; strcpy(tmp, "_="); @@ -1405,7 +1406,7 @@ int GatherEnv(char*** dest, char** env, char* prog) (*dest)[idx++] = box_strdup(tmp); } // and a final NULL - (*dest)[idx++] = 0; + (*dest)[idx++] = NULL; return 0; } @@ -1857,7 +1858,7 @@ static void add_argv(const char* what) { if(!where) where = (box64_wine)?2:1; printf_log(LOG_INFO, "Inserting \"%s\" to the argument %d\n", what, where); - my_context->argv = (char**)box_realloc(my_context->argv, (my_context->argc+1)*sizeof(char*)); + my_context->argv = (char**)box_realloc(my_context->argv, (my_context->argc+1+1)*sizeof(char*)); memmove(my_context->argv+where+1, my_context->argv+where, (my_context->argc-where)*sizeof(char*)); my_context->argv[where] = box_strdup(what); my_context->argc++; @@ -2095,7 +2096,7 @@ int initialize(int argc, const char **argv, char** env, x64emu_t** emulator, elf my_context->envc = CountEnv(environ?environ:env); printf_log(LOG_INFO, "Counted %d Env var\n", my_context->envc); // allocate extra space for new environment variables such as BOX64_PATH - my_context->envv = (char**)box_calloc(my_context->envc+4, sizeof(char*)); + my_context->envv = (char**)box_calloc(my_context->envc+1, sizeof(char*)); path_collection_t ld_preload = {0}; if(getenv("BOX64_LD_PRELOAD")) { diff --git a/src/tools/box32stack.c b/src/tools/box32stack.c index 0fa1460e80..903996c71a 100644 --- a/src/tools/box32stack.c +++ b/src/tools/box32stack.c @@ -38,11 +38,12 @@ void SetupInitialStack32(x64emu_t *emu) PushString32(emu, emu->context->argv[0]); uintptr_t p_arg0 = from_ptr(R_ESP); // push envs - uintptr_t p_envv[emu->context->envc]; + uintptr_t p_envv[emu->context->envc+1]; for (int i=emu->context->envc-1; i>=0; --i) { PushString32(emu, emu->context->envv[i]); p_envv[i] = from_ptr(R_ESP); } + p_envv[emu->context->envc] = 0; // push args, also, free the argv[] string and point to the one in the main stack uintptr_t p_argv[emu->context->argc]; for (int i=emu->context->argc-1; i>=0; --i) { diff --git a/src/tools/box64stack.c b/src/tools/box64stack.c index 9036bda477..8b5249d98b 100644 --- a/src/tools/box64stack.c +++ b/src/tools/box64stack.c @@ -61,7 +61,8 @@ void SetupInitialStack(x64emu_t *emu) PushString(emu, emu->context->argv[0]); uintptr_t p_arg0 = R_RSP; // push envs - uintptr_t p_envv[emu->context->envc]; + uintptr_t p_envv[emu->context->envc+1]; + p_envv[emu->context->envc] = 0; for (int i=emu->context->envc-1; i>=0; --i) { PushString(emu, emu->context->envv[i]); box_free(emu->context->envv[i]);