From 1848eb097362bac77c83609b9d26526d5220813a Mon Sep 17 00:00:00 2001 From: Emily Hudson Date: Sat, 27 Jun 2020 16:00:27 +0100 Subject: [PATCH] cgen,map: fix leaks in `keys()` and `for x in y` --- vlib/builtin/map.v | 2 +- vlib/v/gen/cgen.v | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/vlib/builtin/map.v b/vlib/builtin/map.v index 7348aa428d..e9d403f8c6 100644 --- a/vlib/builtin/map.v +++ b/vlib/builtin/map.v @@ -448,7 +448,7 @@ pub fn (mut m map) delete(key string) { // Returns all keys in the map. // TODO: add optimization in case of no deletes pub fn (m &map) keys() []string { - mut keys := [''].repeat(m.len) + mut keys := []string{ len:m.len } mut j := 0 for i := u32(0); i < m.key_values.len; i++ { if m.key_values.keys[i].str == 0 { diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 2cb0bc2de8..3235f2e0fc 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -841,7 +841,10 @@ fn (mut g Gen) for_in(it ast.ForInStmt) { g.writeln(', $key, &($val_styp[]){ $zero }));') } g.stmts(it.stmts) + g.writeln('/* for in map cleanup*/ string_free(&$key);') g.writeln('}') + g.writeln('/*for in map cleanup*/') + g.writeln('array_free(&$keys_tmp);') } else if it.cond_type.has_flag(.variadic) { g.writeln('// FOR IN cond_type/variadic') i := if it.key_var in ['', '_'] { g.new_tmp_var() } else { it.key_var }