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

Message toString does not handle nulls #44

Closed xeruf closed 5 years ago

xeruf commented 5 years ago

This is a snippet from the toString method of Message.java:

            for (Object o : largs) {
                if (o instanceof Object[]) {
                    sb.append(Arrays.deepToString((Object[]) o));
                } else if (o instanceof byte[]) {
                    sb.append(Arrays.toString((byte[]) o));
                } else if (o instanceof int[]) {
                    sb.append(Arrays.toString((int[]) o));
                } else if (o instanceof short[]) {
                    sb.append(Arrays.toString((short[]) o));
                } else if (o instanceof long[]) {
                    sb.append(Arrays.toString((long[]) o));
                } else if (o instanceof boolean[]) {
                    sb.append(Arrays.toString((boolean[]) o));
                } else if (o instanceof double[]) {
                    sb.append(Arrays.toString((double[]) o));
                } else if (o instanceof float[]) {
                    sb.append(Arrays.toString((float[]) o));
                } else {
                    sb.append(o.toString());
                }
                sb.append(',');
                sb.append(' ');
            }

When I used 2.7.5, this regularly throwed a NullPointerException for me since o was apparently null in some cases. I haven't had the Exception after upgrading to version 3, but I am unsure whether it is correct to not handle null here. Even if o can never be null you should add a little Objects.requireNonNull or something to catch potential problems immediately.

hypfvieh commented 5 years ago

toString() should never throw anything in my opinion. I've just pushed a fix for that one.