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

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