linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Tommi Rantala <tommi.t.rantala@nokia.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: <linux-kernel@vger.kernel.org>,
	Tommi Rantala <tommi.t.rantala@nokia.com>
Subject: [PATCH 2/6] perf buildid: do not assume that readlink() returns a null terminated string
Date: Wed, 22 Mar 2017 15:06:20 +0200	[thread overview]
Message-ID: <20170322130624.21881-3-tommi.t.rantala@nokia.com> (raw)
In-Reply-To: <20170322130624.21881-1-tommi.t.rantala@nokia.com>

Valgrind was complaining:

  $ valgrind ./perf list >/dev/null
  ==11643== Memcheck, a memory error detector
  ==11643== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
  ==11643== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
  ==11643== Command: ./perf list
  ==11643==
  ==11643== Conditional jump or move depends on uninitialised value(s)
  ==11643==    at 0x4C30620: rindex (vg_replace_strmem.c:199)
  ==11643==    by 0x49DAA9: build_id_cache__origname (build-id.c:198)
  ==11643==    by 0x49E1C7: build_id_cache__valid_id (build-id.c:222)
  ==11643==    by 0x49E1C7: build_id_cache__list_all (build-id.c:507)
  ==11643==    by 0x4B9C8F: print_sdt_events (parse-events.c:2067)
  ==11643==    by 0x4BB0B3: print_events (parse-events.c:2313)
  ==11643==    by 0x439501: cmd_list (builtin-list.c:53)
  ==11643==    by 0x497150: run_builtin (perf.c:359)
  ==11643==    by 0x428CE0: handle_internal_command (perf.c:421)
  ==11643==    by 0x428CE0: run_argv (perf.c:467)
  ==11643==    by 0x428CE0: main (perf.c:614)
  [...]

Additionally, a zero length result from readlink() is not very interesting.

Signed-off-by: Tommi Rantala <tommi.t.rantala@nokia.com>
---
 tools/perf/util/build-id.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c
index 234859f..9ad77b0 100644
--- a/tools/perf/util/build-id.c
+++ b/tools/perf/util/build-id.c
@@ -182,13 +182,17 @@ char *build_id_cache__origname(const char *sbuild_id)
 	char buf[PATH_MAX];
 	char *ret = NULL, *p;
 	size_t offs = 5;	/* == strlen("../..") */
+	ssize_t len;
 
 	linkname = build_id_cache__linkname(sbuild_id, NULL, 0);
 	if (!linkname)
 		return NULL;
 
-	if (readlink(linkname, buf, PATH_MAX) < 0)
+	len = readlink(linkname, buf, sizeof(buf)-1);
+	if (len <= 0)
 		goto out;
+	buf[len] = '\0';
+
 	/* The link should be "../..<origpath>/<sbuild_id>" */
 	p = strrchr(buf, '/');	/* Cut off the "/<sbuild_id>" */
 	if (p && (p > buf + offs)) {
-- 
2.9.3

  parent reply	other threads:[~2017-03-22 13:09 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-22 13:06 [PATCH 0/6] perf string handling fixes Tommi Rantala
2017-03-22 13:06 ` [PATCH 1/6] perf buildid: do not update SDT cache with null filename Tommi Rantala
2017-03-28  5:56   ` [tip:perf/core] perf buildid: Do " tip-bot for Tommi Rantala
2017-03-22 13:06 ` Tommi Rantala [this message]
2017-03-28  5:56   ` [tip:perf/core] perf buildid: Do not assume that readlink() returns a null terminated string tip-bot for Tommi Rantala
2017-03-22 13:06 ` [PATCH 3/6] perf tests: do " Tommi Rantala
2017-03-28  5:57   ` [tip:perf/core] perf tests: Do " tip-bot for Tommi Rantala
2017-03-22 13:06 ` [PATCH 4/6] perf utils: use sizeof(buf)-1 in readlink() call Tommi Rantala
2017-03-28  5:58   ` [tip:perf/core] perf utils: use sizeof(buf) - 1 " tip-bot for Tommi Rantala
2017-03-22 13:06 ` [PATCH 5/6] perf utils: null terminate buf in read_ftrace_printk() Tommi Rantala
2017-03-27 18:28   ` Arnaldo Carvalho de Melo
2017-03-28  5:58   ` [tip:perf/core] perf utils: Null " tip-bot for Tommi Rantala
2017-03-22 13:06 ` [PATCH 6/6] perf utils: readlink /proc/self/exe to find the perf binary Tommi Rantala
2017-03-28  5:59   ` [tip:perf/core] perf utils: Readlink " tip-bot for Tommi Rantala
2017-03-27 18:38 ` [PATCH 0/6] perf string handling fixes Arnaldo Carvalho de Melo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170322130624.21881-3-tommi.t.rantala@nokia.com \
    --to=tommi.t.rantala@nokia.com \
    --cc=acme@kernel.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).