]> git.ipfire.org Git - people/arne_f/kernel.git/commitdiff
perf config: Fix caching and memory leak in perf_home_perfconfig()
authorArnaldo Carvalho de Melo <acme@kernel.org>
Fri, 20 Aug 2021 14:13:36 +0000 (11:13 -0300)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 22 Sep 2021 10:39:30 +0000 (12:39 +0200)
[ Upstream commit 261f491133aecb943ccfdc3b3794e2d775607a87 ]

Acaict, perf_home_perfconfig() is supposed to cache the result of
home_perfconfig, which returns the default location of perfconfig for
the user, given the HOME environment variable.

However, the current implementation calls home_perfconfig every time
perf_home_perfconfig() is called (so no caching is actually performed),
replacing the previous pointer, thus also causing a memory leak.

This patch adds a check of whether either config or failed is set and,
in that case, directly returns config without calling home_perfconfig at
each invocation.

Fixes: f5f03e19ce14fc31 ("perf config: Add perf_home_perfconfig function")
Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Jin Yao <yao.jin@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: Song Liu <song@kernel.org>
Link: http://lore.kernel.org/lkml/20210820130817.740536-1-rickyman7@gmail.com
[ Removed needless double check for the 'failed' variable ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
tools/perf/util/config.c

index 63d472b336de21d4d197bf9c9590b1c56bcaabab..4fb5e90d7a57ae481873b2d632dc60a992ea165d 100644 (file)
@@ -581,7 +581,10 @@ const char *perf_home_perfconfig(void)
        static const char *config;
        static bool failed;
 
-       config = failed ? NULL : home_perfconfig();
+       if (failed || config)
+               return config;
+
+       config = home_perfconfig();
        if (!config)
                failed = true;