Skip to contentSkip to conversation

Fixes for config file parsing

PATCH Superseded Martin Wilck 1 participant 9 messages Mar 6, 2018 – Mar 7, 2018 Device mapper

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Fixes for config file parsing

This series was motivated by the real-world problem that a user couldn't figure out how to write a blacklist entry for a device called '1.8" SSD'. Fixing this for good turned out to be a little tricky, therefore I also added a test suite.

Martin Wilck (6):
tests/uevent: fix -Wcast-qual errors
tests: add unit tests for config file parser
libmultipath: config parser: don't strip whitepace between quotes
libmultipath: config parser: Allow '"' in strings
libmultipath: config parser: fix corner case for double quotes multipath.conf(5): improve syntax documentation

 libmultipath/parser.c      |  65 ++++--
 multipath/multipath.conf.5 |  17 ++
 tests/Makefile             |   2 +-
 tests/globals.c            |   1 +
 tests/parser.c             | 497 +++++++++++++++++++++++++++++++++++++++++++++
 tests/uevent.c             |   8 +-
 6 files changed, 566 insertions(+), 24 deletions(-)
 create mode 100644 tests/parser.c
Signature · 1 line
2.16.1
#1 lore raw

PATCH 1/6 tests/uevent: fix -Wcast-qual errors

I overlooked these in my previous series of fixes.

Fixes "libmultipath: fix compiler warnings for -Wcast-qual"
---
 tests/uevent.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
tests/uevent.c +4 -4
index b7d6458710f4..0836b08d5b50 100644
@@ -172,7 +172,7 @@ static void test_dm_name_good(void **state)
 	const char *name = uevent_get_dm_name(uev);
 
 	assert_string_equal(name, DM_NAME);
-	free((void*)name);
+	FREE_CONST(name);
 }
 
 static void test_dm_name_bad_0(void **state)
@@ -183,7 +183,7 @@ static void test_dm_name_bad_0(void **state)
 	uev->envp[3] = "DM_NAME" DM_NAME;
 	name = uevent_get_dm_name(uev);
 	assert_ptr_equal(name, NULL);
-	free((void*)name);
+	FREE_CONST(name);
 }
 
 static void test_dm_name_bad_1(void **state)
@@ -194,7 +194,7 @@ static void test_dm_name_bad_1(void **state)
 	uev->envp[3] = "DM_NAMES=" DM_NAME;
 	name = uevent_get_dm_name(uev);
 	assert_ptr_equal(name, NULL);
-	free((void*)name);
+	FREE_CONST(name);
 }
 
 static void test_dm_name_good_1(void **state)
@@ -206,7 +206,7 @@ static void test_dm_name_good_1(void **state)
 	uev->envp[2] = "DM_NAME=" DM_NAME;
 	name = uevent_get_dm_name(uev);
 	assert_string_equal(name, DM_NAME);
-	free((void*)name);
+	FREE_CONST(name);
 }
 
 static void test_dm_uuid_false_0(void **state)
Signature · 1 line
2.16.1
#2 lore raw

PATCH 2/6 tests: add unit tests for config file parser

In reply to Martin Wilck

Add test cases for parsing the config file.

Some of these tests currently fail unless the CPP macros at the top are set to 1. The patches that follow fix them.

---
 tests/Makefile  |   2 +-
 tests/globals.c |   1 +
 tests/parser.c  | 497 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 499 insertions(+), 1 deletion(-)
 create mode 100644 tests/parser.c
tests/Makefile +1 -1
index f6b55836a434..231f73bc7332 100644
@@ -3,7 +3,7 @@ include ../Makefile.inc
 CFLAGS += $(BIN_CFLAGS) -I$(multipathdir) -I$(mpathcmddir)
 LIBDEPS += -L$(multipathdir) -lmultipath -lcmocka
 
-TESTS := uevent
+TESTS := uevent parser
 
 .SILENT: $(TESTS:%=%.o)
 .PRECIOUS: $(TESTS:%=%-test)
tests/globals.c +1 -0
index 96a56515fd09..80f57bd3639a 100644
@@ -6,6 +6,7 @@ struct udev *udev;
 int logsink = 0;
 struct config conf = {
 	.uid_attrs = "sd:ID_BOGUS",
+	.verbosity = 4,
 };
 
 struct config *get_multipath_config(void)
tests/parser.c new file +497 -0

497 lines not on this page · raw message

Signature · 1 line
2.16.1
#3 lore raw

PATCH 3/6 libmultipath: config parser: don't strip whitepace between quotes

In reply to Martin Wilck

Between double quotes, the parser currently strips leading (but not trailing) whitespace. That's inconsistent and unexpected. Fix it.

---
 libmultipath/parser.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
