increase and decrease depth properly on subhandlers

the original implementation couldn't parse a document with more than
depth_limit entries. now we explicitly increase *and* decrease the
depth on specific handlers like maps, sequences and so on - any
handler that may in turn callback into HandleNode().

this is a little clunky - I would have prefered to increment and
decrement the counter in only one place, but there are many different
return points and this is not Golang so I can't think of a better way
to to this.
This commit is contained in:
Antoine Beaupré 2017-04-26 10:39:45 -04:00
parent ac00ef9377
commit e78e3bf6a6
No known key found for this signature in database
GPG Key ID: 792152527B75921E

View File

@ -49,7 +49,6 @@ void SingleDocParser::HandleNode(EventHandler& eventHandler) {
if (depth > depth_limit) {
throw ParserException(m_scanner.mark(), ErrorMsg::BAD_FILE);
}
depth++;
// an empty node *is* a possibility
if (m_scanner.empty()) {
eventHandler.OnNull(m_scanner.mark(), NullAnchor);
@ -61,9 +60,11 @@ void SingleDocParser::HandleNode(EventHandler& eventHandler) {
// special case: a value node by itself must be a map, with no header
if (m_scanner.peek().type == Token::VALUE) {
depth++;
eventHandler.OnMapStart(mark, "?", NullAnchor, EmitterStyle::Default);
HandleMap(eventHandler);
eventHandler.OnMapEnd();
depth--;
return;
}
@ -98,32 +99,42 @@ void SingleDocParser::HandleNode(EventHandler& eventHandler) {
m_scanner.pop();
return;
case Token::FLOW_SEQ_START:
depth++;
eventHandler.OnSequenceStart(mark, tag, anchor, EmitterStyle::Flow);
HandleSequence(eventHandler);
eventHandler.OnSequenceEnd();
depth--;
return;
case Token::BLOCK_SEQ_START:
depth++;
eventHandler.OnSequenceStart(mark, tag, anchor, EmitterStyle::Block);
HandleSequence(eventHandler);
eventHandler.OnSequenceEnd();
depth--;
return;
case Token::FLOW_MAP_START:
depth++;
eventHandler.OnMapStart(mark, tag, anchor, EmitterStyle::Flow);
HandleMap(eventHandler);
eventHandler.OnMapEnd();
depth--;
return;
case Token::BLOCK_MAP_START:
depth++;
eventHandler.OnMapStart(mark, tag, anchor, EmitterStyle::Block);
HandleMap(eventHandler);
eventHandler.OnMapEnd();
depth--;
return;
case Token::KEY:
// compact maps can only go in a flow sequence
if (m_pCollectionStack->GetCurCollectionType() ==
CollectionType::FlowSeq) {
depth++;
eventHandler.OnMapStart(mark, tag, anchor, EmitterStyle::Flow);
HandleMap(eventHandler);
eventHandler.OnMapEnd();
depth--;
return;
}
break;