Blame SOURCES/0011-mdadm-add-map_num_s.patch

c0f891
From 5f21d67472ad08c1e96b4385254adba79aa1c467 Mon Sep 17 00:00:00 2001
c0f891
From: Mariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>
c0f891
Date: Thu, 20 Jan 2022 13:18:33 +0100
c0f891
Subject: [PATCH 11/12] mdadm: add map_num_s()
c0f891
c0f891
map_num() returns NULL if key is not defined. This patch adds
c0f891
alternative, non NULL version for cases where NULL is not expected.
c0f891
c0f891
There are many printf() calls where map_num() is called on variable
c0f891
without NULL verification. It works, even if NULL is passed because
c0f891
gcc is able to ignore NULL argument quietly but the behavior is
c0f891
undefined. For safety reasons such usages will use map_num_s() now.
c0f891
It is a potential point of regression.
c0f891
c0f891
Signed-off-by: Mariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>
c0f891
Signed-off-by: Jes Sorensen <jsorensen@fb.com>
c0f891
---
c0f891
 Assemble.c    |  6 ++----
c0f891
 Create.c      |  2 +-
c0f891
 Detail.c      |  4 ++--
c0f891
 Grow.c        | 16 ++++++++--------
c0f891
 Query.c       |  4 ++--
c0f891
 maps.c        | 24 ++++++++++++++++++++++++
c0f891
 mdadm.c       | 20 ++++++++++----------
c0f891
 mdadm.h       |  2 +-
c0f891
 super-ddf.c   |  6 +++---
c0f891
 super-intel.c |  2 +-
c0f891
 super0.c      |  2 +-
c0f891
 super1.c      |  2 +-
c0f891
 sysfs.c       |  9 +++++----
c0f891
 13 files changed, 61 insertions(+), 38 deletions(-)
c0f891
c0f891
diff --git a/Assemble.c b/Assemble.c
c0f891
index 704b8293..9eac9ce0 100644
c0f891
--- a/Assemble.c
c0f891
+++ b/Assemble.c
c0f891
@@ -63,7 +63,7 @@ static void set_array_assembly_status(struct context *c,
c0f891
 				   struct assembly_array_info *arr)
