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

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