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

Report bug SA_FIELD_SELF_ASSIGNMENT in nested classes as well #2161

Merged
merged 6 commits into from Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@ Currently the versioning policy of this project follows [Semantic Versioning v2.
- Bump up logback to `1.4.0`
- Bump up log4j2 binding to `2.18.0`
- Fixed InvalidInputException in Eclipse while bug reporting ([#2134](https://github.com/spotbugs/spotbugs/issues/2134))
- Bug `SA_FIELD_SELF_ASSIGNMENT` is now reported from nested classes as well ([#2142](https://github.com/spotbugs/spotbugs/issues/2142))
- Fixed false positives `EI_EXPOSE_REP` thrown in case of fields initialized by the `of` or `copyOf` method of a `List`, `Map` or `Set` ([#1771](https://github.com/spotbugs/spotbugs/issues/1771))
- Fixed CFGBuilderException thrown when `dup_x2` is used to swap the reference and wide-value (double, long) in the stack ([#2146](https://github.com/spotbugs/spotbugs/pull/2146))

Expand Down
@@ -0,0 +1,38 @@
package edu.umd.cs.findbugs.detect;

import org.junit.Test;

import edu.umd.cs.findbugs.AbstractIntegrationTest;
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcher;
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcherBuilder;

import static edu.umd.cs.findbugs.test.CountMatcher.containsExactly;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.MatcherAssert.assertThat;

public class Issue2142Test extends AbstractIntegrationTest {
@Test
public void test() {
performAnalysis("ghIssues/Issue2142.class",
"ghIssues/Issue2142$Inner.class");
BugInstanceMatcher matcher = new BugInstanceMatcherBuilder()
.bugType("SA_FIELD_SELF_ASSIGNMENT").build();
assertThat(getBugCollection(), containsExactly(2, matcher));

matcher = new BugInstanceMatcherBuilder()
.bugType("SA_FIELD_SELF_ASSIGNMENT")
.inClass("Issue2142")
.inMethod("foo1")
.atLine(6)
.build();
assertThat(getBugCollection(), hasItem(matcher));

matcher = new BugInstanceMatcherBuilder()
.bugType("SA_FIELD_SELF_ASSIGNMENT")
.inClass("Issue2142$Inner")
.inMethod("foo2")
.atLine(10)
.build();
assertThat(getBugCollection(), hasItem(matcher));
}
}
Expand Up @@ -69,6 +69,8 @@ public void visit(Code obj) {

XField possibleOverwrite;

private int parentInstanceLoadFromRegister = -1;

@Override
public void sawOpcode(int seen) {

Expand All @@ -91,8 +93,12 @@ public void sawOpcode(int seen) {
OpcodeStack.Item fourth = stack.getStackItem(3);
XField f2 = third.getXField();
int registerNumber2 = fourth.getRegisterNumber();
if (f2 != null && f2.equals(getXFieldOperand()) && registerNumber2 >= 0
&& registerNumber2 == third.getFieldLoadedFromRegister()
int loadedFromRegister2 = fourth.getFieldLoadedFromRegister();
if (f2 != null && f2.equals(getXFieldOperand())
&& ((registerNumber2 >= 0 && registerNumber2 == third.getFieldLoadedFromRegister())
|| (loadedFromRegister2 >= 0 && third.getUserValue() != null
&& third.getUserValue() instanceof Integer
baloghadamsoftware marked this conversation as resolved.
Show resolved Hide resolved
&& loadedFromRegister2 == (Integer) third.getUserValue()))
baloghadamsoftware marked this conversation as resolved.
Show resolved Hide resolved
&& !third.sameValue(top) && (third.getPC() == -1 || third.getPC() > lastMethodCall)) {
possibleOverwrite = f2;
}
Expand Down Expand Up @@ -161,7 +167,28 @@ public void sawOpcode(int seen) {
default:
break;
}
if (seen == Const.GETFIELD) {
XField f = getXFieldOperand();
OpcodeStack.Item top = stack.getStackItem(0);
XField topF = top.getXField();
if (f == null || topF == null) {
return;
}

if ("this$0".equals(topF.getName())) {
parentInstanceLoadFromRegister = top.getFieldLoadedFromRegister();
}
}
}

@Override
public void afterOpcode(int seen) {
super.afterOpcode(seen);

if (seen == Const.GETFIELD && parentInstanceLoadFromRegister > -1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you replace this magic number -1 with a static final int field?
Better to tell our intention to who is reading our code.

OpcodeStack.Item top = stack.getStackItem(0);
top.setUserValue(parentInstanceLoadFromRegister);
KengoTODA marked this conversation as resolved.
Show resolved Hide resolved
parentInstanceLoadFromRegister = -1;
}
}
}
13 changes: 13 additions & 0 deletions spotbugsTestCases/src/java/ghIssues/Issue2142.java
@@ -0,0 +1,13 @@
package ghIssues;

public class Issue2142 {
int foo;
void foo1() {
foo = foo++; // can report a warning in this line
}
class Inner {
void foo2() {
foo = foo++; // should report a warning in this line
}
}
}