Re: [PATCH] perf jevents: Fix resource leak in process_mapfile()

From: Yunfeng Ye
Date: Wed Oct 16 2019 - 06:59:13 EST




On 2019/10/16 18:40, John Garry wrote:
> On 16/10/2019 08:47, Yunfeng Ye wrote:
>> There are memory leaks and file descriptor resource leaks in
>> process_mapfile().
>>
>> Fix this by adding free() and fclose() on the error paths.
>>
>> Fixes: 80eeb67fe577 ("perf jevents: Program to convert JSON file")
>> Signed-off-by: Yunfeng Ye <yeyunfeng@xxxxxxxxxx>
>> ---
>> Âtools/perf/pmu-events/jevents.c | 9 +++++++--
>> Â1 file changed, 7 insertions(+), 2 deletions(-)
>>
>> diff --git a/tools/perf/pmu-events/jevents.c b/tools/perf/pmu-events/jevents.c
>> index e2837260ca4d..6e60d4cff592 100644
>> --- a/tools/perf/pmu-events/jevents.c
>> +++ b/tools/perf/pmu-events/jevents.c
>> @@ -758,6 +758,7 @@ static int process_mapfile(FILE *outfp, char *fpath)
>> ÂÂÂÂ char *line, *p;
>> ÂÂÂÂ int line_num;
>> ÂÂÂÂ char *tblname;
>> +ÂÂÂ int ret = 0;
>>
>> ÂÂÂÂ pr_info("%s: Processing mapfile %s\n", prog, fpath);
>>
>> @@ -769,6 +770,7 @@ static int process_mapfile(FILE *outfp, char *fpath)
>> ÂÂÂÂ if (!mapfp) {
>> ÂÂÂÂÂÂÂÂ pr_info("%s: Error %s opening %s\n", prog, strerror(errno),
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ fpath);
>> +ÂÂÂÂÂÂÂ free(line);
>> ÂÂÂÂÂÂÂÂ return -1;
>> ÂÂÂÂ }
>>
>> @@ -795,7 +797,8 @@ static int process_mapfile(FILE *outfp, char *fpath)
>> ÂÂÂÂÂÂÂÂÂÂÂÂ /* TODO Deal with lines longer than 16K */
>> ÂÂÂÂÂÂÂÂÂÂÂÂ pr_info("%s: Mapfile %s: line %d too long, aborting\n",
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ prog, fpath, line_num);
>> -ÂÂÂÂÂÂÂÂÂÂÂ return -1;
>> +ÂÂÂÂÂÂÂÂÂÂÂ ret = -1;
>> +ÂÂÂÂÂÂÂÂÂÂÂ goto out;
>
> There's a subtle change of behaviour here, i.e. now calling print_mapping_table_suffix(), but I don't think that it makes any difference.
>
yes, I know that "goto out" will run print_mapping_table_suffix(outfp), because the error path before is done like this.
so I think it should be use "goto out" to run run print_mapping_table_suffix(outfp).

> However, does outfp remain open also in this case:
>
Because it has a comment that "Make build fail", so I am not handle the outfp, only modify the process_mapfile() function.

> main(int argc, char *argv[])
> {
> ...
>
> if (process_mapfile(eventsfp, mapfile)) {
> ÂÂÂÂpr_info("%s: Error processing mapfile %s\n", prog, mapfile);
> ÂÂÂÂ/* Make build fail */
> ÂÂÂÂreturn 1;
> }
>
> return 0;
>
> empty_map:
> ÂÂÂÂfclose(eventsfp);
> ÂÂÂÂ...
> }
>
> I think that this code works on the basis that the program exits on any sort of error and releases resources automatically. Having said that, it is a good practice to tidy up.
>
I agree with you, when program exits, it will releases resources automatically. It's just to make the program clearer and more correct.

> John
>
>> ÂÂÂÂÂÂÂÂ }
>> ÂÂÂÂÂÂÂÂ line[strlen(line)-1] = '\0';
>>
>> @@ -825,7 +828,9 @@ static int process_mapfile(FILE *outfp, char *fpath)
>>
>> Âout:
>> ÂÂÂÂ print_mapping_table_suffix(outfp);
>> -ÂÂÂ return 0;
>> +ÂÂÂ fclose(mapfp);
>> +ÂÂÂ free(line);
>> +ÂÂÂ return ret;
>> Â}
>>
>> Â/*
>>
>
>
>
> .
>