Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf disasm: Use disasm_line__free() to properly free disasm_line

symbol__disassemble_capstone_powerpc() goto the 'err' label when it
failed in the loop that created disasm_line, and then used free()
directly to free disasm_line. Since the structure disasm_line contains
members that allocate memory dynamically, this can result in a memory
leak. In fact, we can simply break the loop when it fails in the middle
of the loop, and disasm_line__free() will then be called to properly
free the created line. Other error paths do not need to consider freeing
disasm_line.

Fixes: c5d60de1813a ("perf annotate: Add support to use libcapstone in powerpc")
Signed-off-by: Li Huafei <lihuafei1@huawei.com>
Tested-by: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: sesse@google.com
Cc: kjain@linux.ibm.com
Link: https://lore.kernel.org/r/20241019154157.282038-2-lihuafei1@huawei.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>

authored by

Li Huafei and committed by
Namhyung Kim
908d50e5 b4e0e9a1

+1 -13
+1 -13
tools/perf/util/disasm.c
··· 1573 1573 1574 1574 dl = disasm_line__new(args); 1575 1575 if (dl == NULL) 1576 - goto err; 1576 + break; 1577 1577 1578 1578 annotation_line__add(&dl->al, &notes->src->source); 1579 1579 ··· 1603 1603 err: 1604 1604 if (fd >= 0) 1605 1605 close(fd); 1606 - if (needs_cs_close) { 1607 - struct disasm_line *tmp; 1608 - 1609 - /* 1610 - * It probably failed in the middle of the above loop. 1611 - * Release any resources it might add. 1612 - */ 1613 - list_for_each_entry_safe(dl, tmp, &notes->src->source, al.node) { 1614 - list_del(&dl->al.node); 1615 - free(dl); 1616 - } 1617 - } 1618 1606 count = -1; 1619 1607 goto out; 1620 1608 }