Skip to content

Commit

Permalink
- builder.rb: catch encoding errors when parsing invalid encoding reg…
Browse files Browse the repository at this point in the history
…exp (#1033)

This errors when trying to set the regexp source string to binary.

prism parser logic: https://github.com/ruby/prism/blob/89c22f0e6cbe7aac81c789ca35786a9a97afcb57/src/prism.c#L7169-L7198

Closes #1032
  • Loading branch information
Earlopain authored Jul 31, 2024
1 parent 5bc1032 commit c7af34c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/parser/builders/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ def regexp_options(regopt_t)
def regexp_compose(begin_t, parts, end_t, options)
begin
static_regexp(parts, options)
rescue RegexpError => e
rescue RegexpError, Encoding::UndefinedConversionError => e
diagnostic :error, :invalid_regexp, { :message => e.message },
loc(begin_t).join(loc(end_t))
end
Expand Down
12 changes: 12 additions & 0 deletions test/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,18 @@ def test_regex_error
SINCE_1_9)
end

def test_regexp_error_invalid_encoding_conversion
message = if defined?(JRUBY_VERSION)
'"\\xE3\\x81\\x82" from UTF-8 to ASCII-8BIT'
else
'U+3042 from UTF-8 to ASCII-8BIT'
end
assert_diagnoses(
[:error, :invalid_regexp, { message: message }],
%q[/あ/n],
%q(~~~ location))
end

# Arrays

def test_array_plain
Expand Down

0 comments on commit c7af34c

Please sign in to comment.