[ruby/yarp] fix: regular expression with start and end out of order

Also, a similar test and fix for interpolated regular expressions.

This snippet:

    <<-A.g//,
    A
    /{/, ''\

previously created a regular expression node with inverted start and
end:

    RegularExpressionNode(14...13)((14...15), (15...21), (12...13), ", ''", 0),

which failed an assertion during serialization.

After this change:

    RegularExpressionNode(12...15)((14...15), (15...21), (12...13), ", ''", 0),

Found by the fuzzer.

https://github.com/ruby/yarp/commit/5fef572f95
This commit is contained in:
Mike Dalessio 2023-08-30 13:30:29 -04:00 коммит произвёл git
Родитель 6beaf010a4
Коммит bbaae3681c
2 изменённых файлов: 19 добавлений и 3 удалений

Просмотреть файл

@ -33,5 +33,16 @@ module YARP
A
/, ""\\
EOF
snippet "regular expression with start and end out of order", <<~RUBY
<<-A.g//,
A
/{/, ''\\
RUBY
snippet "interpolated regular expression with start and end out of order", <<~RUBY
<<-A.g/{/,
A
a
/{/, ''\\
RUBY
end
end

Просмотреть файл

@ -2766,8 +2766,13 @@ yp_interpolated_regular_expression_node_create(yp_parser_t *parser, const yp_tok
static inline void
yp_interpolated_regular_expression_node_append(yp_interpolated_regular_expression_node_t *node, yp_node_t *part) {
if (node->base.location.start > part->location.start) {
node->base.location.start = part->location.start;
}
if (node->base.location.end < part->location.end) {
node->base.location.end = part->location.end;
}
yp_node_list_append(&node->parts, part);
node->base.location.end = part->location.end;
}
static inline void
@ -3600,8 +3605,8 @@ yp_regular_expression_node_create(yp_parser_t *parser, const yp_token_t *opening
.type = YP_NODE_REGULAR_EXPRESSION_NODE,
.flags = yp_regular_expression_flags_create(closing),
.location = {
.start = opening->start,
.end = closing->end
.start = MIN(opening->start, closing->start),
.end = MAX(opening->end, closing->end)
}
},
.opening_loc = YP_LOCATION_TOKEN_VALUE(opening),