dcavalca / rpms / mdadm

Forked from rpms/mdadm 3 years ago
Clone

Blame SOURCES/0021-super1-Fix-cases-triggering-gcc-8.1-strncpy-truncate.patch

d922d5
From 4d061b02b61d5e6d4186e0118166fdd40ea8b55f Mon Sep 17 00:00:00 2001
d922d5
From: Jes Sorensen <jsorensen@fb.com>
d922d5
Date: Thu, 31 May 2018 13:38:08 -0400
d922d5
Subject: [RHEL7.5 PATCH 21/26] super1: Fix cases triggering gcc-8.1 strncpy
d922d5
 truncate warning
d922d5
d922d5
Find the string length, copy it, and zero out the rest, instead of
d922d5
relying on strncpy cleaning up for us.
d922d5
d922d5
Signed-off-by: Jes Sorensen <jsorensen@fb.com>
d922d5
---
d922d5
 super1.c | 22 ++++++++++++++++++----
d922d5
 1 file changed, 18 insertions(+), 4 deletions(-)
d922d5
d922d5
diff --git a/super1.c b/super1.c
d922d5
index 6774fbd..636a286 100644
d922d5
--- a/super1.c
d922d5
+++ b/super1.c
d922d5
@@ -1434,8 +1434,15 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
d922d5
 			strcpy(sb->set_name, homehost);
d922d5
 			strcat(sb->set_name, ":");
d922d5
 			strcat(sb->set_name, info->name);
d922d5
-		} else
d922d5
-			strncpy(sb->set_name, info->name, sizeof(sb->set_name));
d922d5
+		} else {
d922d5
+			int namelen;
d922d5
+
d922d5
+			namelen = min((int)strlen(info->name),
d922d5
+				      (int)sizeof(sb->set_name) - 1);
d922d5
+			memcpy(sb->set_name, info->name, namelen);
d922d5
+			memset(&sb->set_name[namelen], '\0',
d922d5
+			       sizeof(sb->set_name) - namelen);
d922d5
+		}
d922d5
 	} else if (strcmp(update, "devicesize") == 0 &&
d922d5
 		   __le64_to_cpu(sb->super_offset) <
d922d5
 		   __le64_to_cpu(sb->data_offset)) {
d922d5
@@ -1592,8 +1599,15 @@ static int init_super1(struct supertype *st, mdu_array_info_t *info,
d922d5
 		strcpy(sb->set_name, homehost);
d922d5
 		strcat(sb->set_name, ":");
d922d5
 		strcat(sb->set_name, name);
d922d5
-	} else
d922d5
-		strncpy(sb->set_name, name, sizeof(sb->set_name));
d922d5
+	} else {
d922d5
+		int namelen;
d922d5
+
d922d5
+		namelen = min((int)strlen(name),
d922d5
+			      (int)sizeof(sb->set_name) - 1);
d922d5
+		memcpy(sb->set_name, name, namelen);
d922d5
+		memset(&sb->set_name[namelen], '\0',
d922d5
+		       sizeof(sb->set_name) - namelen);
d922d5
+	}
d922d5
 
d922d5
 	sb->ctime = __cpu_to_le64((unsigned long long)time(0));
d922d5
 	sb->level = __cpu_to_le32(info->level);
d922d5
-- 
d922d5
2.7.4
d922d5