libmultipath/parser.c +2 -1
index 5caa2019a1a4..3d9656f47945 100644
@@ -262,7 +262,8 @@ alloc_strvec(char *string)
 		}
 		vector_set_slot(strvec, token);
 
-		while ((isspace((int) *cp) || !isascii((int) *cp))
+		while ((!in_string &&
+			(isspace((int) *cp) || !isascii((int) *cp)))
 		       && *cp != '\0')
 			cp++;
 		if (*cp == '\0' || *cp == '!' || *cp == '#')
Signature · 1 line
2.16.1
#4 lore raw

PATCH 4/6 libmultipath: config parser: Allow '"' in strings

In reply to Martin Wilck

We have seen model strings lile '2.5" SSD' which can't be parsed by the current config parser. This patch fixes this by allowing '""' to represent a double quote character inside a a string. The above model string could now be entered in the config file like this:

blacklist {
	  vendor SomeCorp
	  product "2.5"" SSD"
}
---
 libmultipath/parser.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)
libmultipath/parser.c +25 -1
index 3d9656f47945..592269a9b5b1 100644
@@ -223,7 +223,7 @@ alloc_strvec(char *string)
 			goto out;
 
 		start = cp;
-		if (*cp == '"') {
+		if (!in_string && *cp == '"') {
 			cp++;
 			token = MALLOC(2);
 
@@ -246,11 +246,25 @@ alloc_strvec(char *string)
 			*(token + 1) = '\0';
 			cp++;
 		} else {
+			int two_quotes = 0;
+
+		move_on:
 			while ((in_string ||
 				(!isspace((int) *cp) && isascii((int) *cp) &&
 				 *cp != '!' && *cp != '#' && *cp != '{' &&
 				 *cp != '}')) && *cp != '\0' && *cp != '"')
 				cp++;
+
+			/* Two consecutive double quotes - don't end string */
+			if (in_string && *cp == '"') {
+				if (*(cp + 1) == '"') {
+					two_quotes = 1;
+					cp += 2;
+					goto move_on;
+				} else
+					in_string = 0;
+			}
+
 			strlen = cp - start;
 			token = MALLOC(strlen + 1);
 
@@ -259,6 +273,16 @@ alloc_strvec(char *string)
 
 			memcpy(token, start, strlen);
 			*(token + strlen) = '\0';
+
+			/* Replace "" by " */
+			if (two_quotes) {
+				char *qq = strstr(token, "\"\"");
+				while (qq != NULL) {
+					memmove(qq + 1, qq + 2,
+						strlen + 1 - (qq + 2 - token));
+					qq = strstr(qq + 1, "\"\"");
+				}
+			}
 		}
 		vector_set_slot(strvec, token);
 
Signature · 1 line
2.16.1
#5 lore raw

PATCH 5/6 libmultipath: config parser: fix corner case for double quotes

In reply to Martin Wilck

A corner case of the previous patch are strings starting with a double quote, such as '"prepended to itself is false" prepended to itself is false' or '"" is the empty string', and in particular, the string '"' ("\"" in C notation), which is indistinguishable from the "QUOTE" token in the parsed strvec.

This patch fixes that by introducing a special token that can't occur as part of a normal string to indicate the beginning and end of a quoted string.

'"' is admittedly not a very likely keyword value for multipath.conf, but
a) this is a matter of correctness, b) we didn't think of '2.5"' before, either, and
c) the (*str != '"') expressions would need to be patched anyway to fix the 'string starting with "' case.

---
 libmultipath/parser.c | 36 +++++++++++++++++++-----------------
 1 file changed, 19 insertions(+), 17 deletions(-)
libmultipath/parser.c +19 -17
index 592269a9b5b1..da8de305f680 100644
@@ -186,6 +186,12 @@ snprint_keyword(char *buff, int len, char *fmt, struct keyword *kw,
 	return fwd;
 }
 
+static const char quote_marker[] = { '\0', '"', '\0' };
+static bool is_quote(const char* token)
+{
+	return !memcmp(token, quote_marker, sizeof(quote_marker));
+}
+
 vector
 alloc_strvec(char *string)
 {
@@ -225,17 +231,13 @@ alloc_strvec(char *string)
 		start = cp;
 		if (!in_string && *cp == '"') {
 			cp++;
-			token = MALLOC(2);
+			token = MALLOC(sizeof(quote_marker));
 
 			if (!token)
 				goto out;
 
-			*(token) = '"';
-			*(token + 1) = '\0';
-			if (in_string)
-				in_string = 0;
-			else
-				in_string = 1;
+			memcpy(token, quote_marker, sizeof(quote_marker));
+			in_string = 1;
 		} else if (!in_string && (*cp == '{' || *cp == '}')) {
 			token = MALLOC(2);
 
@@ -324,13 +326,13 @@ set_value(vector strvec)
 			(char *)VECTOR_SLOT(strvec, 0));
 		return NULL;
 	}
