Skip to content

Commit c4aab02

Browse files
SONARJAVA-7282 False negative java:S2168: not reported when the outer null check is written !(helper != null) (#6450)
1 parent 5b718f7 commit c4aab02

3 files changed

Lines changed: 87 additions & 9 deletions

File tree

‎java-checks-test-sources/default/src/main/files/non-compiling/checks/DoubleCheckedLockingCheckSample.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,18 @@ public UnknownType unknownType() {
1717
}
1818
}
1919

20+
public UnknownType unknownTypeNegated() {
21+
if (!(unknownType != null)) {
22+
// ^^^^^^^^^^^^^^^^^^^^^^>
23+
synchronized (Helper.class) { // Noncompliant {{Remove this dangerous instance of double-checked locking.}}
24+
// ^^^^^^^^^^^^
25+
if (!(unknownType != null)) {
26+
// ^^^^^^^^^^^^^^^^^^^^^^<
27+
this.unknownType = new UnknownType();
28+
}
29+
}
30+
}
31+
}
32+
2033
class Helper { }
2134
}

‎java-checks-test-sources/default/src/main/java/checks/DoubleCheckedLockingCheckSample.java‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,42 @@ public Helper classicCase() {
1414
return helper;
1515
}
1616

17+
public Helper classicCaseNegated() {
18+
if (!(helper != null))
19+
// ^^^^^^^^^^^^^^^^^>
20+
synchronized (this) { // Noncompliant {{Remove this dangerous instance of double-checked locking.}}
21+
// ^^^^^^^^^^^^
22+
if (helper == null)
23+
// ^^^^^^^^^^^^^^<
24+
helper = new Helper();
25+
}
26+
return helper;
27+
}
28+
29+
public Helper classicCaseInnerNegated() {
30+
if (helper == null)
31+
// ^^^^^^^^^^^^^^>
32+
synchronized (this) { // Noncompliant {{Remove this dangerous instance of double-checked locking.}}
33+
// ^^^^^^^^^^^^
34+
if (!(helper != null))
35+
// ^^^^^^^^^^^^^^^^^<
36+
helper = new Helper();
37+
}
38+
return helper;
39+
}
40+
41+
public Helper classicCaseRepeatedNegations() {
42+
if (((!!(helper == null))))
43+
// ^^^^^^^^^^^^^^^^^^^^^^>
44+
synchronized (this) { // Noncompliant {{Remove this dangerous instance of double-checked locking.}}
45+
// ^^^^^^^^^^^^
46+
if (!(!(!(helper != null))))
47+
// ^^^^^^^^^^^^^^^^^^^^^^^<
48+
helper = new Helper();
49+
}
50+
return helper;
51+
}
52+
1753
public Helper fieldIsNotActuallyInitializedHere() {
1854
if (helper == null)
1955
synchronized (this) { // Compliant - field is not initialized in this method actually
@@ -250,6 +286,24 @@ public synchronized Helper synchronizedClassicCase() {
250286
return helper;
251287
}
252288

289+
public Helper negatedNullEqualityOuter() {
290+
if (!(helper == null))
291+
synchronized (this) { // Compliant
292+
if (helper == null)
293+
helper = new Helper();
294+
}
295+
return helper;
296+
}
297+
298+
public Helper doubleNegatedNotEqualInner() {
299+
if (helper == null)
300+
synchronized (this) {
301+
if (!!(helper != null))
302+
helper = new Helper();
303+
}
304+
return helper;
305+
}
306+
253307
static class Helper { }
254308
}
255309

‎java-checks/src/main/java/org/sonar/java/checks/synchronization/DoubleCheckedLockingCheck.java‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@
1616
*/
1717
package org.sonar.java.checks.synchronization;
1818

19+
import java.util.Arrays;
20+
import java.util.Deque;
21+
import java.util.LinkedList;
22+
import java.util.List;
23+
import java.util.Optional;
24+
import java.util.stream.Stream;
1925
import org.sonar.check.Rule;
26+
import org.sonar.java.model.ExpressionUtils;
2027
import org.sonar.java.model.ModifiersUtils;
2128
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
2229
import org.sonar.plugins.java.api.JavaFileScannerContext;
@@ -34,19 +41,15 @@
3441
import org.sonar.plugins.java.api.tree.SynchronizedStatementTree;
3542
import org.sonar.plugins.java.api.tree.SyntaxToken;
3643
import org.sonar.plugins.java.api.tree.Tree;
37-
38-
import java.util.Arrays;
39-
import java.util.Deque;
40-
import java.util.LinkedList;
41-
import java.util.List;
42-
import java.util.Optional;
43-
import java.util.stream.Stream;
44+
import org.sonar.plugins.java.api.tree.UnaryExpressionTree;
4445

4546
import static org.sonar.plugins.java.api.tree.Tree.Kind.EQUAL_TO;
4647
import static org.sonar.plugins.java.api.tree.Tree.Kind.IDENTIFIER;
4748
import static org.sonar.plugins.java.api.tree.Tree.Kind.IF_STATEMENT;
49+
import static org.sonar.plugins.java.api.tree.Tree.Kind.LOGICAL_COMPLEMENT;
4850
import static org.sonar.plugins.java.api.tree.Tree.Kind.MEMBER_SELECT;
4951
import static org.sonar.plugins.java.api.tree.Tree.Kind.METHOD;
52+
import static org.sonar.plugins.java.api.tree.Tree.Kind.NOT_EQUAL_TO;
5053
import static org.sonar.plugins.java.api.tree.Tree.Kind.NULL_LITERAL;
5154
import static org.sonar.plugins.java.api.tree.Tree.Kind.SYNCHRONIZED_STATEMENT;
5255

@@ -90,10 +93,18 @@ private static Optional<IfFieldEqNull> isIfFieldEqNull(Tree tree) {
9093
return Optional.empty();
9194
}
9295
IfStatementTree ifTree = (IfStatementTree) tree;
93-
if (!ifTree.condition().is(EQUAL_TO)) {
96+
97+
ExpressionTree condition = ExpressionUtils.skipParentheses(ifTree.condition());
98+
boolean negated = false;
99+
while (condition.is(LOGICAL_COMPLEMENT)) {
100+
negated = !negated;
101+
condition = ExpressionUtils.skipParentheses(((UnaryExpressionTree) condition).expression());
102+
}
103+
if (!condition.is(negated ? NOT_EQUAL_TO : EQUAL_TO)) {
94104
return Optional.empty();
95105
}
96-
BinaryExpressionTree eqRelation = (BinaryExpressionTree) ifTree.condition();
106+
107+
BinaryExpressionTree eqRelation = (BinaryExpressionTree) condition;
97108
if (eqRelation.rightOperand().is(NULL_LITERAL)) {
98109
return isField(eqRelation.leftOperand()).map(f -> new IfFieldEqNull(ifTree, f));
99110
}

0 commit comments

Comments
 (0)