perf report: Fix memory leaks around perf_tip()

[ Upstream commit d9fc706108c15f8bc2d4ccccf8e50f74830fabd9 ]

perf_tip() may allocate memory or use a literal, this means memory
wasn't freed if allocated. Change the API so that literals aren't used.

At the same time add missing frees for system_path. These issues were
spotted using leak sanitizer.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lore.kernel.org/lkml/20211118073804.2149974-1-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
Ian Rogers
2021-11-17 23:38:04 -08:00
committed by Greg Kroah-Hartman
parent a4c17ebdd6
commit ff061b5bda
3 changed files with 17 additions and 14 deletions

View File

@@ -610,14 +610,17 @@ static int report__browse_hists(struct report *rep)
int ret; int ret;
struct perf_session *session = rep->session; struct perf_session *session = rep->session;
struct evlist *evlist = session->evlist; struct evlist *evlist = session->evlist;
const char *help = perf_tip(system_path(TIPDIR)); char *help = NULL, *path = NULL;
if (help == NULL) { path = system_path(TIPDIR);
if (perf_tip(&help, path) || help == NULL) {
/* fallback for people who don't install perf ;-) */ /* fallback for people who don't install perf ;-) */
help = perf_tip(DOCDIR); free(path);
if (help == NULL) path = system_path(DOCDIR);
help = "Cannot load tips.txt file, please install perf!"; if (perf_tip(&help, path) || help == NULL)
help = strdup("Cannot load tips.txt file, please install perf!");
} }
free(path);
switch (use_browser) { switch (use_browser) {
case 1: case 1:
@@ -644,7 +647,7 @@ static int report__browse_hists(struct report *rep)
ret = perf_evlist__tty_browse_hists(evlist, rep, help); ret = perf_evlist__tty_browse_hists(evlist, rep, help);
break; break;
} }
free(help);
return ret; return ret;
} }

View File

@@ -379,32 +379,32 @@ fetch_kernel_version(unsigned int *puint, char *str,
return 0; return 0;
} }
const char *perf_tip(const char *dirpath) int perf_tip(char **strp, const char *dirpath)
{ {
struct strlist *tips; struct strlist *tips;
struct str_node *node; struct str_node *node;
char *tip = NULL;
struct strlist_config conf = { struct strlist_config conf = {
.dirname = dirpath, .dirname = dirpath,
.file_only = true, .file_only = true,
}; };
int ret = 0;
*strp = NULL;
tips = strlist__new("tips.txt", &conf); tips = strlist__new("tips.txt", &conf);
if (tips == NULL) if (tips == NULL)
return errno == ENOENT ? NULL : return -errno;
"Tip: check path of tips.txt or get more memory! ;-p";
if (strlist__nr_entries(tips) == 0) if (strlist__nr_entries(tips) == 0)
goto out; goto out;
node = strlist__entry(tips, random() % strlist__nr_entries(tips)); node = strlist__entry(tips, random() % strlist__nr_entries(tips));
if (asprintf(&tip, "Tip: %s", node->s) < 0) if (asprintf(strp, "Tip: %s", node->s) < 0)
tip = (char *)"Tip: get more memory! ;-)"; ret = -ENOMEM;
out: out:
strlist__delete(tips); strlist__delete(tips);
return tip; return ret;
} }
char *perf_exe(char *buf, int len) char *perf_exe(char *buf, int len)

View File

@@ -39,7 +39,7 @@ int fetch_kernel_version(unsigned int *puint,
#define KVER_FMT "%d.%d.%d" #define KVER_FMT "%d.%d.%d"
#define KVER_PARAM(x) KVER_VERSION(x), KVER_PATCHLEVEL(x), KVER_SUBLEVEL(x) #define KVER_PARAM(x) KVER_VERSION(x), KVER_PATCHLEVEL(x), KVER_SUBLEVEL(x)
const char *perf_tip(const char *dirpath); int perf_tip(char **strp, const char *dirpath);
#ifndef HAVE_SCHED_GETCPU_SUPPORT #ifndef HAVE_SCHED_GETCPU_SUPPORT
int sched_getcpu(void); int sched_getcpu(void);