summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Dalessio <[email protected]>2023-08-30 13:30:29 -0400
committergit <[email protected]>2023-08-30 20:46:09 +0000
commitbbaae3681ca9b4d00f2bb2330da1c7e3ff06cd17 (patch)
treeb8eee6f4406a2f3dabb229b015815ef87efe66aa
parent6beaf010a447ce9fb35c85f9fb2f41685e2114ba (diff)
[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
-rw-r--r--test/yarp/fuzzer_test.rb11
-rw-r--r--yarp/yarp.c11
2 files changed, 19 insertions, 3 deletions
diff --git a/test/yarp/fuzzer_test.rb b/test/yarp/fuzzer_test.rb
index 4d7a0af8e7..568c2eaf08 100644
--- a/test/yarp/fuzzer_test.rb
+++ b/test/yarp/fuzzer_test.rb
@@ -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
diff --git a/yarp/yarp.c b/yarp/yarp.c
index 5b8fba9f62..5381eabe00 100644
--- a/yarp/yarp.c
+++ b/yarp/yarp.c
@@ -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),