-
-
Save MasterDuke17/497d9bc0ed4ca7caa881709b29845492 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git src/vm/moar/runner/main.c src/vm/moar/runner/main.c | |
index 214bac1a5..f914dfded 100644 | |
--- src/vm/moar/runner/main.c | |
+++ src/vm/moar/runner/main.c | |
@@ -80,16 +80,13 @@ int file_exists(const char *path) { | |
int res; | |
struct _stat sb; | |
const int len = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0); | |
- wchar_t * const wpath = (wchar_t *)malloc(len * sizeof(wchar_t)); | |
+ wchar_t * const wpath = (wchar_t *)alloca(len * sizeof(wchar_t)); | |
MultiByteToWideChar(CP_UTF8, 0, path, -1, (LPWSTR)wpath, len); | |
res = _wstat(wpath, &sb); | |
- MVM_free(wpath); | |
return res == 0; | |
#else | |
- struct stat *sb = malloc(sizeof(struct stat)); | |
- int res = stat(path, sb) == 0; | |
- free(sb); | |
- return res; | |
+ struct stat sb; | |
+ return stat(path, &sb) == 0; | |
#endif | |
} | |
@@ -117,8 +114,8 @@ int retrieve_home( | |
char *options_home | |
) { | |
char *check_file_path; | |
- size_t home_size; | |
- int ret; | |
+ size_t home_size, file_path_size; | |
+ int ret, is_malloced = 0; | |
char *env_home = getenv(env_var); | |
if (options_home) { | |
@@ -150,12 +147,20 @@ int retrieve_home( | |
platformify_path(*out_home + exec_dir_path_size); | |
} | |
- check_file_path = (char*)malloc(home_size + check_file_size + 1); | |
+ file_path_size = home_size + check_file_size + 1; | |
+ if (file_path_size < 3000) { | |
+ check_file_path = (char*)alloca(file_path_size); | |
+ } | |
+ else { | |
+ check_file_path = (char*)malloc(file_path_size); | |
+ is_malloced = 1; | |
+ } | |
strncpy(check_file_path, *out_home, home_size + check_file_size); | |
strncat(check_file_path, check_file, check_file_size); | |
ret = file_exists(check_file_path); | |
- free(check_file_path); | |
+ if (is_malloced) | |
+ free(check_file_path); | |
return ret; | |
} | |
@@ -246,6 +251,8 @@ int main(int argc, char *argv[]) { | |
int argi = 1; | |
int flag; | |
int new_argc = 0; | |
+ int exec_dir_path_is_malloced = 0; | |
+ int lib_path_is_malloced = 0; | |
MVMuint32 debugserverport = 0; | |
int start_suspended = 0; | |
@@ -364,12 +371,24 @@ int main(int argc, char *argv[]) { | |
#endif | |
/* The +1 is the trailing \0 terminating the string. */ | |
- exec_dir_path_temp = (char*)malloc(exec_path_size + 1); | |
+ if (exec_path_size + 1 < 3000) { | |
+ exec_dir_path_temp = (char*)alloca(exec_path_size + 1); | |
+ } | |
+ else { | |
+ exec_dir_path_temp = (char*)malloc(exec_path_size + 1); | |
+ exec_dir_path_is_malloced = 1; | |
+ } | |
memcpy(exec_dir_path_temp, exec_path, exec_path_size + 1); | |
#ifdef _WIN32 | |
PathRemoveFileSpecA(exec_dir_path_temp); | |
exec_dir_path_size = strlen(exec_dir_path_temp); | |
- exec_dir_path = (char*)malloc(exec_dir_path_size + 1); | |
+ if (exec_dir_path_size + 1 < 3000) { | |
+ exec_dir_path = (char*)alloca(exec_dir_path_size + 1); | |
+ } | |
+ else { | |
+ exec_dir_path = (char*)malloc(exec_dir_path_size + 1); | |
+ exec_dir_path_is_malloced = 1; | |
+ } | |
memcpy(exec_dir_path, exec_dir_path_temp, exec_dir_path_size + 1); | |
#else | |
exec_dir_path = dirname(exec_dir_path_temp); | |
@@ -415,10 +434,19 @@ int main(int argc, char *argv[]) { | |
/* Put together the lib paths and perl6_file path. */ | |
- lib_path[0] = (char*)malloc(nqp_home_size + 50); | |
- lib_path[1] = (char*)malloc(rakudo_home_size + 50); | |
- lib_path[2] = (char*)malloc(rakudo_home_size + 50); | |
- perl6_file = (char*)malloc(rakudo_home_size + 50); | |
+ if (nqp_home_size + 50 < 3000 && rakudo_home_size + 50 < 3000) { | |
+ lib_path[0] = (char*)alloca(nqp_home_size + 50); | |
+ lib_path[1] = (char*)alloca(rakudo_home_size + 50); | |
+ lib_path[2] = (char*)alloca(rakudo_home_size + 50); | |
+ perl6_file = (char*)alloca(rakudo_home_size + 50); | |
+ } | |
+ else { | |
+ lib_path[0] = (char*)malloc(nqp_home_size + 50); | |
+ lib_path[1] = (char*)malloc(rakudo_home_size + 50); | |
+ lib_path[2] = (char*)malloc(rakudo_home_size + 50); | |
+ perl6_file = (char*)malloc(rakudo_home_size + 50); | |
+ lib_path_is_malloced = 1; | |
+ } | |
memcpy(lib_path[0], nqp_home, nqp_home_size); | |
memcpy(lib_path[1], rakudo_home, rakudo_home_size); | |
@@ -480,10 +508,12 @@ int main(int argc, char *argv[]) { | |
} | |
#endif | |
- free(lib_path[0]); | |
- free(lib_path[1]); | |
- free(lib_path[2]); | |
- free(perl6_file); | |
+ if (lib_path_is_malloced) { | |
+ free(lib_path[0]); | |
+ free(lib_path[1]); | |
+ free(lib_path[2]); | |
+ free(perl6_file); | |
+ } | |
#ifndef STATIC_EXEC_PATH | |
free(exec_path); | |
#endif | |
@@ -491,9 +521,11 @@ int main(int argc, char *argv[]) { | |
/* dirname's return value is either on the stack or is the same pointer | |
* that was passed to it depending on the version of libc used, which leads | |
* to double frees. */ | |
- free(exec_dir_path); | |
+ if (exec_dir_path_is_malloced) | |
+ free(exec_dir_path); | |
#endif | |
- free(exec_dir_path_temp); | |
+ if (exec_dir_path_is_malloced) | |
+ free(exec_dir_path_temp); | |
#ifndef STATIC_RAKUDO_HOME | |
free(rakudo_home); | |
#else |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment