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

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