xml_node: support backslash as attribute value

XML allows attribute values like <node attr="\"/>. The XML parser
wrongly reflects this case as 'Invalid_syntax'. This behavior stems from
the implicit use of the 'end_of_quote' function, which considers the
sequence of '\"' as a quoted '"' rather than the end of a quoted string.

The patch solves this problem by making the 'end_of_quote' part of
the tokenizer's scanner policy.

The patch removes the 'end_of_quote' function from 'util/string.h'
because it is not universal, and to avoid the ambiguity with
'SCANNER_POLICY::end_of_quote'.

Fixes #4431
This commit is contained in:
Norman Feske
2022-02-17 12:17:19 +01:00
parent 494f881f27
commit c2efa5406e
10 changed files with 58 additions and 47 deletions

View File

@@ -512,15 +512,6 @@ namespace Genode {
}
/**
* Check for end of quotation
*
* Checks if next character is non-backslashed quotation mark.
*/
inline bool end_of_quote(const char *s) {
return s[0] != '\\' && s[1] == '\"'; }
/**
* Unpack quoted string
*
@@ -537,6 +528,9 @@ namespace Genode {
src++;
auto end_of_quote = [] (const char *s) {
return s[0] != '\\' && s[1] == '\"'; };
size_t i = 0;
for (; *src && !end_of_quote(src - 1) && (i < dst_len - 1); i++) {

View File

@@ -40,6 +40,19 @@ struct Genode::Scanner_policy_identifier_with_underline
*/
static bool identifier_char(char c, unsigned i) {
return is_letter(c) || (c == '_') || (i && is_digit(c)); }
/**
* Check for end of quotation
*
* Checks if next character is non-backslashed quotation mark.
* The end of a quoted string is reached when encountering a '"'
* character that is not preceded by a backslash.
*
* \param s pointer to null-terminated string with at least one
* character
*/
static bool end_of_quote(const char *s) {
return s[0] != '\\' && s[1] == '\"'; }
};
@@ -184,7 +197,7 @@ class Genode::Token
* Hence, the upper bound of the index is max_len - 2.
*/
unsigned i = 0;
for (; i + 1 < max_len && !end_of_quote(&_start[i]); i++)
for (; i + 1 < max_len && !SCANNER_POLICY::end_of_quote(&_start[i]); i++)
/* string ends without final quotation mark? too bad! */
if (!_start[i]) return 0;

View File

@@ -35,13 +35,17 @@ class Genode::Xml_attribute
{
private:
/**
* Scanner policy that accepts hyphens in identifiers
*/
struct Scanner_policy_xml_identifier {
static bool identifier_char(char c, unsigned i) {
struct Scanner_policy_xml_identifier
{
static bool identifier_char(char c, unsigned i)
{
/* accepts hyphens in identifiers */
return is_letter(c) || c == '_' || c == ':'
|| (i && (c == '-' || c == '.' || is_digit(c))); } };
|| (i && (c == '-' || c == '.' || is_digit(c)));
}
static bool end_of_quote(const char *s) { return s[1] == '\"'; }
};
/**
* Define tokenizer that matches XML tags (with hyphens) as identifiers