Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Style/FrozenStringLiteralComment - invalid byte sequence in UTF-8 #12034

Closed
snutij opened this issue Jul 7, 2023 · 0 comments · Fixed by #12035
Closed

Style/FrozenStringLiteralComment - invalid byte sequence in UTF-8 #12034

snutij opened this issue Jul 7, 2023 · 0 comments · Fixed by #12035
Labels

Comments

@snutij
Copy link

snutij commented Jul 7, 2023

Hello 👋 I have a tricky issue, with Style/FrozenStringLiteralComment. Regex give me nightmare, so can't help more than providing as much as possible information. Do not hesitate if you need something else!

Given a file foo.rb:

"\xE3\xD3\x8B\xE3\x83\xBC\x83\xE3\x83\xE3\x82\xB3\xA3\x82\x99" \
        "\xE3\x83\xB3\xE3\x83\x8F\xE3\x82\x9A\xC3\xBD;foo@bar.com"

Which is a valid syntax:

ruby -c foo.rb
Syntax OK

Expected behavior

Should not trigger an error. And probably propose a fix to add the comment.

Actual behavior

bin/rubocop -d --only Style/FrozenStringLiteralComment foo.rb

An error occurred while Style/FrozenStringLiteralComment cop was inspecting foo.rb.
invalid byte sequence in UTF-8
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.54.1/lib/rubocop/cop/style/frozen_string_literal_comment.rb:145:in `match?'
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.54.1/lib/rubocop/cop/style/frozen_string_literal_comment.rb:145:in `last_special_comment'
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.54.1/lib/rubocop/cop/style/frozen_string_literal_comment.rb:195:in `insert_comment'
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.54.1/lib/rubocop/cop/style/frozen_string_literal_comment.rb:159:in `block in missing_offense'
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.54.1/lib/rubocop/cop/base.rb:377:in `correct'
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.54.1/lib/rubocop/cop/base.rb:181:in `add_offense'

Steps to reproduce the problem

In Style::FrozenStringLiteralComment the method last_special_comment receive an argument processed_source,

(ruby) processed_source.raw_source
"\"\\xE3\\xD3\\x8B\\xE3\\x83\\xBC\\x83\\xE3\\x83\\xE3\\x82\\xB3\\xA3\\x82\\x99\" \\\n        \"\\xE3\\x83\\xB3\\xE3\\x83\\x8F\\xE3\\x82\\x9A\\xC3\\xBD;foo@bar.com\"\n"

(ruby) processed_source.ast
s(:dstr,
  s(:str, "\xE3Ӌー\x83\xE3\x83\xA3\x82\x99"),
  s(:str, "ンパý;foo@bar.com"))

Its raised an error on Regex match check:

(ruby) next_token
#<RuboCop::AST::Token:0x00000001078413b0
 @pos=#<Parser::Source::Range /Users/justinrabiller/Documents/www/ruby-lsp/test/requests/diagnostics_expectations_test.rb 0...62>,
 @text="\xE3Ӌー\x83\xE3\x83\xA3\x82\x99",
 @type=:tSTRING>

(ruby) next_token&.text
"\xE3Ӌー\x83\xE3\x83\xA3\x82\x99"

(ruby) Encoding::ENCODING_PATTERN
/#.*coding\s?[:=]\s?(?:UTF|utf)-8/

(ruby) Encoding::ENCODING_PATTERN.match?(next_token&.text)
eval error: invalid byte sequence in UTF-8

def last_special_comment(processed_source)
token_number = 0
if processed_source.tokens[token_number].text.start_with?(SHEBANG)
token = processed_source.tokens[token_number]
token_number += 1
end
next_token = processed_source.tokens[token_number]
token = next_token if Encoding::ENCODING_PATTERN.match?(next_token&.text)
token
end

RuboCop version

bundle exec rubocop -V

1.54.1 (using Parser 3.2.2.3, rubocop-ast 1.29.0, running on ruby 3.2.2) [arm64-darwin22]
  - rubocop-minitest 0.31.0
  - rubocop-rake 0.6.0
  - rubocop-sorbet 0.7.0
@koic koic added the bug label Jul 7, 2023
koic added a commit to koic/rubocop that referenced this issue Jul 8, 2023
Fixes rubocop#12034.

This PR fixes invalid byte sequence in UTF-8 error when using an invalid encoding string.
koic added a commit that referenced this issue Jul 9, 2023
…d_encoding_string

[Fix #12034] Fix an error when using an invalid encoding string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants