Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit df5990db authored by Ian Rogers's avatar Ian Rogers Committed by Greg Kroah-Hartman
Browse files

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: default avatarIan 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: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
parent b380d09e
Loading
Loading
Loading
Loading
+9 −6
Original line number Diff line number Diff line
@@ -569,14 +569,17 @@ static int report__browse_hists(struct report *rep)
	int ret;
	struct perf_session *session = rep->session;
	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 ;-) */
		help = perf_tip(DOCDIR);
		if (help == NULL)
			help = "Cannot load tips.txt file, please install perf!";
		free(path);
		path = system_path(DOCDIR);
		if (perf_tip(&help, path) || help == NULL)
			help = strdup("Cannot load tips.txt file, please install perf!");
	}
	free(path);

	switch (use_browser) {
	case 1:
@@ -598,7 +601,7 @@ static int report__browse_hists(struct report *rep)
		ret = perf_evlist__tty_browse_hists(evlist, rep, help);
		break;
	}

	free(help);
	return ret;
}

+7 −7
Original line number Diff line number Diff line
@@ -343,32 +343,32 @@ fetch_kernel_version(unsigned int *puint, char *str,
	return 0;
}

const char *perf_tip(const char *dirpath)
int perf_tip(char **strp, const char *dirpath)
{
	struct strlist *tips;
	struct str_node *node;
	char *tip = NULL;
	struct strlist_config conf = {
		.dirname = dirpath,
		.file_only = true,
	};
	int ret = 0;

	*strp = NULL;
	tips = strlist__new("tips.txt", &conf);
	if (tips == NULL)
		return errno == ENOENT ? NULL :
			"Tip: check path of tips.txt or get more memory! ;-p";
		return -errno;

	if (strlist__nr_entries(tips) == 0)
		goto out;

	node = strlist__entry(tips, random() % strlist__nr_entries(tips));
	if (asprintf(&tip, "Tip: %s", node->s) < 0)
		tip = (char *)"Tip: get more memory! ;-)";
	if (asprintf(strp, "Tip: %s", node->s) < 0)
		ret = -ENOMEM;

out:
	strlist__delete(tips);

	return tip;
	return ret;
}

char *perf_exe(char *buf, int len)
+1 −1
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ int fetch_kernel_version(unsigned int *puint,
#define KVER_FMT	"%d.%d.%d"
#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
int sched_getcpu(void);