c0f891
 {
c0f891
 	int raid_disks = arr->preexist_cnt + arr->new_cnt;
c0f891
-	char *status_msg = map_num(assemble_statuses, status);
c0f891
+	char *status_msg = map_num_s(assemble_statuses, status);
c0f891
 
c0f891
 	if (c->export && result)
c0f891
 		*result |= status;
c0f891
@@ -77,9 +77,7 @@ static void set_array_assembly_status(struct context *c,
c0f891
 		fprintf(stderr, " (%d new)", arr->new_cnt);
c0f891
 	if (arr->exp_cnt)
c0f891
 		fprintf(stderr, " ( + %d for expansion)", arr->exp_cnt);
c0f891
-	if (status_msg)
c0f891
-		fprintf(stderr, " %s", status_msg);
c0f891
-	fprintf(stderr, ".\n");
c0f891
+	fprintf(stderr, " %s.\n", status_msg);
c0f891
 }
c0f891
 
c0f891
 static int name_matches(char *found, char *required, char *homehost, int require_homehost)
c0f891
diff --git a/Create.c b/Create.c
c0f891
index 9ea19de0..c84c1ac8 100644
c0f891
--- a/Create.c
c0f891
+++ b/Create.c
c0f891
@@ -83,7 +83,7 @@ int default_layout(struct supertype *st, int level, int verbose)
c0f891
 
c0f891
 	if (layout_map) {
c0f891
 		layout = map_name(layout_map, "default");
c0f891
-		layout_name = map_num(layout_map, layout);
c0f891
+		layout_name = map_num_s(layout_map, layout);
c0f891
 	}
c0f891
 	if (layout_name && verbose > 0)
c0f891
 		pr_err("layout defaults to %s\n", layout_name);
c0f891
diff --git a/Detail.c b/Detail.c
c0f891
index 95d4cc70..ce7a8445 100644
c0f891
--- a/Detail.c
c0f891
+++ b/Detail.c
c0f891
@@ -495,8 +495,8 @@ int Detail(char *dev, struct context *c)
c0f891
 			if (array.state & (1 << MD_SB_CLEAN)) {
c0f891
 				if ((array.level == 0) ||
c0f891
 				    (array.level == LEVEL_LINEAR))
c0f891
-					arrayst = map_num(sysfs_array_states,
c0f891
-							  sra->array_state);
c0f891
+					arrayst = map_num_s(sysfs_array_states,
c0f891
+							       sra->array_state);
c0f891
 				else
c0f891
 					arrayst = "clean";
c0f891
 			} else {
c0f891
diff --git a/Grow.c b/Grow.c
c0f891
index 18c5719b..8a242b0f 100644
c0f891
--- a/Grow.c
c0f891
+++ b/Grow.c
c0f891
@@ -547,7 +547,7 @@ int Grow_consistency_policy(char *devname, int fd, struct context *c, struct sha
c0f891
 	if (s->consistency_policy != CONSISTENCY_POLICY_RESYNC &&
c0f891
 	    s->consistency_policy != CONSISTENCY_POLICY_PPL) {
c0f891
 		pr_err("Operation not supported for consistency policy %s\n",
c0f891
-		       map_num(consistency_policies, s->consistency_policy));
c0f891
+		       map_num_s(consistency_policies, s->consistency_policy));
c0f891
 		return 1;
c0f891
 	}
c0f891
 
c0f891
@@ -578,14 +578,14 @@ int Grow_consistency_policy(char *devname, int fd, struct context *c, struct sha
c0f891
 
c0f891
 	if (sra->consistency_policy == (unsigned)s->consistency_policy) {
c0f891
 		pr_err("Consistency policy is already %s\n",
c0f891
-		       map_num(consistency_policies, s->consistency_policy));
c0f891
+		       map_num_s(consistency_policies, s->consistency_policy));
c0f891
 		ret = 1;
c0f891
 		goto free_info;
c0f891
 	} else if (sra->consistency_policy != CONSISTENCY_POLICY_RESYNC &&
c0f891
 		   sra->consistency_policy != CONSISTENCY_POLICY_PPL) {
c0f891
 		pr_err("Current consistency policy is %s, cannot change to %s\n",
c0f891
-		       map_num(consistency_policies, sra->consistency_policy),
c0f891
-		       map_num(consistency_policies, s->consistency_policy));
c0f891
+		       map_num_s(consistency_policies, sra->consistency_policy),
c0f891
+		       map_num_s(consistency_policies, s->consistency_policy));
c0f891
 		ret = 1;
c0f891
 		goto free_info;
c0f891
 	}
c0f891
@@ -704,8 +704,8 @@ int Grow_consistency_policy(char *devname, int fd, struct context *c, struct sha
c0f891
 	}
c0f891
 
c0f891
 	ret = sysfs_set_str(sra, NULL, "consistency_policy",
c0f891
-			    map_num(consistency_policies,
c0f891
-				    s->consistency_policy));
c0f891
+			    map_num_s(consistency_policies,
c0f891
+					 s->consistency_policy));
c0f891
 	if (ret)
c0f891
 		pr_err("Failed to change array consistency policy\n");
c0f891
 
c0f891
@@ -2241,7 +2241,7 @@ size_change_error:
c0f891
 		info.new_layout = UnSet;
