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