Skip to content

Commit

Permalink
multiple leak fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mcfriend99 committed Sep 27, 2024
1 parent 88e2608 commit d2f14ce
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/blade.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,11 @@ int main(int argc, char *argv[]) {
char **std_args = (char**)calloc(opt_deviation, sizeof(char *));
if(std_args != NULL) {
if(optind > 0) {
std_args[0] = strdup(argv[0]);
std_args[0] = argv[0];
}

for(int i = optind; i < argc; i++) {
std_args[i - optind + 1] = strdup(argv[i]);
std_args[i - optind + 1] = argv[i];
}

vm->std_args = std_args;
Expand Down
7 changes: 7 additions & 0 deletions src/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -2147,6 +2147,7 @@ static void import_statement(b_parser *p) {
emit_byte_and_short(p, OP_NATIVE_MODULE, module);

parse_specific_import(p, module_name, module, false, true);
free(module_name);
return;
}

Expand Down Expand Up @@ -2191,10 +2192,13 @@ static void import_statement(b_parser *p) {
pop(p->vm);

parse_specific_import(p, module_name, module, false, true);
free(module_name);
return;
}

free(module_path);
free(module_file);
free(module_name);
error(p, "module not found");
return;
}
Expand All @@ -2207,6 +2211,7 @@ static void import_statement(b_parser *p) {
char *source = read_file(module_path);
if (source == NULL) {
error(p, "could not read import file %s", module_path);
free(module_file);
return;
}

Expand All @@ -2220,6 +2225,7 @@ static void import_statement(b_parser *p) {

if (function == NULL) {
error(p, "failed to import %s", module_name);
free(module_file);
return;
}

Expand All @@ -2237,6 +2243,7 @@ static void import_statement(b_parser *p) {
register_module__FILE__(p->vm, module);

parse_specific_import(p, module_name, import_constant, was_renamed, false);
free(module_file);
}

static void assert_statement(b_parser *p) {
Expand Down
3 changes: 3 additions & 0 deletions src/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ void bind_user_modules(b_vm *vm, char *pkg_root) {
}
}
}

free(path);
}
closedir(dir);
}
Expand All @@ -203,6 +205,7 @@ void bind_native_modules(b_vm *vm) {
for (int i = 0; modules[i] != NULL; i++) {
load_module(vm, modules[i], NULL, strdup("<__native__>"), NULL);
}

bind_user_modules(vm, merge_paths(get_exe_dir(), "dist"));
bind_user_modules(vm, merge_paths(getcwd(NULL, 0), LOCAL_PACKAGES_DIRECTORY LOCAL_EXT_DIRECTORY));
}
Expand Down
7 changes: 7 additions & 0 deletions src/pathinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ char *resolve_import_path(char *module_name, const char *current_file, char *roo

if (path1 != NULL) {
if (path2 == NULL || memcmp(path1, path2, (int) strlen(path2)) != 0) {
free(current_dir);
free(path2);
free(vendor_file);
free(blade_file_name);
Expand All @@ -171,6 +172,7 @@ char *resolve_import_path(char *module_name, const char *current_file, char *roo

if (path1 != NULL) {
if (path2 == NULL || memcmp(path1, path2, (int) strlen(path2)) != 0) {
free(current_dir);
free(path2);
free(vendor_index_file);
free(blade_file_name);
Expand All @@ -191,6 +193,7 @@ char *resolve_import_path(char *module_name, const char *current_file, char *roo

if (path1 != NULL) {
if (path2 == NULL || memcmp(path1, path2, (int) strlen(path2)) != 0) {
free(current_dir);
free(path2);
free(current_vendor_file);
free(blade_file_name);
Expand All @@ -214,6 +217,7 @@ char *resolve_import_path(char *module_name, const char *current_file, char *roo

if (path1 != NULL) {
if (path2 == NULL || memcmp(path1, path2, (int) strlen(path2)) != 0) {
free(current_dir);
free(path2);
free(current_vendor_index_file);
free(blade_file_name);
Expand All @@ -222,6 +226,7 @@ char *resolve_import_path(char *module_name, const char *current_file, char *roo
}
}
free(current_vendor_index_file);
free(current_dir);

// then, check in blade's default locations
char *exe_dir = get_exe_dir();
Expand All @@ -236,6 +241,7 @@ char *resolve_import_path(char *module_name, const char *current_file, char *roo

if (path1 != NULL) {
if (path2 == NULL || memcmp(path1, path2, (int) strlen(path2)) != 0) {
free(blade_directory);
free(path2);
free(library_file);
free(blade_file_name);
Expand All @@ -256,6 +262,7 @@ char *resolve_import_path(char *module_name, const char *current_file, char *roo

if (path1 != NULL) {
if (path2 == NULL || memcmp(path1, path2, (int) strlen(path2)) != 0) {
free(blade_directory);
free(path2);
free(library_index_file);
free(blade_file_name);
Expand Down
9 changes: 6 additions & 3 deletions src/standard/os.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,10 @@ b_value get_blade_os_path_separator(b_vm *vm) {

b_value get_blade_os_exe_path(b_vm *vm) {
char *path = get_exe_path();
return STRING_VAL(path);
if(path) {
return STRING_TT_VAL(path);
}
return NIL_VAL;
}

DECLARE_MODULE_METHOD(os_getenv) {
Expand Down Expand Up @@ -336,9 +339,9 @@ static int remove_directory(char *path, int path_length, bool recursive) {
} else if(unlink(path_string) == -1) {
free(path_string);
return -1;
} else {
free(path_string);
}

free(path_string);
} else {
free(path_string);
return -1;
Expand Down
1 change: 1 addition & 0 deletions src/utf8.c
Original file line number Diff line number Diff line change
Expand Up @@ -3015,6 +3015,7 @@ char *utf8_case_fold(char *str, int str_len, bool simple, size_t *out_length) {
}
}

free(s);
return result;
}

Expand Down

0 comments on commit d2f14ce

Please sign in to comment.