package/inc/ByteGrabber.hxx           |    1 
 package/source/zipapi/ByteGrabber.cxx |  101 ++++++++++++++++++++++++----------
 2 files changed, 75 insertions(+), 27 deletions(-)

New commits:
commit 4d209d61df2aa0bd6af1f9db0525138f04f1ed56
Author:     Noel Grandin <noel.gran...@collabora.co.uk>
AuthorDate: Thu Aug 14 11:42:03 2025 +0200
Commit:     Xisco Fauli <xiscofa...@libreoffice.org>
CommitDate: Thu Aug 14 18:42:44 2025 +0200

    tdf#167936 crash when opening document from UNO pipe
    
    regression from
        commit a6ad198d097fb4a503c8d5831d484ff46721134b
        Author: Noel Grandin <noel.gran...@collabora.co.uk>
        Date:   Sat Aug 17 13:19:54 2024 +0200
        tdf#158556 use more comphelper::ByteReader
    
    Change-Id: I2cc14d0dbb0214dcf54938796eeeebefbd86bd88
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/189577
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk>
    (cherry picked from commit 7e71af5af43da1f7a7cb0bc4d5319f277a67738a)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/189589
    Reviewed-by: Xisco Fauli <xiscofa...@libreoffice.org>

diff --git a/package/inc/ByteGrabber.hxx b/package/inc/ByteGrabber.hxx
index ea7e6867a4bc..85435fd0c753 100644
--- a/package/inc/ByteGrabber.hxx
+++ b/package/inc/ByteGrabber.hxx
@@ -33,6 +33,7 @@ class ByteGrabber final
     css::uno::Reference < css::io::XSeekable > xSeek;
     comphelper::ByteReader* mpByteReader;
     std::array<sal_Int8, 8> maBuffer;
+    css::uno::Sequence < sal_Int8 > aSequence;
 
 public:
     ByteGrabber (css::uno::Reference < css::io::XInputStream > const & 
xIstream);
diff --git a/package/source/zipapi/ByteGrabber.cxx 
b/package/source/zipapi/ByteGrabber.cxx
index 7d7c77a0228f..bc1206986c5c 100644
--- a/package/source/zipapi/ByteGrabber.cxx
+++ b/package/source/zipapi/ByteGrabber.cxx
@@ -39,9 +39,11 @@ using namespace ::com::sun::star;
 ByteGrabber::ByteGrabber(uno::Reference  < io::XInputStream > const & xIstream)
 : xStream(xIstream)
 , xSeek (xIstream, uno::UNO_QUERY )
+, aSequence ( 4 )
 {
+    // The input stream here could be from a remote UNO connection, in which
+    // case it will not implement comphelper::ByteReader.
     mpByteReader = dynamic_cast<comphelper::ByteReader*>(xStream.get());
-    assert(mpByteReader);
 }
 
 ByteGrabber::~ByteGrabber()
