diff --git a/vm/factor.cpp b/vm/factor.cpp index f554292570..7a2200f6db 100644 --- a/vm/factor.cpp +++ b/vm/factor.cpp @@ -56,15 +56,12 @@ void factor_vm::init_factor(vm_parameters* p) { /* OS-specific initialization */ early_init(); - const vm_char* executable_path = vm_executable_path(); - - if (executable_path) - p->executable_path = executable_path; + p->executable_path = vm_executable_path(); if (p->image_path == NULL) { if (embedded_image_p()) { p->embedded_image = true; - p->image_path = p->executable_path; + p->image_path = safe_strdup(p->executable_path); } else p->image_path = default_image_path(); } @@ -82,7 +79,8 @@ void factor_vm::init_factor(vm_parameters* p) { cell aliens[][2] = { {OBJ_CPU, (cell)FACTOR_CPU_STRING}, - {OBJ_EXECUTABLE, (cell)p->executable_path}, + {OBJ_EXECUTABLE, (cell)safe_strdup(p->executable_path)}, + {OBJ_IMAGE, (cell)safe_strdup(p->image_path)}, {OBJ_OS, (cell)FACTOR_OS_STRING}, {OBJ_VM_COMPILE_TIME, (cell)FACTOR_COMPILE_TIME}, {OBJ_VM_COMPILER, (cell)FACTOR_COMPILER_VERSION}, diff --git a/vm/image.cpp b/vm/image.cpp index f4b2311097..4e408df73a 100644 --- a/vm/image.cpp +++ b/vm/image.cpp @@ -43,9 +43,12 @@ vm_parameters::vm_parameters() { callback_size = 256; } -void vm_parameters::init_from_args(int argc, vm_char** argv) { - executable_path = argv[0]; +vm_parameters::~vm_parameters() { + free((vm_char *)image_path); + free((vm_char *)executable_path); +} +void vm_parameters::init_from_args(int argc, vm_char** argv) { int i = 0; for (i = 1; i < argc; i++) { @@ -73,8 +76,13 @@ void vm_parameters::init_from_args(int argc, vm_char** argv) { ; else if (factor_arg(arg, STRING_LITERAL("-callbacks=%d"), &callback_size)) ; - else if (STRNCMP(arg, STRING_LITERAL("-i="), 3) == 0) - image_path = arg + 3; + else if (STRNCMP(arg, STRING_LITERAL("-i="), 3) == 0) { + /* In case you specify -i more than once. */ + if (image_path) { + free((vm_char *)image_path); + } + image_path = safe_strdup(arg + 3); + } else if (STRCMP(arg, STRING_LITERAL("-fep")) == 0) fep = true; else if (STRCMP(arg, STRING_LITERAL("-nosignals")) == 0) @@ -255,9 +263,6 @@ void factor_vm::load_image(vm_parameters* p) { cell data_offset = data->tenured->start - h.data_relocation_base; cell code_offset = code->allocator->start - h.code_relocation_base; fixup_heaps(data_offset, code_offset); - - /* Store image path name */ - special_objects[OBJ_IMAGE] = allot_alien(false_object, (cell)p->image_path); } /* Save the current image to disk. We don't throw any exceptions here diff --git a/vm/image.hpp b/vm/image.hpp index 36709b2ec1..a4e9e7d0c5 100644 --- a/vm/image.hpp +++ b/vm/image.hpp @@ -45,6 +45,7 @@ struct vm_parameters { cell callback_size; vm_parameters(); + ~vm_parameters(); void init_from_args(int argc, vm_char** argv); }; diff --git a/vm/os-genunix.cpp b/vm/os-genunix.cpp index 21b9304e40..4fb857b296 100644 --- a/vm/os-genunix.cpp +++ b/vm/os-genunix.cpp @@ -12,15 +12,15 @@ void early_init() {} #define SUFFIX ".image" #define SUFFIX_LEN 6 -/* You must delete[] the result yourself. */ +/* You must free() the result yourself. */ const char* default_image_path() { const char* path = vm_executable_path(); if (!path) - return "factor.image"; + return strdup("factor.image"); int len = strlen(path); - char* new_path = new char[len + SUFFIX_LEN + 1]; + char* new_path = (char *)malloc(len + SUFFIX_LEN + 1); memcpy(new_path, path, len); memcpy(new_path + len, SUFFIX, SUFFIX_LEN + 1); free(const_cast(path));