summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Newton <kddnewton@gmail.com>2023-09-15 12:30:17 -0400
committergit <svn-admin@ruby-lang.org>2023-09-15 22:59:48 +0000
commit1be64e34d0881e5c66be51a892bcd3a056e8f5f0 (patch)
tree76793914f7d0f05a7c61826573d41fbda0f353ea
parent18780c22f657be2a0251fbf174fb46fd8523fae7 (diff)
[ruby/yarp] Alnum cannot be %-literal delimiters
https://github.com/ruby/yarp/commit/4ba6d5ca70
-rw-r--r--test/yarp/errors_test.rb14
-rw-r--r--yarp/yarp.c15
2 files changed, 26 insertions, 3 deletions
diff --git a/test/yarp/errors_test.rb b/test/yarp/errors_test.rb
index 54cbda14d1..41cb6b7f47 100644
--- a/test/yarp/errors_test.rb
+++ b/test/yarp/errors_test.rb
@@ -1255,6 +1255,20 @@ module YARP
assert_error_messages "0x1_1_", error_messages
end
+ def test_alnum_delimiters
+ error_messages = ["Invalid `%` token"]
+
+ assert_error_messages "%qXfooX", error_messages
+ assert_error_messages "%QXfooX", error_messages
+ assert_error_messages "%wXfooX", error_messages
+ assert_error_messages "%WxfooX", error_messages
+ assert_error_messages "%iXfooX", error_messages
+ assert_error_messages "%IXfooX", error_messages
+ assert_error_messages "%xXfooX", error_messages
+ assert_error_messages "%rXfooX", error_messages
+ assert_error_messages "%sXfooX", error_messages
+ end
+
private
def assert_errors(expected, source, errors, compare_ripper: RUBY_ENGINE == "ruby")
diff --git a/yarp/yarp.c b/yarp/yarp.c
index b343566ee0..7c4b347678 100644
--- a/yarp/yarp.c
+++ b/yarp/yarp.c
@@ -7077,9 +7077,10 @@ parser_lex(yp_parser_t *parser) {
// % %= %i %I %q %Q %w %W
case '%': {
- // If there is no subsequent character then we have an invalid token. We're
- // going to say it's the percent operator because we don't want to move into the
- // string lex mode unnecessarily.
+ // If there is no subsequent character then we have an
+ // invalid token. We're going to say it's the percent
+ // operator because we don't want to move into the string
+ // lex mode unnecessarily.
if ((lex_state_beg_p(parser) || lex_state_arg_p(parser)) && (parser->current.end >= parser->end)) {
yp_diagnostic_list_append(&parser->error_list, parser->current.start, parser->current.end, YP_ERR_INVALID_PERCENT);
LEX(YP_TOKEN_PERCENT);
@@ -7110,6 +7111,14 @@ parser_lex(yp_parser_t *parser) {
}
}
+ // Delimiters for %-literals cannot be alphanumeric. We
+ // validate that here.
+ uint8_t delimiter = peek_offset(parser, 1);
+ if (delimiter >= 0x80 || parser->encoding.alnum_char(&delimiter, 1)) {
+ yp_diagnostic_list_append(&parser->error_list, parser->current.start, parser->current.end, YP_ERR_INVALID_PERCENT);
+ goto lex_next_token;
+ }
+
switch (peek(parser)) {
case 'i': {
parser->current.end++;