Skip to content

Commit 41d0c31

Browse files
zaxarikuba-moo
authored andcommitted
tools: ynl: call nested attribute free function for indexed arrays
When freeing indexed arrays, the corresponding free function should be called for each entry of the indexed array. For example, for for 'struct tc_act_attrs' 'tc_act_attrs_free(...)' needs to be called for each entry. Previously, memory leaks were reported when enabling the ASAN analyzer. ================================================================= ==874==ERROR: LeakSanitizer: detected memory leaks Direct leak of 24 byte(s) in 1 object(s) allocated from: #0 0x7f221fd20cb5 in malloc ./debug/gcc/gcc/libsanitizer/asan/asan_malloc_linux.cpp:67 kernel-patches#1 0x55c98db048af in tc_act_attrs_set_options_vlan_parms ../generated/tc-user.h:2813 kernel-patches#2 0x55c98db048af in main ./linux/tools/net/ynl/samples/tc-filter-add.c:71 Direct leak of 24 byte(s) in 1 object(s) allocated from: #0 0x7f221fd20cb5 in malloc ./debug/gcc/gcc/libsanitizer/asan/asan_malloc_linux.cpp:67 kernel-patches#1 0x55c98db04a93 in tc_act_attrs_set_options_vlan_parms ../generated/tc-user.h:2813 kernel-patches#2 0x55c98db04a93 in main ./linux/tools/net/ynl/samples/tc-filter-add.c:74 Direct leak of 10 byte(s) in 2 object(s) allocated from: #0 0x7f221fd20cb5 in malloc ./debug/gcc/gcc/libsanitizer/asan/asan_malloc_linux.cpp:67 kernel-patches#1 0x55c98db0527d in tc_act_attrs_set_kind ../generated/tc-user.h:1622 SUMMARY: AddressSanitizer: 58 byte(s) leaked in 4 allocation(s). The following diff illustrates the changes introduced compared to the previous version of the code. void tc_flower_attrs_free(struct tc_flower_attrs *obj) { + unsigned int i; + free(obj->indev); + for (i = 0; i < obj->_count.act; i++) + tc_act_attrs_free(&obj->act[i]); free(obj->act); free(obj->key_eth_dst); free(obj->key_eth_dst_mask); Signed-off-by: Zahari Doychev <zahari.doychev@linux.com> Reviewed-by: Jakub Kicinski <kuba@kernel.org> Link: https://patch.msgid.link/20251106151529.453026-3-zahari.doychev@linux.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 762e7e1 commit 41d0c31

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

tools/net/ynl/pyynl/ynl_gen_c.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,18 @@ def _setter_lines(self, ri, member, presence):
861861
return [f"{member} = {self.c_name};",
862862
f"{presence} = n_{self.c_name};"]
863863

864+
def free_needs_iter(self):
865+
return self.sub_type == 'nest'
866+
867+
def _free_lines(self, ri, var, ref):
868+
lines = []
869+
if self.sub_type == 'nest':
870+
lines += [
871+
f"for (i = 0; i < {var}->{ref}_count.{self.c_name}; i++)",
872+
f'{self.nested_render_name}_free(&{var}->{ref}{self.c_name}[i]);',
873+
]
874+
lines += f"free({var}->{ref}{self.c_name});",
875+
return lines
864876

865877
class TypeNestTypeValue(Type):
866878
def _complex_member_type(self, ri):

0 commit comments

Comments
 (0)