-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix #70962: xml_parse_into_struct strips embedded whitespace with XML… #7493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--TEST-- | ||
Bug #70962: xml_parse_into_struct strips embedded whitespace with XML_OPTION_SKIP_WHITE | ||
--SKIPIF-- | ||
<?php | ||
if (!extension_loaded('xml')) die('skip xml extension not available'); | ||
?> | ||
--FILE-- | ||
<?php | ||
function parseAndOutput($xml) | ||
{ | ||
$parser = xml_parser_create(); | ||
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); | ||
|
||
xml_parse_into_struct($parser, $xml, $values); | ||
|
||
return $values; | ||
} | ||
|
||
$xml = "<a><b><d>\n <e></b><c>\n \t</c></a>"; | ||
|
||
$parsed = parseAndOutput($xml); | ||
|
||
// Check embedded whitespace is not getting skipped. | ||
echo $parsed[1]['value'] . "\n"; | ||
|
||
// Check XML_OPTION_SKIP_WHITE ignores values of tags containing whitespace characters only. | ||
var_dump(isset($parsed[2]['value'])); | ||
|
||
?> | ||
--EXPECT-- | ||
<d> | ||
<e> | ||
bool(false) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -881,78 +881,81 @@ void _xml_characterDataHandler(void *userData, const XML_Char *s, int len) | |||||
} | ||||||
|
||||||
if (!Z_ISUNDEF(parser->data)) { | ||||||
size_t i; | ||||||
int doprint = 0; | ||||||
zend_string *decoded_value; | ||||||
|
||||||
decoded_value = xml_utf8_decode(s, len, parser->target_encoding); | ||||||
for (i = 0; i < ZSTR_LEN(decoded_value); i++) { | ||||||
switch (ZSTR_VAL(decoded_value)[i]) { | ||||||
case ' ': | ||||||
case '\t': | ||||||
case '\n': | ||||||
continue; | ||||||
default: | ||||||
doprint = 1; | ||||||
break; | ||||||
} | ||||||
if (doprint) { | ||||||
break; | ||||||
} | ||||||
} | ||||||
if (doprint || (! parser->skipwhite)) { | ||||||
if (parser->lastwasopen) { | ||||||
zval *myval; | ||||||
|
||||||
/* check if the current tag already has a value - if yes append to that! */ | ||||||
if ((myval = zend_hash_str_find(Z_ARRVAL_P(parser->ctag), "value", sizeof("value") - 1))) { | ||||||
int newlen = Z_STRLEN_P(myval) + ZSTR_LEN(decoded_value); | ||||||
Z_STR_P(myval) = zend_string_extend(Z_STR_P(myval), newlen, 0); | ||||||
strncpy(Z_STRVAL_P(myval) + Z_STRLEN_P(myval) - ZSTR_LEN(decoded_value), | ||||||
ZSTR_VAL(decoded_value), ZSTR_LEN(decoded_value) + 1); | ||||||
zend_string_release_ex(decoded_value, 0); | ||||||
} else { | ||||||
add_assoc_str(parser->ctag, "value", decoded_value); | ||||||
} | ||||||
|
||||||
} else { | ||||||
zval tag; | ||||||
zval *curtag, *mytype, *myval; | ||||||
|
||||||
ZEND_HASH_REVERSE_FOREACH_VAL(Z_ARRVAL(parser->data), curtag) { | ||||||
if ((mytype = zend_hash_str_find(Z_ARRVAL_P(curtag),"type", sizeof("type") - 1))) { | ||||||
if (!strcmp(Z_STRVAL_P(mytype), "cdata")) { | ||||||
if ((myval = zend_hash_str_find(Z_ARRVAL_P(curtag), "value", sizeof("value") - 1))) { | ||||||
int newlen = Z_STRLEN_P(myval) + ZSTR_LEN(decoded_value); | ||||||
Z_STR_P(myval) = zend_string_extend(Z_STR_P(myval), newlen, 0); | ||||||
strncpy(Z_STRVAL_P(myval) + Z_STRLEN_P(myval) - ZSTR_LEN(decoded_value), | ||||||
ZSTR_VAL(decoded_value), ZSTR_LEN(decoded_value) + 1); | ||||||
zend_string_release_ex(decoded_value, 0); | ||||||
return; | ||||||
} | ||||||
} | ||||||
} | ||||||
break; | ||||||
} ZEND_HASH_FOREACH_END(); | ||||||
|
||||||
if (parser->level <= XML_MAXLEVEL && parser->level > 0) { | ||||||
array_init(&tag); | ||||||
|
||||||
_xml_add_to_info(parser,SKIP_TAGSTART(parser->ltags[parser->level-1])); | ||||||
|
||||||
add_assoc_string(&tag, "tag", SKIP_TAGSTART(parser->ltags[parser->level-1])); | ||||||
add_assoc_str(&tag, "value", decoded_value); | ||||||
add_assoc_string(&tag, "type", "cdata"); | ||||||
add_assoc_long(&tag, "level", parser->level); | ||||||
|
||||||
zend_hash_next_index_insert(Z_ARRVAL(parser->data), &tag); | ||||||
} else if (parser->level == (XML_MAXLEVEL + 1)) { | ||||||
php_error_docref(NULL, E_WARNING, "Maximum depth exceeded - Results truncated"); | ||||||
} | ||||||
} | ||||||
} else { | ||||||
zend_string_release_ex(decoded_value, 0); | ||||||
} | ||||||
if (parser->lastwasopen) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use tabs (as four spaces) for indentation. |
||||||
zval *myval; | ||||||
|
||||||
/* check if the current tag already has a value - if yes append to that! */ | ||||||
if ((myval = zend_hash_str_find(Z_ARRVAL_P(parser->ctag), "value", sizeof("value") - 1))) { | ||||||
int newlen = Z_STRLEN_P(myval) + ZSTR_LEN(decoded_value); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
That was |
||||||
Z_STR_P(myval) = zend_string_extend(Z_STR_P(myval), newlen, 0); | ||||||
strncpy(Z_STRVAL_P(myval) + Z_STRLEN_P(myval) - ZSTR_LEN(decoded_value), | ||||||
ZSTR_VAL(decoded_value), ZSTR_LEN(decoded_value) + 1); | ||||||
zend_string_release_ex(decoded_value, 0); | ||||||
} else { | ||||||
size_t i; | ||||||
int doprint = 0; | ||||||
if (parser->skipwhite) { | ||||||
for (i = 0; i < ZSTR_LEN(decoded_value); i++) { | ||||||
switch (ZSTR_VAL(decoded_value)[i]) { | ||||||
case ' ': | ||||||
case '\t': | ||||||
case '\n': | ||||||
continue; | ||||||
default: | ||||||
doprint = 1; | ||||||
break; | ||||||
} | ||||||
if (doprint) { | ||||||
break; | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
if (doprint || !parser->skipwhite) { | ||||||
add_assoc_str(parser->ctag, "value", decoded_value); | ||||||
} else { | ||||||
zend_string_release_ex(decoded_value, 0); | ||||||
} | ||||||
} | ||||||
|
||||||
} else { | ||||||
zval tag; | ||||||
zval *curtag, *mytype, *myval; | ||||||
|
||||||
ZEND_HASH_REVERSE_FOREACH_VAL(Z_ARRVAL(parser->data), curtag) { | ||||||
if ((mytype = zend_hash_str_find(Z_ARRVAL_P(curtag),"type", sizeof("type") - 1))) { | ||||||
if (!strcmp(Z_STRVAL_P(mytype), "cdata")) { | ||||||
if ((myval = zend_hash_str_find(Z_ARRVAL_P(curtag), "value", sizeof("value") - 1))) { | ||||||
int newlen = Z_STRLEN_P(myval) + ZSTR_LEN(decoded_value); | ||||||
Z_STR_P(myval) = zend_string_extend(Z_STR_P(myval), newlen, 0); | ||||||
strncpy(Z_STRVAL_P(myval) + Z_STRLEN_P(myval) - ZSTR_LEN(decoded_value), | ||||||
ZSTR_VAL(decoded_value), ZSTR_LEN(decoded_value) + 1); | ||||||
zend_string_release_ex(decoded_value, 0); | ||||||
return; | ||||||
} | ||||||
} | ||||||
} | ||||||
break; | ||||||
} ZEND_HASH_FOREACH_END(); | ||||||
|
||||||
if (parser->level <= XML_MAXLEVEL && parser->level > 0) { | ||||||
array_init(&tag); | ||||||
|
||||||
_xml_add_to_info(parser,SKIP_TAGSTART(parser->ltags[parser->level-1])); | ||||||
|
||||||
add_assoc_string(&tag, "tag", SKIP_TAGSTART(parser->ltags[parser->level-1])); | ||||||
add_assoc_str(&tag, "value", decoded_value); | ||||||
add_assoc_string(&tag, "type", "cdata"); | ||||||
add_assoc_long(&tag, "level", parser->level); | ||||||
|
||||||
zend_hash_next_index_insert(Z_ARRVAL(parser->data), &tag); | ||||||
} else if (parser->level == (XML_MAXLEVEL + 1)) { | ||||||
php_error_docref(NULL, E_WARNING, "Maximum depth exceeded - Results truncated"); | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: missing line break