[ruby/yarp] fix: StatementsNode with out-of-order body nodes

The presence of the heredocs in this snippet with invalid syntax:

    for <<A + <<B
    A
    B

causes the MissingNode to have a location after other nodes in the
list, resulting in a StatementsNode with inverted start and end
locations:

      [ForNode(0...14)(
         MultiWriteNode(4...7)([InterpolatedStringNode(4...7)((4...7), [], (14...16))], nil, nil, nil, nil),
         MissingNode(16...16)(),
    >    StatementsNode(16...14)(
           [MissingNode(16...16)(), InterpolatedStringNode(10...13)((10...13), [], (16...18)), MissingNode(13...14)()]
         ),
         (0...3),
         (16...16),
         nil,
         (14...14)
       )]

which failed an assertion during serialization.

With this fix, the node's locations are:

      [ForNode(0...14)(
         MultiWriteNode(4...7)([InterpolatedStringNode(4...7)((4...7), [], (14...16))], nil, nil, nil, nil),
         MissingNode(16...16)(),
    >    StatementsNode(10...16)(
           [MissingNode(16...16)(), InterpolatedStringNode(10...13)((10...13), [], (16...18)), MissingNode(13...14)()]
         ),
         (0...3),
         (16...16),
         nil,
         (14...14)
       )]

Found by the fuzzer.

09bcedc05e
This commit is contained in:
Mike Dalessio 2023-08-30 09:59:09 -04:00 committed by git
parent 209eda599a
commit f80582cda8
2 changed files with 10 additions and 2 deletions

View file

@ -22,5 +22,11 @@ module YARP
snippet "incomplete escaped list", "%w[\\"
snippet "incomplete escaped regex", "/a\\"
snippet "unterminated heredoc with unterminated escape at end of file", "<<A\n\\"
snippet "statements node with multiple heredocs", <<~EOF
for <<A + <<B
A
B
EOF
end
end

View file

@ -3908,12 +3908,14 @@ yp_statements_node_location_set(yp_statements_node_t *node, const uint8_t *start
// Append a new node to the given StatementsNode node's body.
static void
yp_statements_node_body_append(yp_statements_node_t *node, yp_node_t *statement) {
if (yp_statements_node_body_length(node) == 0) {
if (yp_statements_node_body_length(node) == 0 || statement->location.start < node->base.location.start) {
node->base.location.start = statement->location.start;
}
if (statement->location.end > node->base.location.end) {
node->base.location.end = statement->location.end;
}
yp_node_list_append(&node->body, statement);
node->base.location.end = statement->location.end;
// Every statement gets marked as a place where a newline can occur.
statement->flags |= YP_NODE_FLAG_NEWLINE;