hypfvieh / dbus-java

Improved version of java DBus library provided by freedesktop.org (https://dbus.freedesktop.org/doc/dbus-java/)
https://hypfvieh.github.io/dbus-java/
MIT License
185 stars 73 forks source link

SpotBugs analysis: Possible NPE dereference in matchesParameters(List) #201

Closed ghost closed 1 year ago

ghost commented 1 year ago

While running SpotBugs on our code, the tool noted:

Possible null pointer dereference There is a branch of statement that, if executed, guarantees that a null value will be dereferenced, which would generate a NullPointerException when the code is executed. Of course, the problem might be that the branch or statement is infeasible and that the null pointer exception cannot ever be executed; deciding that is beyond the ability of SpotBugs.

Bug kind and pattern: NP - NP_NULL_ON_SOME_PATH

Apologies for the screenshot, the tool doesn't allow selecting the text:

dbus-npe

The problem appears to be with the following lines:

 public boolean matchesParameters(List<Class<?>> _wantedArgs) {
            if (parameterTypes != null && _wantedArgs == null) {
                return false;
            }
            if (parameterTypes.size() != _wantedArgs.size()) {
                return false;
            }

The parameterTypes != null is a null guard that can be skipped, resulting in an NPE when _wantedArgs isn't null (due to dereferencing the size at parameterTypes.size()). It may be safer to write this as follows:

 public boolean matchesParameters(List<Class<?>> _wantedArgs) {
    if( parameterTypes == null || _wantedArgs == null ) {
      return false;
    }