c0f891
 		if (info.array.level == 6 && info.new_level == UnSet) {
c0f891
 			char l[40], *h;
c0f891
-			strcpy(l, map_num(r6layout, info.array.layout));
c0f891
+			strcpy(l, map_num_s(r6layout, info.array.layout));
c0f891
 			h = strrchr(l, '-');
c0f891
 			if (h && strcmp(h, "-6") == 0) {
c0f891
 				*h = 0;
c0f891
@@ -2266,7 +2266,7 @@ size_change_error:
c0f891
 			info.new_layout = info.array.layout;
c0f891
 		else if (info.array.level == 5 && info.new_level == 6) {
c0f891
 			char l[40];
c0f891
-			strcpy(l, map_num(r5layout, info.array.layout));
c0f891
+			strcpy(l, map_num_s(r5layout, info.array.layout));
c0f891
 			strcat(l, "-6");
c0f891
 			info.new_layout = map_name(r6layout, l);
c0f891
 		} else {
c0f891
diff --git a/Query.c b/Query.c
c0f891
index 23fbf8aa..adcd231e 100644
c0f891
--- a/Query.c
c0f891
+++ b/Query.c
c0f891
@@ -93,7 +93,7 @@ int Query(char *dev)
c0f891
 	else {
c0f891
 		printf("%s: %s %s %d devices, %d spare%s. Use mdadm --detail for more detail.\n",
c0f891
 		       dev, human_size_brief(larray_size,IEC),
c0f891
-		       map_num(pers, level), raid_disks,
c0f891
+		       map_num_s(pers, level), raid_disks,
c0f891
 		       spare_disks, spare_disks == 1 ? "" : "s");
c0f891
 	}
c0f891
 	st = guess_super(fd);
c0f891
@@ -131,7 +131,7 @@ int Query(char *dev)
c0f891
 		       dev,
c0f891
 		       info.disk.number, info.array.raid_disks,
c0f891
 		       activity,
c0f891
-		       map_num(pers, info.array.level),
c0f891
+		       map_num_s(pers, info.array.level),
c0f891
 		       mddev);
c0f891
 		if (st->ss == &super0)
c0f891
 			put_md_name(mddev);
c0f891
diff --git a/maps.c b/maps.c
c0f891
index a4fd2797..20fcf719 100644
c0f891
--- a/maps.c
c0f891
+++ b/maps.c
c0f891
@@ -166,6 +166,30 @@ mapping_t sysfs_array_states[] = {
c0f891
 	{ NULL, ARRAY_UNKNOWN_STATE }
c0f891
 };
c0f891
 
c0f891
+/**
c0f891
+ * map_num_s() - Safer alternative of map_num() function.
c0f891
+ * @map: map to search.
c0f891
+ * @num: key to match.
c0f891
+ *
c0f891
+ * Shall be used only if key existence is quaranted.
c0f891
+ *
c0f891
+ * Return: Pointer to name of the element.
c0f891
+ */
c0f891
+char *map_num_s(mapping_t *map, int num)
c0f891
+{
c0f891
+	char *ret = map_num(map, num);
c0f891
+
c0f891
+	assert(ret);
c0f891
+	return ret;
c0f891
+}
c0f891
+
c0f891
+/**
c0f891
+ * map_num() - get element name by key.
c0f891
+ * @map: map to search.
c0f891
+ * @num: key to match.
c0f891
+ *
c0f891
+ * Return: Pointer to name of the element or NULL.
c0f891
+ */
c0f891
 char *map_num(mapping_t *map, int num)
c0f891
 {
c0f891
 	while (map->name) {
c0f891
diff --git a/mdadm.c b/mdadm.c
c0f891
index 26299b2e..be40686c 100644
c0f891
--- a/mdadm.c
c0f891
+++ b/mdadm.c
c0f891
@@ -280,8 +280,8 @@ int main(int argc, char *argv[])
c0f891
 			else
c0f891
 				fprintf(stderr, "-%c", opt);
c0f891
 			fprintf(stderr, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
c0f891
-				map_num(modes, newmode),
c0f891
-				map_num(modes, mode));
c0f891
+				map_num_s(modes, newmode),
c0f891
+				map_num_s(modes, mode));
c0f891
 			exit(2);
c0f891
 		} else if (!mode && newmode) {
c0f891
 			mode = newmode;
c0f891
@@ -544,7 +544,7 @@ int main(int argc, char *argv[])
c0f891
 			switch(s.level) {
c0f891
 			default:
c0f891
 				pr_err("layout not meaningful for %s arrays.\n",
c0f891
-					map_num(pers, s.level));
c0f891
+					map_num_s(pers, s.level));
c0f891
 				exit(2);
c0f891
 			case UnSet:
c0f891
 				pr_err("raid level must be given before layout.\n");
c0f891
@@ -1248,10 +1248,10 @@ int main(int argc, char *argv[])
c0f891
 		if (option_index > 0)
c0f891
 			pr_err(":option --%s not valid in %s mode\n",
c0f891
 				long_options[option_index].name,
c0f891
-				map_num(modes, mode));
c0f891
+				map_num_s(modes, mode));
c0f891
 		else
c0f891
 			pr_err("option -%c not valid in %s mode\n",
c0f891
-				opt, map_num(modes, mode));
c0f891
+				opt, map_num_s(modes, mode));
c0f891
 		exit(2);
c0f891
 
c0f891
 	}
c0f891
@@ -1276,7 +1276,7 @@ int main(int argc, char *argv[])
c0f891
 		if (s.consistency_policy != CONSISTENCY_POLICY_UNKNOWN &&
c0f891
 		    s.consistency_policy != CONSISTENCY_POLICY_JOURNAL) {
c0f891
 			pr_err("--write-journal is not supported with consistency policy: %s\n",
c0f891
-			       map_num(consistency_policies, s.consistency_policy));
c0f891
+			       map_num_s(consistency_policies, s.consistency_policy));
c0f891
 			exit(2);
