Blame SOURCES/0030-libmultipath-split-set_int-to-enable-reuse.patch

e65fa3
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
e65fa3
From: Benjamin Marzinski <bmarzins@redhat.com>
e65fa3
Date: Mon, 4 Oct 2021 15:27:36 -0500
e65fa3
Subject: [PATCH] libmultipath: split set_int to enable reuse
e65fa3
e65fa3
Split the code that does the actual value parsing out of set_int(), into
e65fa3
a helper function, do_set_int(), so that it can be used by other
e65fa3
handlers. These functions no longer set the config value at all, when
e65fa3
they have invalid input.
e65fa3
e65fa3
Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
e65fa3
---
e65fa3
 libmultipath/dict.c | 82 +++++++++++++++++++++++++--------------------
e65fa3
 1 file changed, 46 insertions(+), 36 deletions(-)
e65fa3
e65fa3
diff --git a/libmultipath/dict.c b/libmultipath/dict.c
e65fa3
index d14be340..68647061 100644
e65fa3
--- a/libmultipath/dict.c
e65fa3
+++ b/libmultipath/dict.c
e65fa3
@@ -31,17 +31,12 @@
e65fa3
 #include "strbuf.h"
e65fa3
 
e65fa3
 static int
e65fa3
-set_int(vector strvec, void *ptr, int min, int max, const char *file,
e65fa3
-	int line_nr)
e65fa3
+do_set_int(vector strvec, void *ptr, int min, int max, const char *file,
e65fa3
+	int line_nr, char *buff)
e65fa3
 {
e65fa3
 	int *int_ptr = (int *)ptr;
e65fa3
-	char *buff, *eptr;
e65fa3
+	char *eptr;
e65fa3
 	long res;
e65fa3
-	int rc;
e65fa3
-
e65fa3
-	buff = set_value(strvec);
e65fa3
-	if (!buff)
e65fa3
-		return 1;
e65fa3
 
e65fa3
 	res = strtol(buff, &eptr, 10);
e65fa3
 	if (eptr > buff)
e65fa3
@@ -50,17 +45,30 @@ set_int(vector strvec, void *ptr, int min, int max, const char *file,
e65fa3
 	if (*buff == '\0' || *eptr != '\0') {
e65fa3
 		condlog(1, "%s line %d, invalid value for %s: \"%s\"",
e65fa3
 			file, line_nr, (char*)VECTOR_SLOT(strvec, 0), buff);
e65fa3
-		rc = 1;
e65fa3
-	} else {
e65fa3
-		if (res > max || res < min) {
e65fa3
-			res = (res > max) ? max : min;
e65fa3
-			condlog(1, "%s line %d, value for %s too %s, capping at %ld",
e65fa3
+		return 1;
e65fa3
+	}
e65fa3
+	if (res > max || res < min) {
e65fa3
+		res = (res > max) ? max : min;
e65fa3
+		condlog(1, "%s line %d, value for %s too %s, capping at %ld",
e65fa3
 			file, line_nr, (char*)VECTOR_SLOT(strvec, 0),
e65fa3
-			(res == max)? "large" : "small", res);
e65fa3
-		}
e65fa3
-		rc = 0;
e65fa3
-		*int_ptr = res;
e65fa3
+		(res == max)? "large" : "small", res);
e65fa3
 	}
e65fa3
+	*int_ptr = res;
e65fa3
+	return 0;
e65fa3
+}
e65fa3
+
e65fa3
+static int
e65fa3
+set_int(vector strvec, void *ptr, int min, int max, const char *file,
e65fa3
+	int line_nr)
e65fa3
+{
e65fa3
+	char *buff;
e65fa3
+	int rc;
e65fa3
+
e65fa3
+	buff = set_value(strvec);
e65fa3
+	if (!buff)
e65fa3
+		return 1;
e65fa3
+
e65fa3
+	rc = do_set_int(strvec, ptr, min, max, file, line_nr, buff);
e65fa3
 
e65fa3
 	FREE(buff);
e65fa3
 	return rc;
e65fa3
@@ -929,6 +937,7 @@ declare_mp_attr_snprint(gid, print_gid)
e65fa3
 static int
e65fa3
 set_undef_off_zero(vector strvec, void *ptr, const char *file, int line_nr)
e65fa3
 {
e65fa3
+	int rc = 0;
e65fa3
 	char * buff;
e65fa3
 	int *int_ptr = (int *)ptr;
e65fa3
 
e65fa3
@@ -938,10 +947,10 @@ set_undef_off_zero(vector strvec, void *ptr, const char *file, int line_nr)
e65fa3
 
e65fa3
 	if (strcmp(buff, "off") == 0)
e65fa3
 		*int_ptr = UOZ_OFF;
e65fa3
-	else if (sscanf(buff, "%d", int_ptr) != 1 ||
e65fa3
-		 *int_ptr < UOZ_ZERO)
e65fa3
-		*int_ptr = UOZ_UNDEF;
e65fa3
-	else if (*int_ptr == 0)
e65fa3
+	else
e65fa3
+		rc = do_set_int(strvec, int_ptr, 0, INT_MAX, file, line_nr,
e65fa3
+				buff);
e65fa3
+	if (rc == 0 && *int_ptr == 0)
e65fa3
 		*int_ptr = UOZ_ZERO;
e65fa3
 
e65fa3
 	FREE(buff);
e65fa3
@@ -1093,14 +1102,12 @@ max_fds_handler(struct config *conf, vector strvec, const char *file,
e65fa3
 		/* Assume safe limit */
e65fa3
 		max_fds = 4096;
e65fa3
 	}
e65fa3
-	if (strlen(buff) == 3 &&
e65fa3
-	    !strcmp(buff, "max"))
e65fa3
-		conf->max_fds = max_fds;
e65fa3
-	else
e65fa3
-		conf->max_fds = atoi(buff);
e65fa3
-
e65fa3
-	if (conf->max_fds > max_fds)
e65fa3
+	if (!strcmp(buff, "max")) {
e65fa3
 		conf->max_fds = max_fds;
e65fa3
+		r = 0;
e65fa3
+	} else
e65fa3
+		r = do_set_int(strvec, &conf->max_fds, 0, max_fds, file,
e65fa3
+			       line_nr, buff);
e65fa3
 
e65fa3
 	FREE(buff);
e65fa3
 
e65fa3
@@ -1169,6 +1176,7 @@ declare_mp_snprint(rr_weight, print_rr_weight)
e65fa3
 static int
e65fa3
 set_pgfailback(vector strvec, void *ptr, const char *file, int line_nr)
e65fa3
 {
e65fa3
+	int rc = 0;
e65fa3
 	int *int_ptr = (int *)ptr;
e65fa3
 	char * buff;
e65fa3
 
e65fa3
@@ -1183,11 +1191,11 @@ set_pgfailback(vector strvec, void *ptr, const char *file, int line_nr)
e65fa3
 	else if (strlen(buff) == 10 && !strcmp(buff, "followover"))
e65fa3
 		*int_ptr = -FAILBACK_FOLLOWOVER;
e65fa3
 	else
e65fa3
-		*int_ptr = atoi(buff);
e65fa3
+		rc = do_set_int(strvec, ptr, 0, INT_MAX, file, line_nr, buff);
e65fa3
 
e65fa3
 	FREE(buff);
e65fa3
 
e65fa3
-	return 0;
e65fa3
+	return rc;
e65fa3
 }
e65fa3
 
e65fa3
 int
e65fa3
@@ -1219,6 +1227,7 @@ declare_mp_snprint(pgfailback, print_pgfailback)
e65fa3
 static int
e65fa3
 no_path_retry_helper(vector strvec, void *ptr, const char *file, int line_nr)
e65fa3
 {
e65fa3
+	int rc = 0;
e65fa3
 	int *int_ptr = (int *)ptr;
e65fa3
 	char * buff;
e65fa3
 
e65fa3
@@ -1230,11 +1239,11 @@ no_path_retry_helper(vector strvec, void *ptr, const char *file, int line_nr)
e65fa3
 		*int_ptr = NO_PATH_RETRY_FAIL;
e65fa3
 	else if (!strcmp(buff, "queue"))
e65fa3
 		*int_ptr = NO_PATH_RETRY_QUEUE;
e65fa3
-	else if ((*int_ptr = atoi(buff)) < 1)
e65fa3
-		*int_ptr = NO_PATH_RETRY_UNDEF;
e65fa3
+	else
e65fa3
+		rc = do_set_int(strvec, ptr, 1, INT_MAX, file, line_nr, buff);
e65fa3
 
e65fa3
 	FREE(buff);
e65fa3
-	return 0;
e65fa3
+	return rc;
e65fa3
 }
e65fa3
 
e65fa3
 int
e65fa3
@@ -1376,6 +1385,7 @@ snprint_mp_reservation_key (struct config *conf, struct strbuf *buff,
e65fa3
 static int
e65fa3
 set_off_int_undef(vector strvec, void *ptr, const char *file, int line_nr)
e65fa3
 {
e65fa3
+	int rc =0;
e65fa3
 	int *int_ptr = (int *)ptr;
e65fa3
 	char * buff;
e65fa3
 
e65fa3
@@ -1385,11 +1395,11 @@ set_off_int_undef(vector strvec, void *ptr, const char *file, int line_nr)
e65fa3
 
e65fa3
 	if (!strcmp(buff, "no") || !strcmp(buff, "0"))
e65fa3
 		*int_ptr = NU_NO;
e65fa3
-	else if ((*int_ptr = atoi(buff)) < 1)
e65fa3
-		*int_ptr = NU_UNDEF;
e65fa3
+	else
e65fa3
+		rc = do_set_int(strvec, ptr, 1, INT_MAX, file, line_nr, buff);
e65fa3
 
e65fa3
 	FREE(buff);
e65fa3
-	return 0;
e65fa3
+	return rc;
e65fa3
 }
e65fa3
 
e65fa3
 int