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

Prevent Style/WordArray from breaking on strings that aren't valid UTF-8 #4442

Merged
merged 2 commits into from
May 29, 2017

Conversation

pocke
Copy link
Collaborator

@pocke pocke commented May 29, 2017

Style/WordArray cop crashes on strings that aren't valid UTF-8, and encoding: binary is specified.

For example

# -*- encoding: binary -*-
["\xC0",
 "\xC2\x4a",
 "\xC2\xC2",
 "\x4a\x82",
 "\x82\x82",
 "\xe1\x82\x4a",
]
$ rubocop -Dd --only WordArray
For /tmp/tmp.GDijgfYoeb: configuration from /home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/config/default.yml
Inheriting configuration from /home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/config/enabled.yml
Inheriting configuration from /home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/config/disabled.yml
Inspecting 1 file
Scanning /tmp/tmp.GDijgfYoeb/test.rb
"\xC0"
true
incompatible encoding regexp match (UTF-8 regexp with ASCII-8BIT string)
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/style/word_array.rb:99:in `=~'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/style/word_array.rb:99:in `!~'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/style/word_array.rb:99:in `block in complex_content?'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/style/word_array.rb:94:in `any?'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/style/word_array.rb:94:in `complex_content?'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/style/word_array.rb:67:in `check_bracketed_array'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/style/word_array.rb:50:in `on_array'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/commissioner.rb:44:in `block (2 levels) in on_array'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/commissioner.rb:106:in `with_cop_error_handling'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/commissioner.rb:43:in `block in on_array'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/commissioner.rb:42:in `each'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/commissioner.rb:42:in `on_array'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/ast/traversal.rb:12:in `walk'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/commissioner.rb:60:in `investigate'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/team.rb:112:in `investigate'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/team.rb:100:in `offenses'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/team.rb:42:in `inspect_file'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:258:in `inspect_file'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:205:in `block in do_inspection_loop'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:237:in `block in iterate_until_no_changes'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:230:in `loop'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:230:in `iterate_until_no_changes'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:201:in `do_inspection_loop'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:111:in `block in file_offenses'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:121:in `file_offense_cache'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:109:in `file_offenses'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:100:in `process_file'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:78:in `block in each_inspected_file'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:75:in `each'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:75:in `reduce'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:75:in `each_inspected_file'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:67:in `inspect_files'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:39:in `run'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cli.rb:82:in `execute_runner'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cli.rb:28:in `run'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/bin/rubocop:13:in `block in <top (required)>'
/usr/lib/ruby/2.4.0/benchmark.rb:308:in `realtime'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/bin/rubocop:12:in `<top (required)>'
/home/pocke/.gem/ruby/2.4.0/bin/rubocop:22:in `load'
/home/pocke/.gem/ruby/2.4.0/bin/rubocop:22:in `<main>'
.

1 file inspected, no offenses detected
Finished in 0.08389679500032798 seconds

The cause is str_content's Encoding is ASCII-8BIT if encoding: binary is specified.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • All tests are passing.
  • The new code doesn't generate RuboCop offenses.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop).

pocke and others added 2 commits May 29, 2017 20:32
…UTF-8

`Style/WordArray` cop crashes on strings that aren't valid UTF-8, and encoding: binary is specified.

For example

```ruby
 # -*- encoding: binary -*-
["\xC0",
 "\xC2\x4a",
 "\xC2\xC2",
 "\x4a\x82",
 "\x82\x82",
 "\xe1\x82\x4a",
]
```

```bash
$ rubocop -Dd --only WordArray
For /tmp/tmp.GDijgfYoeb: configuration from /home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/config/default.yml
Inheriting configuration from /home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/config/enabled.yml
Inheriting configuration from /home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/config/disabled.yml
Inspecting 1 file
Scanning /tmp/tmp.GDijgfYoeb/test.rb
"\xC0"
true
incompatible encoding regexp match (UTF-8 regexp with ASCII-8BIT string)
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/style/word_array.rb:99:in `=~'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/style/word_array.rb:99:in `!~'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/style/word_array.rb:99:in `block in complex_content?'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/style/word_array.rb:94:in `any?'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/style/word_array.rb:94:in `complex_content?'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/style/word_array.rb:67:in `check_bracketed_array'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/style/word_array.rb:50:in `on_array'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/commissioner.rb:44:in `block (2 levels) in on_array'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/commissioner.rb:106:in `with_cop_error_handling'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/commissioner.rb:43:in `block in on_array'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/commissioner.rb:42:in `each'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/commissioner.rb:42:in `on_array'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/ast/traversal.rb:12:in `walk'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/commissioner.rb:60:in `investigate'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/team.rb:112:in `investigate'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/team.rb:100:in `offenses'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cop/team.rb:42:in `inspect_file'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:258:in `inspect_file'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:205:in `block in do_inspection_loop'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:237:in `block in iterate_until_no_changes'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:230:in `loop'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:230:in `iterate_until_no_changes'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:201:in `do_inspection_loop'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:111:in `block in file_offenses'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:121:in `file_offense_cache'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:109:in `file_offenses'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:100:in `process_file'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:78:in `block in each_inspected_file'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:75:in `each'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:75:in `reduce'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:75:in `each_inspected_file'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:67:in `inspect_files'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/runner.rb:39:in `run'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cli.rb:82:in `execute_runner'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/lib/rubocop/cli.rb:28:in `run'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/bin/rubocop:13:in `block in <top (required)>'
/usr/lib/ruby/2.4.0/benchmark.rb:308:in `realtime'
/home/pocke/.gem/ruby/2.4.0/gems/rubocop-0.49.0/bin/rubocop:12:in `<top (required)>'
/home/pocke/.gem/ruby/2.4.0/bin/rubocop:22:in `load'
/home/pocke/.gem/ruby/2.4.0/bin/rubocop:22:in `<main>'
.

1 file inspected, no offenses detected
Finished in 0.08389679500032798 seconds
```

The cause is `str_content`'s Encoding is ASCII-8BIT if `encoding: binary` is specified.
@bbatsov bbatsov merged commit add9605 into rubocop:master May 29, 2017
@pocke pocke deleted the fix-wordarray branch June 6, 2017 02:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants