Fix the java.lang.NullPointerException in readBytes() and readSomeBytes()...

methods in InputStreamToXInputStreamAdapter when called from the inter-process UNO bridge.

XInputStream::readBytes() documents how the buffer is an "out" parameter, and isn't passed
to the implementing end, which is why we get the buffer as a "byte[][] b" and b[0] == null.
Its role is to box a byte[] array to be returned the client. Thus, allocate the buffer if
it is missing or too small.

Additionally, virtually all other readBytes() and readSomeBytes() implementations trim this
sequence to the actual number of bytes read. This presumably reduces the inter-process
traffic, but some callers even rely on the sequence to be trimmed, eg.
main/sax/source/expatwrap/xml2utf.cxx. Thus trim our returned array too.

(cherry-picked from 6cb06142790376a2c58e6392182eb071420a4221)

Change-Id: I42eb209b68f7c13a34670d03c2ca61d76672385b
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/147933
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Tested-by: Jenkins
This commit is contained in:
Damjan Jovanovic
2023-02-28 10:05:12 +00:00
committed by Caolán McNamara
parent 4e6ab75c1a
commit 618e8e30a2
@@ -79,6 +79,9 @@ public final class InputStreamToXInputStreamAdapter implements XInputStream {
{
try {
long bytesRead;
if (b[0] == null || b[0].length < len) {
b[0] = new byte[len];
}
if (len >iIn.available()) {
bytesRead = iIn.read(b[0], 0, iIn.available());
}
@@ -89,6 +92,12 @@ public final class InputStreamToXInputStreamAdapter implements XInputStream {
// Casting bytesRead to an int is okay, since the user can
// only pass in an integer length to read, so the bytesRead
// must <= len.
if (bytesRead < b[0].length) {
int outSize = bytesRead > 0 ? (int)bytesRead : 0;
byte[] out = new byte[outSize];
System.arraycopy(b[0], 0, out, 0, outSize);
b[0] = out;
}
if (bytesRead <= 0) {
return 0;
}
@@ -103,6 +112,9 @@ public final class InputStreamToXInputStreamAdapter implements XInputStream {
{
try {
long bytesRead;
if (b[0] == null || b[0].length < len) {
b[0] = new byte[len];
}
if (len >iIn.available()) {
bytesRead = iIn.read(b[0], 0, iIn.available());
}
@@ -113,6 +125,12 @@ public final class InputStreamToXInputStreamAdapter implements XInputStream {
// Casting bytesRead to an int is okay, since the user can
// only pass in an integer length to read, so the bytesRead
// must <= len.
if (bytesRead < b[0].length) {
int outSize = bytesRead > 0 ? (int)bytesRead : 0;
byte[] out = new byte[outSize];
System.arraycopy(b[0], 0, out, 0, outSize);
b[0] = out;
}
if (bytesRead <= 0) {
return 0;
}