Skip to content

Commit

Permalink
Merge pull request #17 from h2o/kazuho/merge-avoid-recursion
Browse files Browse the repository at this point in the history
avoid infinite recursion when merging a tree with loops
  • Loading branch information
kazuho authored May 16, 2024
2 parents cfb6e2a + 7cddc61 commit 5f60dfe
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
6 changes: 6 additions & 0 deletions yoml-parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ static inline yoml_t *yoml__new_node(const char *filename, yoml_type_t type, siz
node->anchor = anchor != NULL ? yoml__strdup(anchor) : NULL;
node->tag = tag != NULL ? yoml__strdup(tag) : NULL;
node->_refcnt = 1;
node->_merged = 0;
return node;
}

Expand Down Expand Up @@ -199,6 +200,7 @@ static inline int yoml__merge(yoml_t **dest, size_t offset, size_t delete_count,
((*dest)->data.mapping.size + src->data.mapping.size - delete_count) *
sizeof(new_node->data.mapping.elements[0]),
(void *)(*dest)->anchor, (void *)(*dest)->tag, (*dest)->line, (*dest)->column);
new_node->_merged = (*dest)->_merged;
memcpy(new_node->data.mapping.elements, (*dest)->data.mapping.elements, offset * sizeof(new_node->data.mapping.elements[0]));
new_node->data.mapping.size = offset;

Expand Down Expand Up @@ -239,6 +241,10 @@ static inline int yoml__resolve_merge(yoml_t **target, yaml_parser_t *parser, yo
{
size_t i, j;

if ((*target)->_merged)
return 0;
(*target)->_merged = 1;

switch ((*target)->type) {
case YOML_TYPE_SCALAR:
break;
Expand Down
1 change: 1 addition & 0 deletions yoml.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ struct st_yoml_t {
char *anchor;
char *tag;
size_t _refcnt;
unsigned _merged : 1;
union {
char *scalar;
yoml_sequence_t sequence;
Expand Down

0 comments on commit 5f60dfe

Please sign in to comment.