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
189 stars 74 forks source link

Exception ArrayIndexOutOfBoundsException during exporting a dbus object #265

Open dkokorev opened 2 months ago

dkokorev commented 2 months ago

Hi, using dbus-java 3.2.0 I got an exception:

java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10
    at org.freedesktop.dbus.Marshalling.recursiveGetDBusType(Marshalling.java:157)
    at org.freedesktop.dbus.Marshalling.recursiveGetDBusType(Marshalling.java:288)
    at org.freedesktop.dbus.Marshalling.recursiveGetDBusType(Marshalling.java:264)
    at org.freedesktop.dbus.Marshalling.recursiveGetDBusType(Marshalling.java:288)
    at org.freedesktop.dbus.Marshalling.recursiveGetDBusType(Marshalling.java:264)
    at org.freedesktop.dbus.Marshalling.recursiveGetDBusType(Marshalling.java:288)
    at org.freedesktop.dbus.Marshalling.recursiveGetDBusType(Marshalling.java:264)
    at org.freedesktop.dbus.Marshalling.recursiveGetDBusType(Marshalling.java:288)
    at org.freedesktop.dbus.Marshalling.recursiveGetDBusType(Marshalling.java:264)
    at org.freedesktop.dbus.Marshalling.recursiveGetDBusType(Marshalling.java:288)
    at org.freedesktop.dbus.Marshalling.recursiveGetDBusType(Marshalling.java:288)
    at org.freedesktop.dbus.Marshalling.getDBusType(Marshalling.java:148)
    at org.freedesktop.dbus.Marshalling.getDBusType(Marshalling.java:133)
    at org.freedesktop.dbus.messages.ExportedObject.getExportedMethods(ExportedObject.java:186)
    at org.freedesktop.dbus.messages.ExportedObject.getExportedMethods(ExportedObject.java:198)
    at org.freedesktop.dbus.messages.ExportedObject.<init>(ExportedObject.java:51)
    at org.freedesktop.dbus.connections.AbstractConnection.exportObject(AbstractConnection.java:269)

It's because of the problem in org.freedesktop.dbus.Marshalling class (the same code up to the latest version of dbus-java):

...
private static String[] recursiveGetDBusType(StringBuffer[] _out, Type _dataType, boolean _basic, int _level) throws DBusException {
        if (_out.length <= _level) {
            StringBuffer[] newout = new StringBuffer[_out.length];
            System.arraycopy(_out, 0, newout, 0, _out.length);
            _out = newout;
        }
...

My solution is something like this:

-            StringBuffer[] newout = new StringBuffer[_out.length];
+            StringBuffer[] newout = new StringBuffer[_level + 10];
hypfvieh commented 2 months ago

When does this happen? Can you provide a sample to reproduce this? Changing code with some random numbers may not be the ultimate solution when the cause of the error is unknown.

dkokorev commented 2 months ago

When does this happen? Can you provide a sample to reproduce this?

This happens when a dbus struct contains more than 10 levels of nesting.

Changing code with some random numbers may not be the ultimate solution when the cause of the error is unknown.

For simplicity I just used the initial allocation size from here:

public static String[] getDBusType(Type _dataType, boolean _basic) throws DBusException {
        return recursiveGetDBusType(new StringBuffer[10], _dataType, _basic, 0);
}
hypfvieh commented 2 months ago

Will be fixed in next release.