linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: William Cohen <wcohen@redhat.com>
To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org,
	linux-kernel@vger.kernel.org
Cc: alexander.shishkin@linux.intel.com, jolsa@redhat.com,
	namhyung@kernel.org, William Cohen <wcohen@redhat.com>
Subject: [PATCH] Check jvmti_agent snprintf return value to avoid build failures with GCC-8.1.1
Date: Tue, 10 Jul 2018 14:27:16 -0400	[thread overview]
Message-ID: <20180710182716.21801-1-wcohen@redhat.com> (raw)

Newer versions of GCC perform static analysis to determine whether
string truncation is possible with functions such as snprintf and
provide a warning if truncation could occur.  The make for
jvmti_agent.c uses the compiler option that treats any compiler
warnings as compiler errors.  For GCC-8.1.1 in Fedora 28 this causes
the build to fail.  The return value of the snprint is now checked to
ensure snprintf produced a NULL-terminated string.  If the string for
the path is invalid, the code does attempt to use the string.

Signed-off-by: William Cohen <wcohen@redhat.com>
---
 tools/perf/jvmti/jvmti_agent.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/tools/perf/jvmti/jvmti_agent.c b/tools/perf/jvmti/jvmti_agent.c
index 0c6d1002b524..30f14eafe4b3 100644
--- a/tools/perf/jvmti/jvmti_agent.c
+++ b/tools/perf/jvmti/jvmti_agent.c
@@ -227,7 +227,7 @@ void *jvmti_open(void)
 {
 	char dump_path[PATH_MAX];
 	struct jitheader header;
-	int fd;
+	int retlen, fd;
 	FILE *fp;
 
 	init_arch_timestamp();
@@ -249,7 +249,10 @@ void *jvmti_open(void)
 	/*
 	 * jitdump file name
 	 */
-	snprintf(dump_path, PATH_MAX, "%s/jit-%i.dump", jit_path, getpid());
+	retlen = snprintf(dump_path, PATH_MAX, "%s/jit-%i.dump",
+			  jit_path, getpid());
+	if (retlen <= 0 || ((int) sizeof(dump_path)) <= retlen)
+		return NULL;
 
 	fd = open(dump_path, O_CREAT|O_TRUNC|O_RDWR, 0666);
 	if (fd == -1)
-- 
2.17.1


             reply	other threads:[~2018-07-10 18:27 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-10 18:27 William Cohen [this message]
2018-07-10 22:58 ` [PATCH] Check jvmti_agent snprintf return value to avoid build failures with GCC-8.1.1 Jiri Olsa
2018-07-11  0:10   ` William Cohen

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=20180710182716.21801-1-wcohen@redhat.com \
    --to=wcohen@redhat.com \
    --cc=acme@kernel.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=jolsa@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --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).