ridljar/com/sun/star/lib/uno/adapter/InputStreamToXInputStreamAdapter.java |   
18 ++++++++++
 1 file changed, 18 insertions(+)

New commits:
commit 618e8e30a21efea41b9a4e63219de20e38987f4a
Author:     Damjan Jovanovic <dam...@apache.org>
AuthorDate: Mon Feb 27 20:15:20 2023 +0000
Commit:     Caolán McNamara <caol...@redhat.com>
CommitDate: Tue Feb 28 10:05:12 2023 +0000

    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 <sberg...@redhat.com>
    Tested-by: Jenkins

diff --git 
a/ridljar/com/sun/star/lib/uno/adapter/InputStreamToXInputStreamAdapter.java 
b/ridljar/com/sun/star/lib/uno/adapter/InputStreamToXInputStreamAdapter.java
index a81bb85b1c57..d547b1e7ce17 100644
--- a/ridljar/com/sun/star/lib/uno/adapter/InputStreamToXInputStreamAdapter.java
+++ b/ridljar/com/sun/star/lib/uno/adapter/InputStreamToXInputStreamAdapter.java
@@ -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;
             }

Reply via email to