facebook / infer

A static analyzer for Java, C, C++, and Objective-C
http://fbinfer.com/
MIT License
14.94k stars 2.01k forks source link

[Java] False positive for Null Dereference #1642

Open Jiehong opened 2 years ago

Jiehong commented 2 years ago

In java, Infer sometimes fails to see that null has been accounted for, depending on how nullity is checked for.

Information

Logs

error: Null Dereference
  object `futureResult` last assigned on line 53 could be null and is dereferenced at line 83.
  81.       } finally {
  82.         if (Objects.nonNull(futureResult)) {
  83. >         futureResult.cancel(true);
  84.         }
  85.         executor.shutdown();

Example to reproduce

Future variable = null;
if (Objects.nonNull(variable)) {
  variable.cancel(); // Infer thinks variable is null here
}

This seems to stem from Objects.nonNull(): Infer doesn't understand it, and only seem to understand x != null.

Jiehong commented 2 years ago

My guess is that most of null checks/requires functions from https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html are not taken into account by Infer.