Blame SOURCES/0117-libmultipath-cleanup-remove_feature.patch

1595c1
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
1595c1
From: Benjamin Marzinski <bmarzins@redhat.com>
1595c1
Date: Fri, 7 Oct 2022 12:35:37 -0500
1595c1
Subject: [PATCH] libmultipath: cleanup remove_feature
1595c1
1595c1
remove_feature() didn't correctly handle feature strings that used
1595c1
whitespace other than spaces, which the kernel allows. It also didn't
1595c1
check if the feature string to be removed was part of a larger feature
1595c1
token. Finally, it did a lot of unnecessary work. By failing if the
1595c1
feature string to be removed contains leading or trailing whitespace,
1595c1
the function can be significanly simplified.
1595c1
1595c1
Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
1595c1
Reviewed-by: Martin Wilck <mwilck@suse.com>
1595c1
---
1595c1
 libmultipath/structs.c | 82 +++++++++++++++---------------------------
1595c1
 1 file changed, 29 insertions(+), 53 deletions(-)
1595c1
1595c1
diff --git a/libmultipath/structs.c b/libmultipath/structs.c
1595c1
index 9f86eb69..471087e2 100644
1595c1
--- a/libmultipath/structs.c
1595c1
+++ b/libmultipath/structs.c
1595c1
@@ -6,6 +6,7 @@
1595c1
 #include <unistd.h>
1595c1
 #include <libdevmapper.h>
1595c1
 #include <libudev.h>
1595c1
+#include <ctype.h>
1595c1
 
1595c1
 #include "checkers.h"
1595c1
 #include "memory.h"
1595c1
@@ -633,7 +634,7 @@ int add_feature(char **f, const char *n)
1595c1
 
1595c1
 int remove_feature(char **f, const char *o)
1595c1
 {
1595c1
-	int c = 0, d, l;
1595c1
+	int c = 0, d;
1595c1
 	char *e, *p, *n;
1595c1
 	const char *q;
1595c1
 
1595c1
@@ -644,33 +645,35 @@ int remove_feature(char **f, const char *o)
1595c1
 	if (!o || *o == '\0')
1595c1
 		return 0;
1595c1
 
1595c1
-	/* Check if not present */
1595c1
-	if (!strstr(*f, o))
1595c1
+	d = strlen(o);
1595c1
+	if (isspace(*o) || isspace(*(o + d - 1))) {
1595c1
+		condlog(0, "internal error: feature \"%s\" has leading or trailing spaces", o);
1595c1
+		return 1;
1595c1
+	}
1595c1
+
1595c1
+	/* Check if present and not part of a larger feature token*/
1595c1
+	p = *f + 1; /* the size must be at the start of the features string */
1595c1
+	while ((p = strstr(p, o)) != NULL) {
1595c1
+		if (isspace(*(p - 1)) &&
1595c1
+		    (isspace(*(p + d)) || *(p + d) == '\0'))
1595c1
+			break;
1595c1
+		p += d;
1595c1
+	}
1595c1
+	if (!p)
1595c1
 		return 0;
1595c1
 
1595c1
 	/* Get feature count */
1595c1
 	c = strtoul(*f, &e, 10);
1595c1
-	if (*f == e)
1595c1
-		/* parse error */
1595c1
+	if (*f == e || !isspace(*e)) {
1595c1
+		condlog(0, "parse error in feature string \"%s\"", *f);
1595c1
 		return 1;
1595c1
-
1595c1
-	/* Normalize features */
1595c1
-	while (*o == ' ') {
1595c1
-		o++;
1595c1
 	}
1595c1
-	/* Just spaces, return */
1595c1
-	if (*o == '\0')
1595c1
-		return 0;
1595c1
-	q = o + strlen(o);
1595c1
-	while (*q == ' ')
1595c1
-		q--;
1595c1
-	d = (int)(q - o);
1595c1
 
1595c1
 	/* Update feature count */
1595c1
 	c--;
1595c1
 	q = o;
1595c1
-	while (q[0] != '\0') {
1595c1
-		if (q[0] == ' ' && q[1] != ' ' && q[1] != '\0')
1595c1
+	while (*q != '\0') {
1595c1
+		if (isspace(*q) && !isspace(*(q + 1)) && *(q + 1) != '\0')
1595c1
 			c--;
1595c1
 		q++;
1595c1
 	}
1595c1
@@ -684,15 +687,8 @@ int remove_feature(char **f, const char *o)
1595c1
 		goto out;
1595c1
 	}
1595c1
 
1595c1
-	/* Search feature to be removed */
1595c1
-	e = strstr(*f, o);
1595c1
-	if (!e)
1595c1
-		/* Not found, return */
1595c1
-		return 0;
1595c1
-
1595c1
 	/* Update feature count space */
1595c1
-	l = strlen(*f) - d;
1595c1
-	n =  MALLOC(l + 1);
1595c1
+	n =  MALLOC(strlen(*f) - d + 1);
1595c1
 	if (!n)
1595c1
 		return 1;
1595c1
 
1595c1
@@ -702,36 +698,16 @@ int remove_feature(char **f, const char *o)
1595c1
 	 * Copy existing features up to the feature
1595c1
 	 * about to be removed
1595c1
 	 */
1595c1
-	p = strchr(*f, ' ');
1595c1
-	if (!p) {
1595c1
-		/* Internal error, feature string inconsistent */
1595c1
-		FREE(n);
1595c1
-		return 1;
1595c1
-	}
1595c1
-	while (*p == ' ')
1595c1
-		p++;
1595c1
-	p--;
1595c1
-	if (e != p) {
1595c1
-		do {
1595c1
-			e--;
1595c1
-			d++;
1595c1
-		} while (*e == ' ');
1595c1
-		e++; d--;
1595c1
-		strncat(n, p, (size_t)(e - p));
1595c1
-		p += (size_t)(e - p);
1595c1
-	}
1595c1
+	strncat(n, e, (size_t)(p - e));
1595c1
 	/* Skip feature to be removed */
1595c1
 	p += d;
1595c1
-
1595c1
 	/* Copy remaining features */
1595c1
-	if (strlen(p)) {
1595c1
-		while (*p == ' ')
1595c1
-			p++;
1595c1
-		if (strlen(p)) {
1595c1
-			p--;
1595c1
-			strcat(n, p);
1595c1
-		}
1595c1
-	}
1595c1
+	while (isspace(*p))
1595c1
+		p++;
1595c1
+	if (*p != '\0')
1595c1
+		strcat(n, p);
1595c1
+	else
1595c1
+		strchop(n);
1595c1
 
1595c1
 out:
1595c1
 	FREE(*f);