From 9f1075f82ecd39a9960f868eef890baf2ba36d4e Mon Sep 17 00:00:00 2001
From: Changqing Li <changqing.li@windriver.com>
Date: Fri, 1 Mar 2024 11:45:10 +0800
Subject: [PATCH] RH: warn on invalid regex instead of failing

multipath.conf used to allow "*" as a match everything regular expression,
instead of requiring ".*". Instead of erroring when the old style
regular expressions are used, it should print a warning and convert
them.

Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>

Upstream-Status: Pending

[OP: Rebase to 0.9.3]
[OP: adjusted FREE() -> free(), made set_regex_value() static]
Signed-off-by: Ovidiu Panait <ovidiu.panait@windriver.com>

Rebase to 0.9.8
Signed-off-by: Changqing Li <changqing.li@windriver.com>
---
 libmultipath/dict.c | 40 ++++++++++++++++++++++++++++++++++------
 1 file changed, 34 insertions(+), 6 deletions(-)

diff --git a/libmultipath/dict.c b/libmultipath/dict.c
index a06a6138..a734ba9b 100644
--- a/libmultipath/dict.c
+++ b/libmultipath/dict.c
@@ -189,6 +189,34 @@ set_str_noslash(vector strvec, void *ptr, const char *file, int line_nr)
 	return 0;
 }
 
+static void *
+set_regex_value(vector strvec)
+{
+       char *buff = set_value(strvec);
+
+       if (buff && strcmp("*", buff) == 0) {
+               condlog(0, "Invalid regular expression \"*\" in multipath.conf. Using \".*\"");
+               free(buff);
+               return strdup(".*");
+       }
+       return buff;
+}
+
+static int
+set_regex(vector strvec, void *ptr, const char *file, int line_nr)
+{
+       char **str_ptr = (char **)ptr;
+
+       if (*str_ptr)
+               free(*str_ptr);
+       *str_ptr = set_regex_value(strvec);
+
+       if (!*str_ptr)
+               return 1;
+
+       return 0;
+}
+
 static int
 set_yes_no(vector strvec, void *ptr, const char *file, int line_nr)
 {
@@ -1854,7 +1882,7 @@ ble_ ## option ## _handler (struct config *conf, vector strvec,		\
 	if (!conf->option)						\
 		return 1;						\
 									\
-	buff = set_value(strvec);					\
+	buff = set_regex_value(strvec);					\
 	if (!buff)							\
 		return 1;						\
 									\
@@ -1874,7 +1902,7 @@ ble_ ## option ## _ ## name ## _handler (struct config *conf, vector strvec, \
 	if (!conf->option)						\
 		return 1;						\
 									\
-	buff = set_value(strvec);					\
+	buff = set_regex_value(strvec);					\
 	if (!buff)							\
 		return 1;						\
 									\
@@ -1980,16 +2008,16 @@ device_handler(struct config *conf, vector strvec, const char *file,
 	return 0;
 }
 
-declare_hw_handler(vendor, set_str)
+declare_hw_handler(vendor, set_regex)
 declare_hw_snprint(vendor, print_str)
 
-declare_hw_handler(product, set_str)
+declare_hw_handler(product, set_regex)
 declare_hw_snprint(product, print_str)
 
-declare_hw_handler(revision, set_str)
+declare_hw_handler(revision, set_regex)
 declare_hw_snprint(revision, print_str)
 
-declare_hw_handler(bl_product, set_str)
+declare_hw_handler(bl_product, set_regex)
 declare_hw_snprint(bl_product, print_str)
 
 declare_hw_arg_str_handler(hwhandler, 0)
-- 
2.25.1

