Skip to content

Commit

Permalink
SONARJAVA-1996 Fix order of flows on suspicious call
Browse files Browse the repository at this point in the history
  • Loading branch information
Wohops authored and benzonico committed Feb 27, 2017
1 parent e0474af commit 7adda72
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ private void reportIssue(ExplodedGraph.Node node, ExceptionalCheckBasedYield yie
for (List<JavaFileScannerContext.Location> argumentFlow : argumentsFlows) {
for (List<JavaFileScannerContext.Location> exceptionFlow : exceptionFlows) {
flows.add(ImmutableList.<JavaFileScannerContext.Location>builder()
.addAll(Lists.reverse(argumentFlow))
.addAll(exceptionFlow)
.add(methodInvocationMessage)
.addAll(Lists.reverse(exceptionFlow))
.addAll(argumentFlow)
.build());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ void foo1() {
}

void foo2() {
A.divByZeroIfArg1Zero( // Noncompliant [[flows=foo2]] {{A division by zero will occur when invoking method divByZeroIfArg1Zero().}} flow@foo2 [[order=5]] {{'divByZeroIfArg1Zero()' is invoked.}}
42, // flow@foo2 [[order=1]] {{non-null}} flow@foo2 [[order=2]] {{non-zero}}
0); // flow@foo2 [[order=3]] {{non-null}} flow@foo2 [[order=4]] {{zero}}
A.divByZeroIfArg1Zero( // Noncompliant [[flows=foo2]] {{A division by zero will occur when invoking method divByZeroIfArg1Zero().}} flow@foo2 [[order=4]] {{'divByZeroIfArg1Zero()' is invoked.}}
42, // flow@foo2 [[order=8]] {{non-null}} flow@foo2 [[order=7]] {{non-zero}}
0); // flow@foo2 [[order=6]] {{non-null}} flow@foo2 [[order=5]] {{zero}}
}

void foo3(int j) {
int i = 42; // flow@foo3 [[order=1]] {{non-null}} flow@foo3 [[order=2]] {{non-zero}}
int i = 42; // flow@foo3 [[order=8]] {{non-null}} flow@foo3 [[order=7]] {{non-zero}}
divByZeroIfArg1Zero(i, j); // Compliant
if (j == 0) { // flow@foo3 [[order=3]] {{Implies 'j' is non-null.}} flow@foo3 [[order=4]] {{Implies 'j' is zero.}}
divByZeroIfArg1Zero(i, j); // Noncompliant [[flows=foo3]] {{A division by zero will occur when invoking method divByZeroIfArg1Zero().}} flow@foo3 [[order=5]] {{'divByZeroIfArg1Zero()' is invoked.}}
if (j == 0) { // flow@foo3 [[order=6]] {{Implies 'j' is non-null.}} flow@foo3 [[order=5]] {{Implies 'j' is zero.}}
divByZeroIfArg1Zero(i, j); // Noncompliant [[flows=foo3]] {{A division by zero will occur when invoking method divByZeroIfArg1Zero().}} flow@foo3 [[order=4]] {{'divByZeroIfArg1Zero()' is invoked.}}
}
}

Expand Down Expand Up @@ -68,7 +68,7 @@ static int divByZeroIfZero(int i) {
}

static int divByZeroIfArg1Zero(int x, int y) {
return x / y; // flow@foo2 [[order=6]] {{Implies 'y' is zero.}} flow@foo2 [[order=7]] {{Implies 'y' is non-null.}} flow@foo2 [[order=8]] {{'ArithmeticException' is thrown here.}} flow@foo3 [[order=6]] {{Implies 'y' is zero.}} flow@foo3 [[order=7]] {{Implies 'y' is non-null.}} flow@foo3 [[order=8]] {{'ArithmeticException' is thrown here.}}
return x / y; // flow@foo2 [[order=3]] {{Implies 'y' is zero.}} flow@foo2 [[order=2]] {{Implies 'y' is non-null.}} flow@foo2 [[order=1]] {{'ArithmeticException' is thrown here.}} flow@foo3 [[order=3]] {{Implies 'y' is zero.}} flow@foo3 [[order=2]] {{Implies 'y' is non-null.}} flow@foo3 [[order=1]] {{'ArithmeticException' is thrown here.}}
}

static int throwsExceptionIfArg1Zero(int i, int j) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ void foo1(Object o) {
}

void foo2(Object o) {
if (o == null) { // flow@foo2 [[order=1]] {{Implies 'o' is null.}}
A.npeIfNull(o); // Noncompliant [[flows=foo2]] [[sc=9;ec=18]] {{NullPointerException will be thrown when invoking method npeIfNull().}} flow@foo2 [[order=2]] {{'npeIfNull()' is invoked.}}
if (o == null) { // flow@foo2 [[order=4]] {{Implies 'o' is null.}}
A.npeIfNull(o); // Noncompliant [[flows=foo2]] [[sc=9;ec=18]] {{NullPointerException will be thrown when invoking method npeIfNull().}} flow@foo2 [[order=3]] {{'npeIfNull()' is invoked.}}
}

o.toString(); // Compliant - can not be reached with 'o 'being NULL, as the NPE is triggered insde bar()
Expand All @@ -34,36 +34,36 @@ void foo4(Object o) {
}

void foo5(Object o) {
if (o == null) { // flow@foo5 [[order=1]] {{Implies 'o' is null.}}
if (o == null) { // flow@foo5 [[order=3]] {{Implies 'o' is null.}}
nullable(o); // Noncompliant [[flows=foo5]] [[sc=7;ec=15]] {{NullPointerException will be thrown when invoking method nullable().}} flow@foo5 [[order=2]] {{'nullable()' is invoked.}}
}
o.toString(); // Compliant - can not be reached with 'o 'being NULL, as the NPE is triggered insde bar()
}

void foo6(Object o, boolean b1, boolean b2) {
if (!b1 && b2) { // flow@foo6 [[order=1]] {{Implies 'b1' is true.}}
if (!b1 && b2) { // flow@foo6 [[order=5]] {{Implies 'b1' is true.}}
npeIfArg0IsTrueAndArg1IsNull(b1, o, b2); // Compliant
}
if (o == null && b1) { // flow@foo6 [[order=2]] {{Implies 'o' is null.}}
if (o == null && b1) { // flow@foo6 [[order=4]] {{Implies 'o' is null.}}
npeIfArg0IsTrueAndArg1IsNull(b1, o, b2); // Noncompliant [[flows=foo6]] [[sc=7;ec=35]] {{NullPointerException will be thrown when invoking method npeIfArg0IsTrueAndArg1IsNull().}} flow@foo6 [[order=3]] {{'npeIfArg0IsTrueAndArg1IsNull()' is invoked.}}
}
}

static void npeIfNull(Object o) {
o.toString(); // flow@foo2 [[order=3]] {{...}} flow@foo2 [[order=4]] {{'NullPointerException' is thrown here.}}
o.toString(); // flow@foo2 [[order=2]] {{...}} flow@foo2 [[order=1]] {{'NullPointerException' is thrown here.}}
}

static void npeIfArg0IsTrueAndArg1IsNull(boolean arg0, Object arg1, boolean arg2) {
if (arg1 != null) { // flow@foo6 [[order=4]] {{Implies 'arg1' is null.}}
if (arg1 != null) { // flow@foo6 [[order=2]] {{Implies 'arg1' is null.}}
return;
}
if (arg0) { // only b2 has consequences on NPE
arg1.toString(); // Noncompliant {{NullPointerException might be thrown as 'arg1' is nullable here}} flow@foo6 [[order=5]] {{'NullPointerException' is thrown here.}}
arg1.toString(); // Noncompliant {{NullPointerException might be thrown as 'arg1' is nullable here}} flow@foo6 [[order=1]] {{'NullPointerException' is thrown here.}}
}
}

static void nullable(@Nullable Object o) {
o.toString(); // Noncompliant {{NullPointerException might be thrown as 'o' is nullable here}} flow@foo5 [[order=3]] {{'NullPointerException' is thrown here.}}
o.toString(); // Noncompliant {{NullPointerException might be thrown as 'o' is nullable here}} flow@foo5 [[order=1]] {{'NullPointerException' is thrown here.}}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ void foo5() {
}

void foo6(boolean b) {
Optional<String> o = getOptional(); // flow@foo6 [[order=1]] {{'getOptional()' returns non-null.}} flow@foo6 [[order=2]] {{'o' is assigned non-null.}}
if (b) { // flow@foo6 [[order=3]] {{Implies 'b' is true.}}
A.nseeIfCalledAndTrue(b, o); // Noncompliant [[flows=foo6]] {{NoSuchElementException will be thrown when invoking method nseeIfCalledAndTrue() without verifying Optional parameter.}} flow@foo6 [[order=4]] {{'nseeIfCalledAndTrue()' is invoked.}}
Optional<String> o = getOptional(); // flow@foo6 [[order=6]] {{'getOptional()' returns non-null.}} flow@foo6 [[order=5]] {{'o' is assigned non-null.}}
if (b) { // flow@foo6 [[order=4]] {{Implies 'b' is true.}}
A.nseeIfCalledAndTrue(b, o); // Noncompliant [[flows=foo6]] {{NoSuchElementException will be thrown when invoking method nseeIfCalledAndTrue() without verifying Optional parameter.}} flow@foo6 [[order=3]] {{'nseeIfCalledAndTrue()' is invoked.}}
} else {
nseeIfCalledAndTrue(b, o); // Compliant
}
Expand All @@ -62,7 +62,7 @@ static void nseeIfCalled(Optional<String> o) {

static void nseeIfCalledAndTrue(boolean b, Optional<String> o) {
if (b) {
o.get(); // Noncompliant {{Call "o.isPresent()" before accessing the value.}} flow@foo6 [[order=5]] {{...}} flow@foo6 [[order=6]] {{'NoSuchElementException' is thrown here.}}
o.get(); // Noncompliant {{Call "o.isPresent()" before accessing the value.}} flow@foo6 [[order=2]] {{...}} flow@foo6 [[order=1]] {{'NoSuchElementException' is thrown here.}}
} else if (o.isPresent()) {
o.get();
}
Expand Down

0 comments on commit 7adda72

Please sign in to comment.