Blame SOURCES/0069-libmultipath-cleanup-add_feature.patch

86e138
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
86e138
From: Benjamin Marzinski <bmarzins@redhat.com>
86e138
Date: Fri, 7 Oct 2022 12:35:38 -0500
86e138
Subject: [PATCH] libmultipath: cleanup add_feature
86e138
86e138
add_feature() didn't correctly handle feature strings that used
86e138
whitespace other than spaces, which the kernel allows. It also didn't
86e138
allow adding features with multiple tokens. When it looked to see if the
86e138
feature string to be added already existed, it didn't check if the match
86e138
was part of a larger token. Finally, it did unnecessary work.  By using
86e138
asprintf() to create the string, the function can be signifcantly
86e138
simplified.
86e138
86e138
Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
86e138
Reviewed-by: Martin Wilck <mwilck@suse.com>
86e138
---
86e138
 libmultipath/structs.c | 49 +++++++++++++++++++++---------------------
86e138
 1 file changed, 24 insertions(+), 25 deletions(-)
86e138
86e138
diff --git a/libmultipath/structs.c b/libmultipath/structs.c
86e138
index 83906ffe..5dfa86a8 100644
86e138
--- a/libmultipath/structs.c
86e138
+++ b/libmultipath/structs.c
86e138
@@ -608,23 +608,33 @@ int add_feature(char **f, const char *n)
86e138
 {
86e138
 	int c = 0, d, l;
86e138
 	char *e, *t;
86e138
+	const char *p;
86e138
 
86e138
 	if (!f)
86e138
 		return 1;
86e138
 
86e138
 	/* Nothing to do */
86e138
-	if (!n || *n == '0')
86e138
+	if (!n || *n == '\0')
86e138
 		return 0;
86e138
 
86e138
-	if (strchr(n, ' ') != NULL) {
86e138
-		condlog(0, "internal error: feature \"%s\" contains spaces", n);
86e138
+	l = strlen(n);
86e138
+	if (isspace(*n) || isspace(*(n + l - 1))) {
86e138
+		condlog(0, "internal error: feature \"%s\" has leading or trailing spaces", n);
86e138
 		return 1;
86e138
 	}
86e138
 
86e138
+	p = n;
86e138
+	d = 1;
86e138
+	while (*p != '\0') {
86e138
+		if (isspace(*p) && !isspace(*(p + 1)) && *(p + 1) != '\0')
86e138
+			d++;
86e138
+		p++;
86e138
+	}
86e138
+
86e138
 	/* default feature is null */
86e138
 	if(!*f)
86e138
 	{
86e138
-		l = asprintf(&t, "1 %s", n);
86e138
+		l = asprintf(&t, "%0d %s", d, n);
86e138
 		if(l == -1)
86e138
 			return 1;
86e138
 
86e138
@@ -633,35 +643,24 @@ int add_feature(char **f, const char *n)
86e138
 	}
86e138
 
86e138
 	/* Check if feature is already present */
86e138
-	if (strstr(*f, n))
86e138
-		return 0;
86e138
+	e = *f;
86e138
+	while ((e = strstr(e, n)) != NULL) {
86e138
+		if (isspace(*(e - 1)) &&
86e138
+		    (isspace(*(e + l)) || *(e + l) == '\0'))
86e138
+			return 0;
86e138
+		e += l;
86e138
+	}
86e138
 
86e138
 	/* Get feature count */
86e138
 	c = strtoul(*f, &e, 10);
86e138
-	if (*f == e || (*e != ' ' && *e != '\0')) {
86e138
+	if (*f == e || (!isspace(*e) && *e != '\0')) {
86e138
 		condlog(0, "parse error in feature string \"%s\"", *f);
86e138
 		return 1;
86e138
 	}
86e138
-
86e138
-	/* Add 1 digit and 1 space */
86e138
-	l = strlen(e) + strlen(n) + 2;
86e138
-
86e138
-	c++;
86e138
-	/* Check if we need more digits for feature count */
86e138
-	for (d = c; d >= 10; d /= 10)
86e138
-		l++;
86e138
-
86e138
-	t = MALLOC(l + 1);
86e138
-	if (!t)
86e138
+	c += d;
86e138
+	if (asprintf(&t, "%0d%s %s", c, e, n) < 0)
86e138
 		return 1;
86e138
 
86e138
-	/* e: old feature string with leading space, or "" */
86e138
-	if (*e == ' ')
86e138
-		while (*(e + 1) == ' ')
86e138
-			e++;
86e138
-
86e138
-	snprintf(t, l + 1, "%0d%s %s", c, e, n);
86e138
-
86e138
 	FREE(*f);
86e138
 	*f = t;
86e138