@@ -53,7 +55,6 @@ void ByteGrabber::setInputStream (const uno::Reference < 
io::XInputStream >& xNe
     xStream = xNewStream;
     xSeek.set(xNewStream, uno::UNO_QUERY);
     mpByteReader = dynamic_cast<comphelper::ByteReader*>(xStream.get());
-    assert(mpByteReader);
 }
 
 sal_Int32 ByteGrabber::readBytes( sal_Int8* aData,
@@ -89,39 +90,85 @@ sal_Int64 ByteGrabber::getLength(  )
 
 sal_uInt16 ByteGrabber::ReadUInt16()
 {
-    if (mpByteReader->readSomeBytes(maBuffer.data(), 2) != 2)
-        return 0;
-
-    return static_cast <sal_uInt16>
-           ( (maBuffer[0] & 0xFF)
-          | (maBuffer[1] & 0xFF) << 8);
+    if (mpByteReader)
+    {
+        if (mpByteReader->readSomeBytes(maBuffer.data(), 2) != 2)
+            return 0;
+
+        return static_cast <sal_uInt16>
+               ( (maBuffer[0] & 0xFF)
+                 | (maBuffer[1] & 0xFF) << 8);
+    }
+    else
+    {
+        if (xStream->readBytes(aSequence, 2) != 2)
+            return 0;
+
+        const sal_Int8 *pSequence = aSequence.getConstArray();
+        return static_cast <sal_uInt16>
+               ( (pSequence[0] & 0xFF)
+                 | (pSequence[1] & 0xFF) << 8);
+    }
 }
 
 sal_uInt32 ByteGrabber::ReadUInt32()
 {
-    if (mpByteReader->readSomeBytes(maBuffer.data(), 4) != 4)
-        return 0;
-
-    return static_cast < sal_uInt32 >
-            ( (maBuffer[0] & 0xFF)
-          | ( maBuffer[1] & 0xFF ) << 8
-          | ( maBuffer[2] & 0xFF ) << 16
-          | ( maBuffer[3] & 0xFF ) << 24 );
+    if (mpByteReader)
+    {
+        if (mpByteReader->readSomeBytes(maBuffer.data(), 4) != 4)
+            return 0;
+
+        return static_cast < sal_uInt32 >
+                ( (maBuffer[0] & 0xFF)
+              | ( maBuffer[1] & 0xFF ) << 8
+              | ( maBuffer[2] & 0xFF ) << 16
+              | ( maBuffer[3] & 0xFF ) << 24 );
+    }
+    else
+    {
+        if (xStream->readBytes(aSequence, 4) != 4)
+            return 0;
+
+        const sal_Int8 *pSequence = aSequence.getConstArray();
+        return static_cast < sal_uInt32 >
+                ( (pSequence[0] & 0xFF)
+              | ( pSequence[1] & 0xFF ) << 8
+              | ( pSequence[2] & 0xFF ) << 16
+              | ( pSequence[3] & 0xFF ) << 24 );
+    }
 }
 
 sal_uInt64 ByteGrabber::ReadUInt64()
 {
-    if (mpByteReader->readSomeBytes(maBuffer.data(), 8) != 8)
-        return 0;
-
-    return  static_cast<sal_uInt64>(maBuffer[0] & 0xFF)
-          | static_cast<sal_uInt64>(maBuffer[1] & 0xFF) << 8
-          | static_cast<sal_uInt64>(maBuffer[2] & 0xFF) << 16
-          | static_cast<sal_uInt64>(maBuffer[3] & 0xFF) << 24
-          | static_cast<sal_uInt64>(maBuffer[4] & 0xFF) << 32
-          | static_cast<sal_uInt64>(maBuffer[5] & 0xFF) << 40
-          | static_cast<sal_uInt64>(maBuffer[6] & 0xFF) << 48
-          | static_cast<sal_uInt64>(maBuffer[7] & 0xFF) << 56;
+    if (mpByteReader)
+    {
+        if (mpByteReader->readSomeBytes(maBuffer.data(), 8) != 8)
+            return 0;
+
+        return  static_cast<sal_uInt64>(maBuffer[0] & 0xFF)
+              | static_cast<sal_uInt64>(maBuffer[1] & 0xFF) << 8
+              | static_cast<sal_uInt64>(maBuffer[2] & 0xFF) << 16
+              | static_cast<sal_uInt64>(maBuffer[3] & 0xFF) << 24
+              | static_cast<sal_uInt64>(maBuffer[4] & 0xFF) << 32
+              | static_cast<sal_uInt64>(maBuffer[5] & 0xFF) << 40
+              | static_cast<sal_uInt64>(maBuffer[6] & 0xFF) << 48
+              | static_cast<sal_uInt64>(maBuffer[7] & 0xFF) << 56;
+    }
+    else
+    {
+        if (xStream->readBytes(aSequence, 8) != 8)
+            return 0;
+
+        const sal_Int8 *pSequence = aSequence.getConstArray();
+        return  static_cast<sal_uInt64>(pSequence[0] & 0xFF)
+              | static_cast<sal_uInt64>(pSequence[1] & 0xFF) << 8
+              | static_cast<sal_uInt64>(pSequence[2] & 0xFF) << 16
+              | static_cast<sal_uInt64>(pSequence[3] & 0xFF) << 24
+              | static_cast<sal_uInt64>(pSequence[4] & 0xFF) << 32
+              | static_cast<sal_uInt64>(pSequence[5] & 0xFF) << 40
+              | static_cast<sal_uInt64>(pSequence[6] & 0xFF) << 48
+              | static_cast<sal_uInt64>(pSequence[7] & 0xFF) << 56;
+    }
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Reply via email to