1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix memleak for [][]T{len: x}, or []Struct{len: x} (#18763)

This commit is contained in:
Delyan Angelov 2023-07-04 06:32:19 +03:00 committed by GitHub
parent 2e9f8e6633
commit 9bf2449a7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 12 deletions

View File

@ -316,8 +316,8 @@ pub fn malloc(n isize) &u8 {
C.fprintf(C.stderr, c'_v_malloc %6d total %10d\n', n, total_m)
// print_backtrace()
}
if n <= 0 {
panic('malloc(${n} <= 0)')
if n < 0 {
panic('malloc(${n} < 0)')
}
$if vplayground ? {
if n > 10000 {
@ -359,8 +359,8 @@ pub fn malloc_noscan(n isize) &u8 {
C.fprintf(C.stderr, c'malloc_noscan %6d total %10d\n', n, total_m)
// print_backtrace()
}
if n <= 0 {
panic('malloc_noscan(${n} <= 0)')
if n < 0 {
panic('malloc_noscan(${n} < 0)')
}
$if vplayground ? {
if n > 10000 {
@ -418,8 +418,8 @@ pub fn malloc_uncollectable(n isize) &u8 {
C.fprintf(C.stderr, c'malloc_uncollectable %6d total %10d\n', n, total_m)
// print_backtrace()
}
if n <= 0 {
panic('malloc_uncollectable(${n} <= 0)')
if n < 0 {
panic('malloc_uncollectable(${n} < 0)')
}
$if vplayground ? {
if n > 10000 {

View File

@ -388,7 +388,7 @@ fn (mut g Gen) array_init_with_fields(node ast.ArrayInit, elem_type Type, is_amp
tmp := g.new_tmp_var()
line := g.go_before_stmt(0).trim_space()
g.empty_line = true
g.write('${elem_styp}* ${tmp} = malloc((')
g.write('${elem_styp}* ${tmp} = (${elem_styp}*) _v_malloc((')
g.expr(node.len_expr)
g.writeln(') * sizeof(${elem_styp}));')
ind := g.new_tmp_var()

View File

@ -79,7 +79,7 @@ fn (mut g Gen) spawn_and_go_expr(node ast.SpawnExpr, mode SpawnGoMode) {
wrapper_fn_name := name + '_thread_wrapper'
arg_tmp_var := 'arg_' + tmp
if is_spawn {
g.writeln('${wrapper_struct_name} *${arg_tmp_var} = malloc(sizeof(thread_arg_${name}));')
g.writeln('${wrapper_struct_name} *${arg_tmp_var} = (${wrapper_struct_name} *) _v_malloc(sizeof(thread_arg_${name}));')
} else if is_go {
g.writeln('${wrapper_struct_name} ${arg_tmp_var};')
}
@ -106,7 +106,7 @@ fn (mut g Gen) spawn_and_go_expr(node ast.SpawnExpr, mode SpawnGoMode) {
}
s_ret_typ := g.typ(node.call_expr.return_type)
if g.pref.os == .windows && node.call_expr.return_type != ast.void_type {
g.writeln('${arg_tmp_var}->ret_ptr = malloc(sizeof(${s_ret_typ}));')
g.writeln('${arg_tmp_var}->ret_ptr = (void *) _v_malloc(sizeof(${s_ret_typ}));')
}
is_opt := node.call_expr.return_type.has_flag(.option)
is_res := node.call_expr.return_type.has_flag(.result)
@ -201,7 +201,7 @@ fn (mut g Gen) spawn_and_go_expr(node ast.SpawnExpr, mode SpawnGoMode) {
}
if node.call_expr.return_type != ast.void_type {
g.gowrappers.writeln('\t${s_ret_typ} ret = *ret_ptr;')
g.gowrappers.writeln('\tfree(ret_ptr);')
g.gowrappers.writeln('\t_v_free(ret_ptr);')
g.gowrappers.writeln('\treturn ret;')
}
g.gowrappers.writeln('}')
@ -278,7 +278,7 @@ fn (mut g Gen) spawn_and_go_expr(node ast.SpawnExpr, mode SpawnGoMode) {
if g.pref.os == .windows {
g.gowrappers.write_string('\t*((${s_ret_typ}*)(arg->ret_ptr)) = ')
} else {
g.gowrappers.writeln('\t${s_ret_typ}* ret_ptr = malloc(sizeof(${s_ret_typ}));')
g.gowrappers.writeln('\t${s_ret_typ}* ret_ptr = (${s_ret_typ}*) _v_malloc(sizeof(${s_ret_typ}));')
g.gowrappers.write_string('\t*ret_ptr = ')
}
} else {
@ -359,7 +359,7 @@ fn (mut g Gen) spawn_and_go_expr(node ast.SpawnExpr, mode SpawnGoMode) {
}
}
g.gowrappers.writeln(');')
g.gowrappers.writeln('\tfree(arg);')
g.gowrappers.writeln('\t_v_free(arg);')
if g.pref.os != .windows && node.call_expr.return_type != ast.void_type {
g.gowrappers.writeln('\treturn ret_ptr;')
} else {