-	size = strlen(str);
-	if (size == 0) {
-		condlog(0, "option '%s' has empty value",
-			(char *)VECTOR_SLOT(strvec, 0));
-		return NULL;
-	}
-	if (*str != '"') {
+	if (!is_quote(str)) {
+		size = strlen(str);
+		if (size == 0) {
+			condlog(0, "option '%s' has empty value",
+				(char *)VECTOR_SLOT(strvec, 0));
+			return NULL;
+		}
 		alloc = MALLOC(sizeof (char) * (size + 1));
 		if (alloc)
 			memcpy(alloc, str, size);
@@ -354,7 +356,7 @@ set_value(vector strvec)
 				(char *)VECTOR_SLOT(strvec, 0));
 			return NULL;
 		}
-		if (*str == '"')
+		if (is_quote(str))
 			break;
 		tmp = alloc;
 		/* The first +1 is for the NULL byte. The rest are for the
@@ -460,7 +462,7 @@ validate_config_strvec(vector strvec, char *file)
 			(char *)VECTOR_SLOT(strvec, 0), line_nr, file);
 		return -1;
 	}
-	if (*str != '"') {
+	if (!is_quote(str)) {
 		if (VECTOR_SIZE(strvec) > 2)
 			condlog(0, "ignoring extra data starting with '%s' on line %d of %s", (char *)VECTOR_SLOT(strvec, 2), line_nr, file);
 		return 0;
@@ -472,7 +474,7 @@ validate_config_strvec(vector strvec, char *file)
 				line_nr, file);
 			return -1;
 		}
-		if (*str == '"') {
+		if (is_quote(str)) {
 			if (VECTOR_SIZE(strvec) > i + 1)
 				condlog(0, "ignoring extra data starting with '%s' on line %d of %s", (char *)VECTOR_SLOT(strvec, (i + 1)), line_nr, file);
 			return 0;
Signature · 1 line
2.16.1
#6 lore raw

PATCH 6/6 multipath.conf(5): improve syntax documentation

In reply to Martin Wilck

Describe the syntax of attribute / value pairs, comments, and quoted strings, as well as the peculiarities of section beginnings and ends. Also describe the newly added '""' feature.

---
 multipath/multipath.conf.5 | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
multipath/multipath.conf.5 +17 -0
index ab151e720d75..c4d0789475a3 100644
@@ -67,6 +67,23 @@ recognized keywords for attributes or subsections depend on the
 section in which they occur.
 .LP
 .
+\fB<attribute>\fR and \fB<value>\fR must be on a single line.
+\fB<attribute>\fR is one of the keywords listed in this man page.
+\fB<value>\fR is either a simple word (containing no whitespace and none of the
+characters '\(dq', '#', and '!') or \fIone\fR string enclosed in double
+quotes ("..."). Outside a quoted string, text starting with '#', and '!' is
+regarded as a comment and ignored until the end of the line. Inside a quoted
+string, '#' and '!' are normal characters, and whitespace is preserved.
+To represent a double quote character inside a double quoted string, use two
+consecutive double quotes ('""'). Thus '2.5\(dq SSD' can be written as "2.5"" SSD".
+.LP
+.
+Opening braces ('{') must follow the (sub)section name on the same line. Closing
+braces ('}') that mark the end of a (sub)section must be the only non-whitespace
+character on the line. Whitespace is ignored except inside double quotes, thus
+the indentation shown in the above example is helpful for human readers but
+not mandatory.
+.LP
 .
 The following \fIsection\fP keywords are recognized:
 .TP 17
Signature · 1 line
2.16.1
#7 lore raw
In reply to Martin Wilck

Just in case this is getting confusing, I've collected the latest versions of my submissions, and what I gathered from others since 0.7.4, here (currently 83 commits ahead of last upstream commit):

https://github.com/openSUSE/multipath-tools/tree/upstream-queue

Please notify me if I missed something or made other mistakes.

Regards,
Martin

Signature · 8 lines
Dr. Martin Wilck <[email protected]>, Tel. +49 (0)911 74053 2107
SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel
#8 lore raw
In reply to Martin Wilck
Quoted 7 lines · Martin Wilck

This series was motivated by the real-world problem that a user couldn't
figure out how to write a blacklist entry for a device called '1.8" SSD'.
Fixing this for good turned out to be a little tricky, therefore I also
added a test suite.

Christophe,

don't apply this yet. It breaks multipathd command parsing. Looking into it.

Martin

Signature · 8 lines
Dr. Martin Wilck <[email protected]>, Tel. +49 (0)911 74053 2107
SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel