Skip to content

Commit 1785fbb

Browse files
yeyunfeng-devacmel
authored andcommitted
perf jevents: Fix resource leak in process_mapfile() and main()
There are memory leaks and file descriptor resource leaks in process_mapfile() and main(). Fix this by adding free(), fclose() and free_arch_std_events() on the error paths. Fixes: 80eeb67 ("perf jevents: Program to convert JSON file") Fixes: 3f056b6 ("perf jevents: Make build fail on JSON parse error") Fixes: e9d32c1 ("perf vendor events: Add support for arch standard events") Signed-off-by: Yunfeng Ye <yeyunfeng@huawei.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Feilong Lin <linfeilong@huawei.com> Cc: Hu Shiyuan <hushiyuan@huawei.com> Cc: Jiri Olsa <jolsa@redhat.com> Cc: John Garry <john.garry@huawei.com> Cc: Kan Liang <kan.liang@linux.intel.com> Cc: Luke Mujica <lukemujica@google.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Zenghui Yu <yuzenghui@huawei.com> Link: http://lore.kernel.org/lkml/d7907042-ec9c-2bef-25b4-810e14602f89@huawei.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 91e2f53 commit 1785fbb

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

‎tools/perf/pmu-events/jevents.c‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,7 @@ static int process_mapfile(FILE *outfp, char *fpath)
772772
char *line, *p;
773773
int line_num;
774774
char *tblname;
775+
int ret = 0;
775776

776777
pr_info("%s: Processing mapfile %s\n", prog, fpath);
777778

@@ -783,6 +784,7 @@ static int process_mapfile(FILE *outfp, char *fpath)
783784
if (!mapfp) {
784785
pr_info("%s: Error %s opening %s\n", prog, strerror(errno),
785786
fpath);
787+
free(line);
786788
return -1;
787789
}
788790

@@ -809,7 +811,8 @@ static int process_mapfile(FILE *outfp, char *fpath)
809811
/* TODO Deal with lines longer than 16K */
810812
pr_info("%s: Mapfile %s: line %d too long, aborting\n",
811813
prog, fpath, line_num);
812-
return -1;
814+
ret = -1;
815+
goto out;
813816
}
814817
line[strlen(line)-1] = '\0';
815818

@@ -839,7 +842,9 @@ static int process_mapfile(FILE *outfp, char *fpath)
839842

840843
out:
841844
print_mapping_table_suffix(outfp);
842-
return 0;
845+
fclose(mapfp);
846+
free(line);
847+
return ret;
843848
}
844849

845850
/*
@@ -1136,6 +1141,7 @@ int main(int argc, char *argv[])
11361141
goto empty_map;
11371142
} else if (rc < 0) {
11381143
/* Make build fail */
1144+
fclose(eventsfp);
11391145
free_arch_std_events();
11401146
return 1;
11411147
} else if (rc) {
@@ -1148,6 +1154,7 @@ int main(int argc, char *argv[])
11481154
goto empty_map;
11491155
} else if (rc < 0) {
11501156
/* Make build fail */
1157+
fclose(eventsfp);
11511158
free_arch_std_events();
11521159
return 1;
11531160
} else if (rc) {
@@ -1165,6 +1172,8 @@ int main(int argc, char *argv[])
11651172
if (process_mapfile(eventsfp, mapfile)) {
11661173
pr_info("%s: Error processing mapfile %s\n", prog, mapfile);
11671174
/* Make build fail */
1175+
fclose(eventsfp);
1176+
free_arch_std_events();
11681177
return 1;
11691178
}
11701179

0 commit comments

Comments
 (0)