All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] kexec: Fix snprintf related compilation warnings
@ 2020-09-23 11:12 Bhupesh Sharma
  2020-09-29 16:21 ` Simon Horman
  0 siblings, 1 reply; 2+ messages in thread
From: Bhupesh Sharma @ 2020-09-23 11:12 UTC (permalink / raw)
  To: kexec; +Cc: bhsharma, bhupesh.linux, Eric Biederman, Simon Horman

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

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] kexec: Fix snprintf related compilation warnings
  2020-09-23 11:12 [PATCH] kexec: Fix snprintf related compilation warnings Bhupesh Sharma
@ 2020-09-29 16:21 ` Simon Horman
  0 siblings, 0 replies; 2+ messages in thread
From: Simon Horman @ 2020-09-29 16:21 UTC (permalink / raw)
  To: Bhupesh Sharma; +Cc: bhupesh.linux, kexec, Eric Biederman

On Wed, Sep 23, 2020 at 04:42:37PM +0530, Bhupesh Sharma wrote:
> 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>

Thanks, applied.

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-09-29 16:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-23 11:12 [PATCH] kexec: Fix snprintf related compilation warnings Bhupesh Sharma
2020-09-29 16:21 ` Simon Horman

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.