Skip to content

Commit d9fc706

Browse files
captain5050acmel
authored andcommitted
perf report: Fix memory leaks around perf_tip()
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 <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Stephane Eranian <[email protected]> Link: http://lore.kernel.org/lkml/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 0ca1f53 commit d9fc706

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

tools/perf/builtin-report.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -619,14 +619,17 @@ static int report__browse_hists(struct report *rep)
619619
int ret;
620620
struct perf_session *session = rep->session;
621621
struct evlist *evlist = session->evlist;
622-
const char *help = perf_tip(system_path(TIPDIR));
622+
char *help = NULL, *path = NULL;
623623

624-
if (help == NULL) {
624+
path = system_path(TIPDIR);
625+
if (perf_tip(&help, path) || help == NULL) {
625626
/* fallback for people who don't install perf ;-) */
626-
help = perf_tip(DOCDIR);
627-
if (help == NULL)
628-
help = "Cannot load tips.txt file, please install perf!";
627+
free(path);
628+
path = system_path(DOCDIR);
629+
if (perf_tip(&help, path) || help == NULL)
630+
help = strdup("Cannot load tips.txt file, please install perf!");
629631
}
632+
free(path);
630633

631634
switch (use_browser) {
632635
case 1:
@@ -651,7 +654,7 @@ static int report__browse_hists(struct report *rep)
651654
ret = evlist__tty_browse_hists(evlist, rep, help);
652655
break;
653656
}
654-
657+
free(help);
655658
return ret;
656659
}
657660

tools/perf/util/util.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -379,32 +379,32 @@ fetch_kernel_version(unsigned int *puint, char *str,
379379
return 0;
380380
}
381381

382-
const char *perf_tip(const char *dirpath)
382+
int perf_tip(char **strp, const char *dirpath)
383383
{
384384
struct strlist *tips;
385385
struct str_node *node;
386-
char *tip = NULL;
387386
struct strlist_config conf = {
388387
.dirname = dirpath,
389388
.file_only = true,
390389
};
390+
int ret = 0;
391391

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

397397
if (strlist__nr_entries(tips) == 0)
398398
goto out;
399399

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

404404
out:
405405
strlist__delete(tips);
406406

407-
return tip;
407+
return ret;
408408
}
409409

410410
char *perf_exe(char *buf, int len)

tools/perf/util/util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int fetch_kernel_version(unsigned int *puint,
3939
#define KVER_FMT "%d.%d.%d"
4040
#define KVER_PARAM(x) KVER_VERSION(x), KVER_PATCHLEVEL(x), KVER_SUBLEVEL(x)
4141

42-
const char *perf_tip(const char *dirpath);
42+
int perf_tip(char **strp, const char *dirpath);
4343

4444
#ifndef HAVE_SCHED_GETCPU_SUPPORT
4545
int sched_getcpu(void);

0 commit comments

Comments
 (0)