use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.
the class NonNullSetToNullCheck method isUndefinedOrNull.
private static boolean isUndefinedOrNull(CheckerContext context, Symbol symbol) {
ProgramState programState = context.getState();
SymbolicValue value = programState.getValue(symbol);
return value == null;
}
use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.
the class ExceptionalYieldChecker method flowsForMethodArguments.
private static Set<Flow> flowsForMethodArguments(ExplodedGraph.Node node, MethodInvocationTree mit, int parameterCausingExceptionIndex) {
ProgramState programState = node.programState;
List<ProgramState.SymbolicValueSymbol> arguments = Lists.reverse(programState.peekValuesAndSymbols(mit.arguments().size()));
SymbolicValue parameterCausingExceptionSV = arguments.get(parameterCausingExceptionIndex).symbolicValue();
Set<SymbolicValue> argSymbolicValues = new LinkedHashSet<>();
Set<Symbol> argSymbols = new LinkedHashSet<>();
arguments.stream().filter(svs -> parameterCausingExceptionSV == svs.symbolicValue() || hasConstraintOtherThanNonNull(svs, programState)).forEach(svs -> {
argSymbolicValues.add(svs.symbolicValue());
Symbol symbol = svs.symbol();
if (symbol != null) {
argSymbols.add(symbol);
}
});
List<Class<? extends Constraint>> domains = domainsFromArguments(programState, argSymbolicValues);
return FlowComputation.flow(node, argSymbolicValues, c -> true, c -> false, domains, argSymbols);
}
use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.
the class NullDereferenceCheck method reportIssue.
private void reportIssue(SymbolicValue currentVal, Tree syntaxNode, ExplodedGraph.Node node) {
String message = "A \"NullPointerException\" could be thrown; ";
if (syntaxNode.is(Tree.Kind.MEMBER_SELECT) && ((MemberSelectExpressionTree) syntaxNode).expression().is(Tree.Kind.METHOD_INVOCATION)) {
message += "\"" + SyntaxTreeNameFinder.getName(syntaxNode) + "()\" can return null.";
} else {
message += "\"" + SyntaxTreeNameFinder.getName(syntaxNode) + "\" is nullable here.";
}
SymbolicValue val = null;
if (!SymbolicValue.NULL_LITERAL.equals(currentVal)) {
val = currentVal;
}
Symbol dereferencedSymbol = dereferencedSymbol(syntaxNode);
Set<Flow> flows = FlowComputation.flow(node, val, Lists.newArrayList(ObjectConstraint.class), dereferencedSymbol).stream().filter(f -> !f.isEmpty()).map(f -> addDereferenceMessage(f, syntaxNode)).collect(Collectors.toSet());
reportIssue(syntaxNode, message, flows);
}
use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.
the class NullDereferenceCheck method setNullConstraint.
private static List<ProgramState> setNullConstraint(CheckerContext context, Tree syntaxNode) {
SymbolicValue val = context.getState().peekValue();
if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION) && isAnnotatedCheckForNull((MethodInvocationTree) syntaxNode)) {
Preconditions.checkNotNull(val);
List<ProgramState> states = new ArrayList<>();
states.addAll(val.setConstraint(context.getState(), ObjectConstraint.NULL));
states.addAll(val.setConstraint(context.getState(), ObjectConstraint.NOT_NULL));
return states;
}
return Lists.newArrayList(context.getState());
}
use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.
the class NullDereferenceCheck method checkPreStatement.
@Override
public ProgramState checkPreStatement(CheckerContext context, Tree syntaxNode) {
if (context.getState().peekValue() == null) {
// stack is empty, nothing to do.
return context.getState();
}
if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree methodInvocation = (MethodInvocationTree) syntaxNode;
Tree methodSelect = methodInvocation.methodSelect();
if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
SymbolicValue dereferencedSV = context.getState().peekValue(methodInvocation.arguments().size());
return checkConstraint(context, methodSelect, dereferencedSV);
}
}
if (syntaxNode.is(Tree.Kind.ARRAY_ACCESS_EXPRESSION)) {
Tree toCheck = ((ArrayAccessExpressionTree) syntaxNode).expression();
SymbolicValue currentVal = context.getState().peekValue(1);
return checkConstraint(context, toCheck, currentVal);
}
if (syntaxNode.is(Tree.Kind.MEMBER_SELECT)) {
return checkMemberSelect(context, (MemberSelectExpressionTree) syntaxNode, context.getState().peekValue());
}
return context.getState();
}
Aggregations