Kexec Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Bhupesh Sharma <bhsharma@redhat.com>
To: kexec@lists.infradead.org
Cc: bhsharma@redhat.com, bhupesh.linux@gmail.com,
	Eric Biederman <ebiederm@xmission.com>,
	Simon Horman <horms@verge.net.au>
Subject: [PATCH] kexec: Fix snprintf related compilation warnings
Date: Wed, 23 Sep 2020 16:42:37 +0530	[thread overview]
Message-ID: <20200923111237.883383-1-bhsharma@redhat.com> (raw)

This patch fixes the following snprintf related compilation warning
seen currently with gcc versions 7 and 8 when kexec is compiled with
-Wformat-truncation option:

    kexec/fs2dt.c:673:34: warning: ‘stdout-path’ directive output may be truncated writing 11 bytes into a region of size between 1 and 1024 [-Wformat-truncation=]
       snprintf(filename, MAXPATH, "%sstdout-path", pathname);
                                      ^~~~~~~~~~~
    kexec/fs2dt.c:673:3: note: ‘snprintf’ output between 12 and 1035 bytes into a destination of size 1024
       snprintf(filename, MAXPATH, "%sstdout-path", pathname);
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    kexec/fs2dt.c:676:35: warning: ‘linux,stdout-path’ directive output may be truncated writing 17 bytes into a region of size between 1 and 1024 [-Wformat-truncation=]
        snprintf(filename, MAXPATH, "%slinux,stdout-path", pathname);
                                       ^~~~~~~~~~~~~~~~~
    kexec/fs2dt.c:676:4: note: ‘snprintf’ output between 18 and 1041 bytes into a destination of size 1024
        snprintf(filename, MAXPATH, "%slinux,stdout-path", pathname);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    kexec/firmware_memmap.c:132:35: warning: ‘%s’ directive output may be truncated writing 5 bytes into a region of size between 0 and 4095 [-Wformat-truncation=]
      snprintf(filename, PATH_MAX, "%s/%s", entry, "start");
                                       ^~          ~~~~~~~
    kexec/firmware_memmap.c:132:2: note: ‘snprintf’ output between 7 and 4102 bytes into a destination of size 4096
      snprintf(filename, PATH_MAX, "%s/%s", entry, "start");
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    kexec/firmware_memmap.c:142:35: warning: ‘%s’ directive output may be truncated writing 3 bytes into a region of size between 0 and 4095 [-Wformat-truncation=]
      snprintf(filename, PATH_MAX, "%s/%s", entry, "end");
                                       ^~          ~~~~~
    kexec/firmware_memmap.c:142:2: note: ‘snprintf’ output between 5 and 4100 bytes into a destination of size 4096
      snprintf(filename, PATH_MAX, "%s/%s", entry, "end");
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    kexec/firmware_memmap.c:152:35: warning: ‘%s’ directive output may be truncated writing 4 bytes into a region of size between 0 and 4095 [-Wformat-truncation=]
      snprintf(filename, PATH_MAX, "%s/%s", entry, "type");
                                       ^~          ~~~~~~
    kexec/firmware_memmap.c:152:2: note: ‘snprintf’ output between 6 and 4101 bytes into a destination of size 4096
      snprintf(filename, PATH_MAX, "%s/%s", entry, "type");
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Since the simplest method to address the gcc warnings and possible
truncation would be to check the return value provided from snprintf
(well there are other methods like using 'asnprintf' or using
'open_memstream' function to create the FILE object, but these are more
intrusive), so this patch does the same.

Cc: Simon Horman <horms@verge.net.au>
Cc: Eric Biederman <ebiederm@xmission.com>
Cc: kexec@lists.infradead.org
Signed-off-by: Bhupesh Sharma <bhsharma@redhat.com>
---
 kexec/firmware_memmap.c | 22 +++++++++++++++++++---
 kexec/fs2dt.c           | 16 +++++++++++++---
 2 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/kexec/firmware_memmap.c b/kexec/firmware_memmap.c
