From: Matthew Fernandez Date: Sat, 16 Oct 2021 05:03:27 +0000 (-0700) Subject: xml_core: update input pointer to reflect how many characters were consumed X-Git-Tag: 2.50.0~64^2~3 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=315c6ffc4e0afc3875ddadd0a6a3dcc99a6c41d3;p=graphviz xml_core: update input pointer to reflect how many characters were consumed This has no immediate effect because the function only ever consumes a single character. However, a future change will introduce more sophisticated escaping that sometimes involves consuming more than one character from the input. Related to #1868. --- diff --git a/lib/common/xml.c b/lib/common/xml.c index 18731015b..7a6faf9fc 100644 --- a/lib/common/xml.c +++ b/lib/common/xml.c @@ -45,8 +45,9 @@ static bool xml_isentity(const char *s) * * \param previous The source character preceding the current one or '\0' if * there was no prior character. - * \param current Pointer to the current position in a source string being - * escaped. + * \param[in, out] current Pointer to the current position in a source string + * being escaped. The pointer is updated based on how many characters are + * consumed. * \param flags Options for configuring behavior. * \param cb User function for emitting escaped data. This is expected to take a * caller-defined state type as the first parameter and the string to emit as @@ -55,13 +56,17 @@ static bool xml_isentity(const char *s) * \param state Data to pass as the first parameter when calling `cb`. * \return The return value of a call to `cb`. */ -static int xml_core(char previous, const char *current, xml_flags_t flags, +static int xml_core(char previous, const char **current, xml_flags_t flags, int (*cb)(void *state, const char *s), void *state) { - char c = *current; + const char *s = *current; + char c = *s; + + // we always consume one character for now + ++*current; // escape '&' only if not part of a legal entity sequence - if (c == '&' && (flags.raw || !xml_isentity(current))) + if (c == '&' && (flags.raw || !xml_isentity(s))) return cb(state, "&"); // '<' '>' are safe to substitute even if string is already UTF-8 coded since @@ -102,11 +107,11 @@ int xml_escape(const char *s, xml_flags_t flags, char previous = '\0'; int rc = 0; while (*s != '\0') { - rc = xml_core(previous, s, flags, cb, state); + char p = *s; + rc = xml_core(previous, &s, flags, cb, state); if (rc < 0) return rc; - previous = *s; - ++s; + previous = p; } return rc; }