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

SA_LOCAL_SELF_COMPUTATION bug #1472

Closed
gloNelson opened this issue Mar 24, 2021 · 3 comments
Closed

SA_LOCAL_SELF_COMPUTATION bug #1472

gloNelson opened this issue Mar 24, 2021 · 3 comments

Comments

@gloNelson
Copy link
Contributor

gloNelson commented Mar 24, 2021

Hi,
I read SA_LOCAL_SELF_COMPUTATION on SpotBugs Bug Description page, which mentions that this method performs a nonsensical computation of a local variable with another reference to the same variable (e.g., x&x or x-x).
So, I wrote test cases as below.

int testSA_LOCAL_SELF_COMPUTATION(){
      int flags = 1;
      return flags ^ flags; // warning SA_LOCAL_SELF_COMPUTATION
  }

  int testSA_LOCAL_SELF_COMPUTATION1(){
      int flags = 1;
      return flags & flags; // no warning (False negative)
  }

  int testSA_LOCAL_SELF_COMPUTATION2(){
      int flags = 1;
      return flags - flags; // no warning (False negative)
  }

  int testSA_LOCAL_SELF_COMPUTATION3(){
      int flags = 1;
      return flags | flags; // no warning (False negative)
  }

It can be seen from the results that only ^ will trigger SpotBugs to report SA_LOCAL_SELF_COMPUTATION, while other operators will not.

I tried to search test cases of SA_LOCAL_SELF_COMPUTATION in SpotBugs source code and found several test cases of SA_FIELD_SELF_COMPUTATION which detect self computation of field instead of local variables.
Similar test-cases for SA_FIELD_SELF_COMPUTATION executed and all four output SA_FIELD_SELF_COMPUTATION errors report.

int x = 1;
  int testSA_FIELD_SELF_COMPUTATION(){
      return x ^ x; // warning SA_FIELD_SELF_COMPUTATION
  }

  int testSA_FIELD_SELF_COMPUTATION1(){
      return x & x; // warning SA_FIELD_SELF_COMPUTATION
  }

  int testSA_FIELD_SELF_COMPUTATION2(){
      return x - x; // warning SA_FIELD_SELF_COMPUTATION
  }

  int testSA_FIELD_SELF_COMPUTATION3(){
      return x | x; // warning SA_FIELD_SELF_COMPUTATION
  }

Finally, I realized that the fault is in line 325 of FindSelfComparison.java.

else if (opCode == Const.IXOR && item0.sameValue(item1)) {

The code shows that SpotBugs reports SA_FIELD_SELF_COMPUTATION only when opCode equals Const.IXOR(^).

It is so confusing that self computation detection of local variables only including ^ operator while the detection of field supports much more. Is it a bug?

@gloNelson gloNelson changed the title SA_LOCAL_SELF_COMPUTATION bug and potential false negative SA_LOCAL_SELF_COMPUTATION bug Mar 24, 2021
@gloNelson
Copy link
Contributor Author

Another problematic code section in FindSelfComparison.java is mentioned in #1473 (False negatives on SA_FIELD_SELF_COMPUTATION and SA_LOCAL_SELF_COMPUTATION) in detail.

Thanks a lot. @KengoTODA

@gloNelson
Copy link
Contributor Author

I have created a pull request to fix this. Please help me review this :) @KengoTODA Thks!

@gtoison
Copy link
Contributor

gtoison commented Aug 19, 2023

Fixed by #1478, thank you!

@gtoison gtoison closed this as completed Aug 19, 2023
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 a pull request may close this issue.

2 participants