scripting/java/com/sun/star/script/framework/io/XInputStreamImpl.java |   12 
+++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

New commits:
commit d0159892793860e53ddc068b4bdb707a521def19
Author:     Damjan Jovanovic <dam...@apache.org>
AuthorDate: Sun Oct 16 18:31:15 2022 +0200
Commit:     Xisco Fauli <xiscofa...@libreoffice.org>
CommitDate: Mon Mar 20 14:43:13 2023 +0000

    Fix some errors in the scripting module's XInputStreamImpl:
    
    - Check the loop termination in readBytes() properly: currently it 
increments
    totalBytesRead while also decrementing nBytesToRead, so when compared to
    each other, the loop terminates when the buffer is half full. Only check
    for nBytesToRead instead.
    - Deal with the possibility of available() returning 0 in readSomeBytes().
    
    Patch by: me
    
    Cherry-picked from
    
https://github.com/apache/openoffice/commit/7e29bacc90c4b1b9788c3b71dfacd17daecde7a7
    "Fix some errors in the scripting module's XInputStreamImpl:"
    
    Change-Id: I951dc10565afa3519b0ddf98de559a7b585b1627
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/149156
    Reviewed-by: Stephan Bergmann <sberg...@redhat.com>
    Tested-by: Jenkins

diff --git 
a/scripting/java/com/sun/star/script/framework/io/XInputStreamImpl.java 
b/scripting/java/com/sun/star/script/framework/io/XInputStreamImpl.java
index 8320b6227cdb..3270d40d5bfb 100644
--- a/scripting/java/com/sun/star/script/framework/io/XInputStreamImpl.java
+++ b/scripting/java/com/sun/star/script/framework/io/XInputStreamImpl.java
@@ -41,11 +41,16 @@ public class XInputStreamImpl implements XInputStream {
         try {
             int bytesRead;
 
-            while ((bytesRead = is.read(aData[ 0 ], totalBytesRead, 
nBytesToRead)) > 0
-                   && (totalBytesRead < nBytesToRead)) {
+            while (( nBytesToRead > 0 ) && (bytesRead = is.read(aData[ 0 ], 
totalBytesRead, nBytesToRead)) > 0) {
                 totalBytesRead += bytesRead;
                 nBytesToRead -= bytesRead;
             }
+            if ( totalBytesRead < aData[ 0 ].length )
+            {
+                byte[] out = new byte[ totalBytesRead ];
+                System.arraycopy( aData[ 0 ], 0, out, 0, totalBytesRead );
+                aData[ 0 ] = out;
+            }
         } catch (IOException e) {
             throw new com.sun.star.io.IOException(e);
         } catch (IndexOutOfBoundsException aie) {
@@ -62,7 +67,8 @@ public class XInputStreamImpl implements XInputStream {
         int bytesToRead = nMaxBytesToRead;
         int availableBytes = available();
 
-        if (availableBytes < nMaxBytesToRead) {
+        if (0 < availableBytes && availableBytes < nMaxBytesToRead)
+        {
             bytesToRead = availableBytes;
         }
 

Reply via email to