Snapchat / djinni

A tool for generating cross-language type declarations and interface bindings. Djinni's new home is in the Snapchat org.
Apache License 2.0
166 stars 44 forks source link

Fix JNI marshalling of zero-length protobuf objects from native->Java #139

Closed kthornberrysc closed 1 year ago

kthornberrysc commented 1 year ago

Protobuf objects that contain only empty or default values take up zero bytes on the wire. In this case the JNI fromCpp() marshaller will try to map a DirectByteBuffer onto a zero-length nullptr C++ buffer, which is forbidden (https://docs.oracle.com/en/java/javase/20/docs/specs/jni/functions.html#newdirectbytebuffer).

On my Android installation with -Xcheck:jni enabled, it will abort with the error "JNI ERROR: non-nullable address is NULL". In other cases the behaviour is undefined.

Fix: check for zero-length C++ buffer and in that case use an empty Java array-backed ByteBuffer to pass to the Java deserializer.

LiFengSC commented 1 year ago

Thanks for the fix!