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

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