[PATCH] kexec: Fix snprintf related compilation warnings

Bhupesh Sharma bhsharma at redhat.com
Wed Sep 23 07:12:37 EDT 2020


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 at verge.net.au>
Cc: Eric Biederman <ebiederm at xmission.com>
Cc: kexec at lists.infradead.org
Signed-off-by: Bhupesh Sharma <bhsharma at 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




More information about the kexec mailing list