fix potential memleak in perf events parser
Subsystems (4)
v2 cover fix potential memleak in perf events parser
fix some memleaks for parse_events_term__sym_hw and parse_events_term__clone.
v1 ==> v2
1. split into two patches
2. add jump targets common exception handling
3. add Fixes tag
Chen Wandun (1):
perf tools: fix potential memleak in perf events parser
Cheng Jian (1):
perf tools: fix potential memleak in perf events parser
tools/perf/util/parse-events.c | 51 ++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 8 deletions(-)
Signature · 1 line
2.17.1
PATCH v2 1/2 perf tools: fix potential memleak in perf events parser
From: Cheng Jian <[email protected]>
Fix memory leak of in function parse_events_term__sym_hw() and parse_events_term__clone() when error occur.
--- tools/perf/util/parse-events.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
tools/perf/util/parse-events.c +10 -2
@@ -2957,8 +2957,12 @@ int parse_events_term__sym_hw(struct parse_events_term **term, sym = &event_symbols_hw[idx]; str =strdup (sym->symbol); -if (!str) +if (!str) { +if (!config) +free (temp.config);return -ENOMEM ; + } +return new_term (term, &temp, str,0 ); }
@@ -2983,8 +2987,12 @@ int parse_events_term__clone(struct parse_events_term **new,return new_term (new, &temp,NULL , term->val.num); str =strdup (term->val.str); -if (!str) +if (!str) { +if (term->config) +free (temp.config);return -ENOMEM ; + } +return new_term (new, &temp, str,0 ); }
Signature · 1 line
2.17.1
Re: [PATCH v2 1/2] perf tools: Fix potential memory leaks in perf events parser
Quoted 2 lines
Fix memory leak of in function
parse_events_term__sym_hw()andparse_events_term__clone()when error occur.
How do you think about a wording variant like the following?
Release a configuration object after a string duplication failed.
Regards,
Markus
Re: [PATCH v2 1/2] perf tools: Fix potential memory leaks in perf events parser
Quoted 9 lines · Markus Elfring
Quoted 2 lines
Fix memory leak of in function
parse_events_term__sym_hw()andparse_events_term__clone()when error occur.How do you think about a wording variant like the following?
Release a configuration object after a string duplication failed.
Regards,
Markus
Hi,
This is the semi-friendly patch-bot of Greg Kroah-Hartman.
Markus, you seem to have sent a nonsensical or otherwise pointless review comment to a patch submission on a Linux kernel developer mailing list. I strongly suggest that you not do this anymore. Please do not bother developers who are actively working to produce patches and features with comments that, in the end, are a waste of time.
Patch submitter, please ignore Markus's suggestion; you do not need to follow it at all. The person/bot/AI that sent it is being ignored by almost all Linux kernel maintainers for having a persistent pattern of behavior of producing distracting and pointless commentary, and inability to adapt to feedback. Please feel free to also ignore emails from them.
thanks,
greg k-h's patch email bot
Re: [PATCH v2 1/2] perf tools: Fix potential memory leaks in perf events parser
In reply to Markus ElfringQuoted 5 lines · Markus Elfring
Quoted 2 lines
Fix memory leak of in function
parse_events_term__sym_hw()andparse_events_term__clone()when error occur.How do you think about a wording variant like the following?
Release a configuration object after a string duplication failed.
Thank you for your reply, I will update in v3
Best Regards,
Chen Wandun
Quoted 4 lines
Regards,
Markus
PATCH v2 2/2 perf tools: fix potential memleak in perf events parser
In reply to Chen WandunFix potential memory leak. Function new_term may return error, so it is need to free memory when the return value is negative.
--- tools/perf/util/parse-events.c | 47 ++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 10 deletions(-)
tools/perf/util/parse-events.c +37 -10
@@ -2947,6 +2947,7 @@ int parse_events_term__sym_hw(struct parse_events_term **term, .type_term = PARSE_EVENTS__TERM_TYPE_USER, .config = config, }; +int ret;if (!temp.config) { temp.config =strdup ("event" );
@@ -2958,12 +2959,23 @@ int parse_events_term__sym_hw(struct parse_events_term **term, str =strdup (sym->symbol);if (!str) { -if (!config) -free (temp.config); -return -ENOMEM ; + ret = -ENOMEM ; + goto free_config; } -return new_term (term, &temp, str,0 ); + ret =new_term (term, &temp, str,0 ); +if (ret) + goto free_str; + +return 0 ; + +free_str: +free (str); +free_config: +if (!config) +free (temp.config); + +return ret; } int parse_events_term__clone(struct parse_events_term **new,
@@ -2977,23 +2989,38 @@ int parse_events_term__clone(struct parse_events_term **new, .err_term = term->err_term, .err_val = term->err_val, }; +int ret;if (term->config) { temp.config =strdup (term->config);if (!temp.config)return -ENOMEM ; } -if (term->type_val ==PARSE_EVENTS__TERM_TYPE_NUM ) -return new_term (new, &temp,NULL , term->val.num); +if (term->type_val ==PARSE_EVENTS__TERM_TYPE_NUM ) { + ret =new_term (new, &temp,NULL , term->val.num); +if (ret) + goto free_config; + } str =strdup (term->val.str);if (!str) { -if (term->config) -free (temp.config); -return -ENOMEM ; + ret = -ENOMEM ; + goto free_config; } -return new_term (new, &temp, str,0 ); + ret =new_term (new, &temp, str,0 ); +if (ret) + goto free_str; + +return 0 ; + +free_str: +free (str); +free_config: +if (term->config) +free (temp.config); + +return ret; }void parse_events_term__delete (struct parse_events_term *term)
Signature · 1 line
2.17.1
Re: [PATCH v2 2/2] perf tools: Improve exception handling in two functions of perf events parser
Quoted 2 lines
Fix potential memory leak. Function
new_termmay return error, so it is need to free memory when the return value is negative.
How do you think about a wording variant like the following?
Add jump targets so that a configuration object and a duplicated string are released after a call of the function “strdup” or “new_term” failed.
Regards,
Markus