Skip to content
Permalink
Browse files

Empty documents are valid

Consider empty YAML documents as valid when parsing if the input stream
is otherwise well formed. Previously an empty input stream or an empty
document embedded in an input stream would be treated as an input error
in contradiction to the YAML 1.2 spec. Now empty documents encountered
in the input stream will be emitted as null values as specified by
http://yaml.org/spec/1.2/spec.html#id2801681.

Bug: 75029
  • Loading branch information...
bd808 committed Nov 12, 2018
1 parent 28e9150 commit 262012a97024ae1b9d7ee1616ac59a8ad81493ff
Showing with 52 additions and 6 deletions.
  1. +1 −0 package.xml
  2. +19 −6 parse.c
  3. +32 −0 tests/bug_75029.phpt
@@ -66,6 +66,7 @@
<file role="test" name="bug_72540.phpt" />
<file role="test" name="bug_74799.phpt" />
<file role="test" name="bug_74799.yaml" />
<file role="test" name="bug_75029.phpt" />
<file role="test" name="bug_76309.phpt" />
<file role="test" name="bug_parsing_alias.phpt" />
<file role="test" name="yaml_001.phpt" />
25 parse.c
@@ -111,6 +111,14 @@ void php_yaml_read_all(parser_state_t *state, zend_long *ndocs, zval *retval TSR
code = Y_PARSER_FAILURE;
break;
}
if (YAML_STREAM_END_EVENT == state->event.type) {
/* entire stream consisted of an empty document */
code = Y_PARSER_SUCCESS;
ZVAL_NULL(&doc);
add_next_index_zval(retval, &doc);
(*ndocs)++;
break;
}

}

@@ -165,12 +173,17 @@ void php_yaml_read_partial(
code = Y_PARSER_FAILURE;

} else if (YAML_STREAM_END_EVENT == state->event.type) {
/* reached end of stream without finding what we wanted */
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"end of stream reached without finding document %ld",
pos);
code = Y_PARSER_FAILURE;

if (pos != 0) {
/* reached end of stream without finding what we wanted */
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"end of stream reached without finding document %ld",
pos);
code = Y_PARSER_FAILURE;
} else {
/* an empty document is valid YAML */
code = Y_PARSER_SUCCESS;
ZVAL_NULL(retval);
}
} else if (YAML_DOCUMENT_START_EVENT == state->event.type) {
if (*ndocs == pos) {
handle_document(state, retval TSRMLS_CC);
@@ -0,0 +1,32 @@
--TEST--
Test PECL bug #75029
--SKIPIF--
<?php if(!extension_loaded('yaml')) die('skip yaml n/a'); ?>
--FILE--
<?php
var_dump(yaml_parse(''));
var_dump(yaml_parse('# Empty', -1));
$doc = <<<EOD
doc
---
---
doc
EOD;
var_dump(yaml_parse($doc, -1));
var_dump(yaml_parse($doc, 1));
?>
--EXPECT--
NULL
array(1) {
[0]=>
NULL
}
array(3) {
[0]=>
string(3) "doc"
[1]=>
NULL
[2]=>
string(3) "doc"
}
NULL

0 comments on commit 262012a

Please sign in to comment.
You can’t perform that action at this time.