Fixes for config file parsing
v2 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.
Changes since v1:
- fixed a problem with parsing the command strings from cli handlers.
Martin Wilck (5):
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 | 60 ++++-- libmultipath/parser.h | 1 + multipath/multipath.conf.5 | 17 ++ multipathd/cli.c | 2 +- tests/Makefile | 2 +- tests/globals.c | 1 + tests/parser.c | 474 +++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 540 insertions(+), 17 deletions(-) create mode 100644 tests/parser.c
Signature · 1 line
2.16.1
PATCH v2 1/5 tests: add unit tests for config file parser
Add test cases for parsing the config file.
Some of these tests currently fail. The patches that follow fix them.
--- tests/Makefile | 2 +- tests/globals.c | 1 + tests/parser.c | 479 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 481 insertions(+), 1 deletion(-) create mode 100644 tests/parser.c
tests/Makefile +1 -1
@@ -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
@@ -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 +479 -0
479 lines not on this page · raw message
Signature · 1 line
2.16.1
PATCH v2 2/5 libmultipath: config parser: don't strip whitepace between quotes
In reply to Martin WilckBetween 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
@@ -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
PATCH v2 3/5 libmultipath: config parser: Allow '"' in strings
In reply to Martin WilckWe 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
@@ -219,11 +219,13 @@ alloc_strvec(char *string) in_string =0 ;while (1 ) { +int two_quotes =0 ; +if (!vector_alloc_slot (strvec)) goto out; start = cp; -if (*cp =='"' ) { +if (*cp =='"' && !(in_string && *(cp +1 ) =='"' )) { cp++; token =MALLOC (2 );
@@ -246,11 +248,23 @@ alloc_strvec(char *string) *(token +1 ) ='\0' ; cp++; }else { + + 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; + } + } + 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
Re: [PATCH v2 3/5] libmultipath: config parser: Allow '"' in strings
Quoted 9 lines · 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" }
Could this work in the first place?
product "2.5\" SSD"
Re: [PATCH v2 3/5] libmultipath: config parser: Allow '"' in strings
Quoted 14 lines · Xose Vazquez Perez
Quoted 10 lines · 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" }
Quoted 3 lines
Could this work in the first place?
product "2.5\" SSD"
I'm not sure what you mean. For the current code, the answer is "no". The parser doesn't treat '\' special in any way. For the parser, the result of the above is the tuple ['product', '2.5\', 'SSD']. The config file parser would discard the last tuple element (the cli command parser takes more than 2 tuple elements, but not the config file parser). The regexp parser would bail out on the lone backslash in '2.5\'.
If you meant to say that, instead of using '""', I could have changed the parser to use '\"' to represent a single double-quote character, yes, that would have been possible, at the cost of a much bigger and more error-prone rewrite of the parsing code.
Moreover, I think using '\"' has an addtional disadvantage compared to '""'. As you know, the values for many options are regular expressions, and the backslash has special meaning in regexps. Figuring out the correct number of backslashes for complex regexps is hard anyway, and if we give '\' special meaning in the option parser, too, we'd treat it specially on two separate layers, making it even harder for users. The double-quote character, OTOH, has no special meaning in regexps, so that the two quoting mechanisms on the two layers are orthogonal.
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
PATCH v2 4/5 libmultipath: config parser: fix corner case for double quotes
In reply to Martin WilckA 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 | 31 ++++++++++++++++++------------- libmultipath/parser.h | 1 + multipathd/cli.c | 2 +- tests/parser.c | 5 ----- 4 files changed, 20 insertions(+), 19 deletions(-)
libmultipath/parser.c +18 -13
@@ -186,6 +186,12 @@ snprint_keyword(char *buff, int len, char *fmt, struct keyword *kw,return fwd; } +static const char quote_marker[] = {'\0' ,'"' ,'\0' }; +bool is_quote (const char * token) +{ +return !memcmp (token, quote_marker,sizeof (quote_marker)); +} +vector alloc_strvec (char *string) {
@@ -227,13 +233,12 @@ alloc_strvec(char *string) start = cp;if (*cp =='"' && !(in_string && *(cp +1 ) =='"' )) { cp++; - token =MALLOC (2 ); + token =MALLOC (sizeof (quote_marker));if (!token) goto out; - *(token) ='"' ; - *(token +1 ) ='\0' ; +memcpy (token, quote_marker,sizeof (quote_marker));if (in_string) in_string =0 ;else
@@ -324,13 +329,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 +359,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 +465,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 +477,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 ;
libmultipath/parser.h +1 -0
@@ -81,5 +81,6 @@ extern int process_file(struct config *conf, char *conf_file);extern struct keyword *find_keyword (vector keywords,vector v,char * name);int snprint_keyword (char *buff,int len,char *fmt,struct keyword *kw,const void *data); +bool is_quote (const char * token);#endif
multipathd/cli.c +1 -1
@@ -279,7 +279,7 @@ get_cmdvec (char * cmd, vector *v) } vector_foreach_slot(strvec, buff, i) { -if (*buff =='"' ) +if (is_quote (buff))continue ;if (get_param) { get_param =0 ;
tests/parser.c +0 -5
@@ -34,11 +34,6 @@/* Stop parsing at 2nd quote */ #define TWO_QUOTES_ONLY 0 -static bool is_quote (const char *s) -{ -return *s =='"' ; -} -static char *test_file ="test.conf" ;/* Missing declaration */
Signature · 1 line
2.16.1
PATCH v2 5/5 multipath.conf(5): improve syntax documentation
In reply to Martin WilckDescribe 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
@@ -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
Quoted 8 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.
Changes since v1:
- fixed a problem with parsing the command strings from cli handlers.
for the set.
Quoted 19 lines
Martin Wilck (5):
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 quotesmultipath.conf(5): improve syntax documentationlibmultipath/parser.c | 60 ++++-- libmultipath/parser.h | 1 + multipath/multipath.conf.5 | 17 ++ multipathd/cli.c | 2 +- tests/Makefile | 2 +- tests/globals.c | 1 + tests/parser.c | 474 +++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 540 insertions(+), 17 deletions(-) create mode 100644 tests/parser.cSignature · 1 line
2.16.1