c0f891
 		}
c0f891
 	}
c0f891
@@ -1285,12 +1285,12 @@ int main(int argc, char *argv[])
c0f891
 	    s.consistency_policy != CONSISTENCY_POLICY_UNKNOWN) {
c0f891
 		if (s.level <= 0) {
c0f891
 			pr_err("--consistency-policy not meaningful with level %s.\n",
c0f891
-			       map_num(pers, s.level));
c0f891
+			       map_num_s(pers, s.level));
c0f891
 			exit(2);
c0f891
 		} else if (s.consistency_policy == CONSISTENCY_POLICY_JOURNAL &&
c0f891
 			   !s.journaldisks) {
c0f891
 			pr_err("--write-journal is required for consistency policy: %s\n",
c0f891
-			       map_num(consistency_policies, s.consistency_policy));
c0f891
+			       map_num_s(consistency_policies, s.consistency_policy));
c0f891
 			exit(2);
c0f891
 		} else if (s.consistency_policy == CONSISTENCY_POLICY_PPL &&
c0f891
 			   s.level != 5) {
c0f891
@@ -1300,14 +1300,14 @@ int main(int argc, char *argv[])
c0f891
 			   (!s.bitmap_file ||
c0f891
 			    strcmp(s.bitmap_file, "none") == 0)) {
c0f891
 			pr_err("--bitmap is required for consistency policy: %s\n",
c0f891
-			       map_num(consistency_policies, s.consistency_policy));
c0f891
+			       map_num_s(consistency_policies, s.consistency_policy));
c0f891
 			exit(2);
c0f891
 		} else if (s.bitmap_file &&
c0f891
 			   strcmp(s.bitmap_file, "none") != 0 &&
c0f891
 			   s.consistency_policy != CONSISTENCY_POLICY_BITMAP &&
c0f891
 			   s.consistency_policy != CONSISTENCY_POLICY_JOURNAL) {
c0f891
 			pr_err("--bitmap is not compatible with consistency policy: %s\n",
c0f891
-			       map_num(consistency_policies, s.consistency_policy));
c0f891
+			       map_num_s(consistency_policies, s.consistency_policy));
c0f891
 			exit(2);
c0f891
 		}
c0f891
 	}
c0f891
diff --git a/mdadm.h b/mdadm.h
c0f891
index cd72e711..09915a00 100644
c0f891
--- a/mdadm.h
c0f891
+++ b/mdadm.h
c0f891
@@ -770,7 +770,7 @@ extern int restore_stripes(int *dest, unsigned long long *offsets,
c0f891
 #endif
c0f891
 
c0f891
 #define SYSLOG_FACILITY LOG_DAEMON
c0f891
-
c0f891
+extern char *map_num_s(mapping_t *map, int num);
c0f891
 extern char *map_num(mapping_t *map, int num);
c0f891
 extern int map_name(mapping_t *map, char *name);
c0f891
 extern mapping_t r0layout[], r5layout[], r6layout[],
c0f891
diff --git a/super-ddf.c b/super-ddf.c
c0f891
index 3f304cdc..8cda23a7 100644
c0f891
--- a/super-ddf.c
c0f891
+++ b/super-ddf.c
c0f891
@@ -1477,13 +1477,13 @@ static void examine_vds(struct ddf_super *sb)
c0f891
 		printf("\n");
c0f891
 		printf("         unit[%d] : %d\n", i, be16_to_cpu(ve->unit));
c0f891
 		printf("        state[%d] : %s, %s%s\n", i,
c0f891
-		       map_num(ddf_state, ve->state & 7),
c0f891
+		       map_num_s(ddf_state, ve->state & 7),
c0f891
 		       (ve->state & DDF_state_morphing) ? "Morphing, ": "",
c0f891
 		       (ve->state & DDF_state_inconsistent)? "Not Consistent" : "Consistent");
c0f891
 		printf("   init state[%d] : %s\n", i,
c0f891
-		       map_num(ddf_init_state, ve->init_state&DDF_initstate_mask));
c0f891
+		       map_num_s(ddf_init_state, ve->init_state & DDF_initstate_mask));
c0f891
 		printf("       access[%d] : %s\n", i,
c0f891
-		       map_num(ddf_access, (ve->init_state & DDF_access_mask) >> 6));
c0f891
+		       map_num_s(ddf_access, (ve->init_state & DDF_access_mask) >> 6));
c0f891
 		printf("         Name[%d] : %.16s\n", i, ve->name);
