Skip to content

Commit 8159afb

Browse files
kknjhFeng Yang
authored andcommitted
libbpf-tools/ksnoop: Fix the memory leak in the structure
Add a dedicated deallocation function to include the release of data within the structure Signed-off-by: Feng Yang <yangfeng@kylinos.cn>
1 parent 9129493 commit 8159afb

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

libbpf-tools/ksnoop.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ static int parse_traces(int argc, char **argv, struct trace *traces)
639639

640640
for (i = 0; i < argc; i++) {
641641
if (parse_trace(argv[i], &(traces[i])))
642-
return -EINVAL;
642+
return -i - 1;
643643

644644
if (!stack_mode || i == 0)
645645
continue;
@@ -668,6 +668,19 @@ static int alloc_traces(int argc, struct trace **traces)
668668
return 0;
669669
}
670670

671+
static void free_traces(struct trace *traces, __u8 num)
672+
{
673+
__u8 i;
674+
675+
for (i = 0; i < num; ++i) {
676+
btf_dump__free(traces[i].dump);
677+
if (traces[i].btf != vmlinux_btf)
678+
btf__free(traces[i].btf);
679+
}
680+
btf__free(vmlinux_btf);
681+
free(traces);
682+
}
683+
671684
static int cmd_info(int argc, char **argv)
672685
{
673686
struct trace *traces = NULL;
@@ -681,8 +694,8 @@ static int cmd_info(int argc, char **argv)
681694

682695
nr_traces = parse_traces(argc, argv, traces);
683696
if (nr_traces < 0) {
684-
free(traces);
685-
return nr_traces;
697+
free_traces(traces, -nr_traces);
698+
return -EINVAL;
686699
}
687700

688701
for (i = 0; i < nr_traces; i++) {
@@ -703,7 +716,7 @@ static int cmd_info(int argc, char **argv)
703716
func->nr_args - MAX_ARGS);
704717
printf(");\n");
705718
}
706-
free(traces);
719+
free_traces(traces, nr_traces);
707720
return 0;
708721
}
709722

@@ -868,8 +881,8 @@ static int cmd_trace(int argc, char **argv)
868881

869882
nr_traces = parse_traces(argc, argv, traces);
870883
if (nr_traces < 0) {
871-
free(traces);
872-
return nr_traces;
884+
free_traces(traces, -nr_traces);
885+
return -EINVAL;
873886
}
874887

875888
skel = ksnoop_bpf__open_and_load();
@@ -931,7 +944,7 @@ static int cmd_trace(int argc, char **argv)
931944
bpf_link__destroy(traces[i].links[0]);
932945
bpf_link__destroy(traces[i].links[1]);
933946
}
934-
free(traces);
947+
free_traces(traces, nr_traces);
935948
perf_buffer__free(pb);
936949
ksnoop_bpf__destroy(skel);
937950

0 commit comments

Comments
 (0)