Skip to content

Commit

Permalink
SONARJAVA-4233 Constants accessed with member-select create different…
Browse files Browse the repository at this point in the history
… SV every time when placed in loops
  • Loading branch information
alban-auzeill authored and dorian-burihabwa-sonarsource committed May 25, 2023
1 parent c071a14 commit a630731
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package symbolicexecution.checks;

import java.util.List;
import javax.annotation.Nullable;

public abstract class NullDereferenceCheck_constants_in_loop {

void foo(List<Object> items) {
Integer max = null;
for (Object item : items) {
Integer value = getValue(item);
if (value == null) {
value = Integer.MAX_VALUE; // Should be a symbolic value from ConstraintManager#constants
}
if (max == null) {
max = value;
} else if (value == max) {
max = value;
} else if (value.equals(max)) { // Noncompliant {{A "NullPointerException" could be thrown; "value" is nullable here.}}
doSomething();
}
}
}

void doSomething() { }

@Nullable
abstract Integer getValue(Object o);

}
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,8 @@ private void executeMemberSelect(MemberSelectExpressionTree mse) {
programState = unstackMSE.state;
}

if (ExpressionUtils.isSelectOnThisOrSuper(mse)) {
Symbol symbol = mse.identifier().symbol();
if (ExpressionUtils.isSelectOnThisOrSuper(mse) || (symbol.isStatic() && symbol.isFinal())) {
executeIdentifier(mse.identifier());
} else {
SymbolicValue mseValue = constraintManager.createSymbolicValue(mse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ void test_unboxing_without_binaries() {
.verifyIssues();
}

@Test
void test_constants_in_loop() {
SECheckVerifier.newVerifier()
.onFile(mainCodeSourcesPath("symbolicexecution/checks/NullDereferenceCheck_constants_in_loop.java"))
.withCheck(new NullDereferenceCheck())
.verifyIssues();
}

@Test
void test() {
SECheckVerifier.newVerifier()
Expand Down

0 comments on commit a630731

Please sign in to comment.