Check array lengths before passing them to C
authorMatt Corallo <git@bluematt.me>
Tue, 30 Nov 2021 01:38:04 +0000 (01:38 +0000)
committerMatt Corallo <git@bluematt.me>
Wed, 1 Dec 2021 14:14:13 +0000 (14:14 +0000)
commit490adeaa3273fe53040c11f415782977895d0be7
treeceeced71d02fd9e0121fe23d236d185f8f9af9b4
parent2269044fc355c549600b3f6495dfcbb176480f75
Check array lengths before passing them to C

When users pass a static-length array to C we currently CHECK its
length, asserting only if we are built in debug mode. In
production, we happily call JNI's `GetByteArrayRegion` with the
expected length, ignoring any errors. `GetByteArrayRegion`,
however, "THROWS ArrayIndexOutOfBoundsException: if one of the
indexes in the region is not valid.". While its somewhat unclear
what "THROWS" means in the context of a C API, it seems safe to
assume accessing return values after a "THROWS" condition is
undefined. Thus, we should ensure we check array lengths before
calling into C.

We do this here with a simple wrapper function added to
`org.ldk.util.InternalUtils` which checks an array is the correct
length before returning it.
gen_type_mapping.py
java_strings.py
src/main/java/org/ldk/util/InternalUtils.java [new file with mode: 0644]