index 1ee214aa9316..457c3dc9a608 100644
--- a/kexec/firmware_memmap.c
+++ b/kexec/firmware_memmap.c
@@ -125,11 +125,17 @@ static int parse_memmap_entry(const char *entry, struct memory_range *range)
 {
 	char filename[PATH_MAX];
 	char *type;
+	int ret;
 
 	/*
 	 * entry/start
 	 */
-	snprintf(filename, PATH_MAX, "%s/%s", entry, "start");
+	ret = snprintf(filename, PATH_MAX, "%s/%s", entry, "start");
+	if (ret < 0 || ret >= PATH_MAX) {
+		fprintf(stderr, "snprintf failed: %s\n", strerror(errno));
+		return -1;
+	}
+
 	filename[PATH_MAX-1] = 0;
 
 	range->start = parse_numeric_sysfs(filename);
@@ -139,7 +145,12 @@ static int parse_memmap_entry(const char *entry, struct memory_range *range)
 	/*
 	 * entry/end
 	 */
-	snprintf(filename, PATH_MAX, "%s/%s", entry, "end");
+	ret = snprintf(filename, PATH_MAX, "%s/%s", entry, "end");
+	if (ret < 0 || ret >= PATH_MAX) {
+		fprintf(stderr, "snprintf failed: %s\n", strerror(errno));
+		return -1;
+	}
+
 	filename[PATH_MAX-1] = 0;
 
 	range->end = parse_numeric_sysfs(filename);
@@ -149,7 +160,12 @@ static int parse_memmap_entry(const char *entry, struct memory_range *range)
 	/*
 	 * entry/type
 	 */
-	snprintf(filename, PATH_MAX, "%s/%s", entry, "type");
+	ret = snprintf(filename, PATH_MAX, "%s/%s", entry, "type");
+	if (ret < 0 || ret >= PATH_MAX) {
+		fprintf(stderr, "snprintf failed: %s\n", strerror(errno));
+		return -1;
+	}
+
 	filename[PATH_MAX-1] = 0;
 
 	type = parse_string_sysfs(filename);
diff --git a/kexec/fs2dt.c b/kexec/fs2dt.c
index 07a5e2f955c2..1a43058b0149 100644
--- a/kexec/fs2dt.c
+++ b/kexec/fs2dt.c
@@ -568,7 +568,7 @@ static void putnode(void)
 	struct dirent **namelist;
 	int numlist, i;
 	struct stat statbuf;
-	int plen;
+	int plen, ret;
 
 	numlist = scandir(pathname, &namelist, 0, comparefunc);
 	if (numlist < 0)
@@ -670,10 +670,20 @@ static void putnode(void)
 		 * code can print 'I'm in purgatory' message. Currently only
 		 * pseries/hvcterminal is supported.
 		 */
-		snprintf(filename, MAXPATH, "%sstdout-path", pathname);
+		ret = snprintf(filename, MAXPATH, "%sstdout-path", pathname);
+		if (ret < 0 || ret >= MAXPATH) {
+			fprintf(stderr, "snprintf failed: %s\n", strerror(errno));
+			goto no_debug;
+		}
+
 		fd = open(filename, O_RDONLY);
 		if (fd == -1) {
-			snprintf(filename, MAXPATH, "%slinux,stdout-path", pathname);
+			ret = snprintf(filename, MAXPATH, "%slinux,stdout-path", pathname);
+			if (ret < 0 || ret >= MAXPATH) {
+				fprintf(stderr, "snprintf failed: %s\n", strerror(errno));
+				goto no_debug;
+			}
+
 			fd = open(filename, O_RDONLY);
 			if (fd == -1) {
 				printf("Unable to find %s[linux,]stdout-path, printing from purgatory is disabled\n",
-- 
2.26.2


_______________________________________________
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec

             reply	other threads:[~2020-09-23 11:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-23 11:12 Bhupesh Sharma [this message]
2020-09-29 16:21 ` [PATCH] kexec: Fix snprintf related compilation warnings Simon Horman

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=20200923111237.883383-1-bhsharma@redhat.com \
    --to=bhsharma@redhat.com \
    --cc=bhupesh.linux@gmail.com \
    --cc=ebiederm@xmission.com \
    --cc=horms@verge.net.au \
    --cc=kexec@lists.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