Fix memory leak in A_ALLOC

Need to free dst page if already allocated.
This commit is contained in:
Nunuhara Cabbage
2021-04-27 19:15:28 -07:00
parent 9b84e38874
commit 0274052947
2 changed files with 16 additions and 3 deletions
+2
View File
@@ -375,6 +375,8 @@ void array_copy(struct page *dst, int dst_i, struct page *src, int src_i, int n)
ERROR("Not an array");
if (!array_index_ok(dst, dst_i) || !array_index_ok(src, src_i))
ERROR("Out of bounds array access");
if (!array_index_ok(dst, dst_i + n - 1) || !array_index_ok(src, src_i + n - 1))
ERROR("Out of bounds array access");
if (dst->rank != 1 || src->rank != 1)
ERROR("Tried to copy to/from a multi-dimensional array");
if (dst->a_type != src->a_type)
+14 -3
View File
@@ -1486,6 +1486,10 @@ static enum opcode execute_instruction(enum opcode opcode)
int pageno = stack_peek(rank+1).i;
int array = heap[pageno].page->values[varno].i;
enum ain_data_type data_type = variable_type(heap[pageno].page, varno, &struct_type, NULL);
if (heap[array].page) {
delete_page_vars(heap[array].page);
free_page(heap[array].page);
}
heap_set_page(array, alloc_array(rank, stack_peek_ptr(rank-1), data_type, struct_type, true));
stack_ptr -= rank + 2;
break;
@@ -1745,18 +1749,25 @@ static void describe_page(struct page *page)
return;
}
char *u;
switch (page->type) {
case GLOBAL_PAGE:
sys_message("GLOBAL_PAGE\n");
break;
case LOCAL_PAGE:
sys_message("LOCAL_PAGE: %s\n", ain->functions[page->index].name);
u = sjis2utf(ain->functions[page->index].name, 0);
sys_message("LOCAL_PAGE: %s\n", u);
free(u);
break;
case STRUCT_PAGE:
sys_message("STRUCT_PAGE: %s\n", ain->structures[page->index].name);
u = sjis2utf(ain->structures[page->index].name, 0);
sys_message("STRUCT_PAGE: %s\n", u);
free(u);
break;
case ARRAY_PAGE:
sys_message("ARRAY_PAGE: %s\n", ain_strtype(ain, page->a_type, page->struct_type));
u = sjis2utf(ain_strtype(ain, page->a_type, page->struct_type), 0);
sys_message("ARRAY_PAGE: %s\n", u);
free(u);
break;
}
}