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/MultipleComparison generates invalid code #9520

Closed
adfoster-r7 opened this issue Feb 16, 2021 · 0 comments · Fixed by #9521
Closed

Style/MultipleComparison generates invalid code #9520

adfoster-r7 opened this issue Feb 16, 2021 · 0 comments · Fixed by #9521
Labels

Comments

@adfoster-r7
Copy link

adfoster-r7 commented Feb 16, 2021

Input

When running this file in its entirety, the following code in particular breaks:

  def self.key2str(key)
    if (key == 'HKLM' or key == 'HKEY_LOCAL_MACHINE')
      return HKEY_LOCAL_MACHINE
    elsif (key == 'HKCU' or key == 'HKEY_CURRENT_USER')
      return HKEY_CURRENT_USER
    elsif (key == 'HKU' or key == 'HKEY_USERS')
      return HKEY_USERS
    elsif (key == 'HKCR' or key == 'HKEY_CLASSES_ROOT')
      return HKEY_CLASSES_ROOT
    elsif (key == 'HKEY_CURRENT_CONFIG')
      return HKEY_CURRENT_CONFIG
    elsif (key == 'HKEY_PERFORMANCE_DATA')
      return HKEY_PERFORMANCE_DATA
    elsif (key == 'HKEY_DYN_DATA')
      return HKEY_DYN_DATA
    else
      raise ArgumentError, "Unknown key: #{key}"
    end
  end

Output

The output is completely different:

  def self.key2str(key)
    if ([REG_SZ, REG_MULTI_SZ, REG_SZ, REG_MULTI_SZ, REG_SZ, REG_MULTI_SZ, REG_SZ, REG_MULTI_SZ, 'HKLM', 'HKEY_LOCAL_MACHINE'].include?(key))
      return HKEY_LOCAL_MACHINE
    elsif ([REG_SZ, REG_MULTI_SZ, REG_SZ, REG_MULTI_SZ, REG_SZ, REG_MULTI_SZ, REG_SZ, REG_MULTI_SZ, 'HKLM', 'HKEY_LOCAL_MACHINE', 'HKLM', 'HKEY_LOCAL_MACHINE', 'HKCU', 'HKEY_CURRENT_USER'].include?(key))
      return HKEY_CURRENT_USER
    elsif ([REG_SZ, REG_MULTI_SZ, REG_SZ, REG_MULTI_SZ, REG_SZ, REG_MULTI_SZ, REG_SZ, REG_MULTI_SZ, 'HKLM', 'HKEY_LOCAL_MACHINE', 'HKLM', 'HKEY_LOCAL_MACHINE', 'HKCU', 'HKEY_CURRENT_USER', 'HKCU', 'HKEY_CURRENT_USER', 'HKU', 'HKEY_USERS'].include?(key))
      return HKEY_USERS
    elsif ([REG_SZ, REG_MULTI_SZ, REG_SZ, REG_MULTI_SZ, REG_SZ, REG_MULTI_SZ, REG_SZ, REG_MULTI_SZ, 'HKLM', 'HKEY_LOCAL_MACHINE', 'HKLM', 'HKEY_LOCAL_MACHINE', 'HKCU', 'HKEY_CURRENT_USER', 'HKCU', 'HKEY_CURRENT_USER', 'HKU', 'HKEY_USERS', 'HKU', 'HKEY_USERS', 'HKCR', 'HKEY_CLASSES_ROOT'].include?(key))
      return HKEY_CLASSES_ROOT
    elsif (key == 'HKEY_CURRENT_CONFIG')
      return HKEY_CURRENT_CONFIG
    elsif (key == 'HKEY_PERFORMANCE_DATA')
      return HKEY_PERFORMANCE_DATA
    elsif (key == 'HKEY_DYN_DATA')
      return HKEY_DYN_DATA
    else
      raise ArgumentError, "Unknown key: #{key}"
    end
  end

Steps to reproduce the problem

Run Style/MultipleComparison on this file:

rubocop -a --only Style/MultipleComparison lib/rex/post/meterpreter/extensions/stdapi/sys/registry.rb

RuboCop version

Include the output of rubocop -V or bundle exec rubocop -V if using Bundler.
If you see extension cop versions (e.g. rubocop-performance, rubocop-rspec, and others)
output by rubocop -V, include them as well. Here's an example:

$ [bundle exec] rubocop -V
1.9.1 (using Parser 3.0.0.0, rubocop-ast 1.4.1, running on ruby 2.7.2 x86_64-darwin19)
@koic koic added the bug label Feb 16, 2021
koic added a commit to koic/rubocop that referenced this issue Feb 16, 2021
…omparison`

Fixes rubocop#9520.

This PR fixes an incorrect auto-correct for `Style/MultipleComparison`
when comparing a variable with multiple items in `if` and `elsif` conditions.
bbatsov pushed a commit that referenced this issue Feb 19, 2021
Fixes #9520.

This PR fixes an incorrect auto-correct for `Style/MultipleComparison`
when comparing a variable with multiple items in `if` and `elsif` conditions.
This was referenced Mar 6, 2021
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