Skip to content

Commit

Permalink
Small changes on envv and arv managment
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitSeb committed Jan 14, 2025
1 parent 9c5daf5 commit d7007dd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1388,24 +1388,25 @@ 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, "_=");
strcat(tmp, prog);
(*dest)[idx++] = box_strdup(tmp);
}
// and a final NULL
(*dest)[idx++] = 0;
(*dest)[idx++] = NULL;
return 0;
}

Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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")) {
Expand Down
3 changes: 2 additions & 1 deletion src/tools/box32stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/tools/box64stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down

0 comments on commit d7007dd

Please sign in to comment.