Skip to content

Commit

Permalink
SONARJAVA-4390 NPE in ECJ should be catched by JType.isSubtype(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
alban-auzeill authored and dorian-burihabwa-sonarsource committed Jan 10, 2023
1 parent 667dfa4 commit 2d18518
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
12 changes: 11 additions & 1 deletion java-frontend/src/main/java/org/sonar/java/model/JType.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package org.sonar.java.model;

import org.eclipse.jdt.core.dom.ITypeBinding;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.semantic.Type;

Expand All @@ -31,6 +33,8 @@

final class JType implements Type, Type.ArrayType {

private static final Logger LOG = Loggers.get(JType.class);

final JSema sema;
final ITypeBinding typeBinding;

Expand Down Expand Up @@ -70,7 +74,13 @@ private static boolean isSubtype(ITypeBinding left, ITypeBinding right) {
if (left.isNullType()) {
return !right.isPrimitive();
}
return left.isSubTypeCompatible(right);
try {
return left.isSubTypeCompatible(right);
} catch (NullPointerException ex) {
// In rare circumstances, ECJ may produce a NPE while calling isSubTypeCompatible(), see SONARJAVA-4390
LOG.debug("NullPointerException while resolving isSubTypeCompatible()", ex);
return false;
}
}

@Override
Expand Down
30 changes: 27 additions & 3 deletions java-frontend/src/test/java/org/sonar/java/model/JTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@
*/
package org.sonar.java.model;

import java.util.Objects;
import java.util.stream.Stream;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.sonar.api.utils.log.LogTesterJUnit5;
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.java.model.JavaTree.CompilationUnitTreeImpl;
import org.sonar.java.model.declaration.ClassTreeImpl;
import org.sonar.java.model.declaration.MethodTreeImpl;
Expand All @@ -35,15 +40,18 @@
import org.sonar.java.model.statement.ReturnStatementTreeImpl;
import org.sonar.plugins.java.api.semantic.Type;

import java.util.Objects;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import static org.sonar.java.model.assertions.TypeAssert.assertThat;

class JTypeTest {

@RegisterExtension
public LogTesterJUnit5 logTester = new LogTesterJUnit5();

@Test
void isArray() {
assertThat(type("int[]").isArray())
Expand Down Expand Up @@ -258,6 +266,22 @@ void null_type() {
.isNotSubtypeOf(primitiveType);
}

@Test
void is_subtype_of_should_not_throw_NPE() {
JType objectType = type("java.lang.Object");
ITypeBinding brokenStringBinding = spy(Objects.requireNonNull(sema.resolveType("java.lang.String")));
// simulate the NullPointerException described in SONARJAVA-4390
when(brokenStringBinding.isSubTypeCompatible(any()))
.thenThrow(new NullPointerException("test NPE"));

JType stringType = new JType(sema, brokenStringBinding);
// should catch the NullPointerException
assertThat(stringType.isSubtypeOf(objectType)).isFalse();

assertThat(logTester.logs(LoggerLevel.DEBUG))
.containsExactly("NullPointerException while resolving isSubTypeCompatible()");
}

@Test
void wildcard() {
JavaTree.CompilationUnitTreeImpl cu = test("class C<T1, T2, T3> { C<? extends String, ? extends String, ? super String> f; }");
Expand Down

0 comments on commit 2d18518

Please sign in to comment.