c0f891
 		examine_vd(i, sb, ve->guid);
c0f891
 	}
c0f891
diff --git a/super-intel.c b/super-intel.c
c0f891
index 6ff336ee..ba3bd41f 100644
c0f891
--- a/super-intel.c
c0f891
+++ b/super-intel.c
c0f891
@@ -5625,7 +5625,7 @@ static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
c0f891
 		free(dev);
c0f891
 		free(dv);
c0f891
 		pr_err("imsm does not support consistency policy %s\n",
c0f891
-		       map_num(consistency_policies, s->consistency_policy));
c0f891
+		       map_num_s(consistency_policies, s->consistency_policy));
c0f891
 		return 0;
c0f891
 	}
c0f891
 
c0f891
diff --git a/super0.c b/super0.c
c0f891
index b79b97a9..61c9ec1d 100644
c0f891
--- a/super0.c
c0f891
+++ b/super0.c
c0f891
@@ -288,7 +288,7 @@ static void export_examine_super0(struct supertype *st)
c0f891
 {
c0f891
 	mdp_super_t *sb = st->sb;
c0f891
 
c0f891
-	printf("MD_LEVEL=%s\n", map_num(pers, sb->level));
c0f891
+	printf("MD_LEVEL=%s\n", map_num_s(pers, sb->level));
c0f891
 	printf("MD_DEVICES=%d\n", sb->raid_disks);
c0f891
 	if (sb->minor_version >= 90)
c0f891
 		printf("MD_UUID=%08x:%08x:%08x:%08x\n",
c0f891
diff --git a/super1.c b/super1.c
c0f891
index a12a5bc8..e3e2f954 100644
c0f891
--- a/super1.c
c0f891
+++ b/super1.c
c0f891
@@ -671,7 +671,7 @@ static void export_examine_super1(struct supertype *st)
c0f891
 	int len = 32;
c0f891
 	int layout;
c0f891
 
c0f891
-	printf("MD_LEVEL=%s\n", map_num(pers, __le32_to_cpu(sb->level)));
c0f891
+	printf("MD_LEVEL=%s\n", map_num_s(pers, __le32_to_cpu(sb->level)));
c0f891
 	printf("MD_DEVICES=%d\n", __le32_to_cpu(sb->raid_disks));
c0f891
 	for (i = 0; i < 32; i++)
c0f891
 		if (sb->set_name[i] == '\n' || sb->set_name[i] == '\0') {
c0f891
diff --git a/sysfs.c b/sysfs.c
c0f891
index 2995713d..0d98a65f 100644
c0f891
--- a/sysfs.c
c0f891
+++ b/sysfs.c
c0f891
@@ -689,7 +689,7 @@ int sysfs_set_array(struct mdinfo *info, int vers)
c0f891
 	if (info->array.level < 0)
c0f891
 		return 0; /* FIXME */
c0f891
 	rv |= sysfs_set_str(info, NULL, "level",
c0f891
-			    map_num(pers, info->array.level));
c0f891
+			    map_num_s(pers, info->array.level));
c0f891
 	if (info->reshape_active && info->delta_disks != UnSet)
c0f891
 		raid_disks -= info->delta_disks;
c0f891
 	rv |= sysfs_set_num(info, NULL, "raid_disks", raid_disks);
c0f891
@@ -724,9 +724,10 @@ int sysfs_set_array(struct mdinfo *info, int vers)
c0f891
 	}
c0f891
 
c0f891
 	if (info->consistency_policy == CONSISTENCY_POLICY_PPL) {
c0f891
-		if (sysfs_set_str(info, NULL, "consistency_policy",
c0f891
-				  map_num(consistency_policies,
c0f891
-					  info->consistency_policy))) {
c0f891
+		char *policy = map_num_s(consistency_policies,
c0f891
+					    info->consistency_policy);
c0f891
+
c0f891
+		if (sysfs_set_str(info, NULL, "consistency_policy", policy)) {
c0f891
 			pr_err("This kernel does not support PPL. Falling back to consistency-policy=resync.\n");
c0f891
 			info->consistency_policy = CONSISTENCY_POLICY_RESYNC;
c0f891
 		}
c0f891
-- 
c0f891
2.31.1
c0f891