package/inc/ZipOutputEntry.hxx                 |   13 ++++++---
 package/inc/ZipOutputStream.hxx                |    6 ++--
 package/source/zipapi/ZipOutputEntry.cxx       |   22 +++++++++-------
 package/source/zipapi/ZipOutputStream.cxx      |   33 ++++++++++++-------------
 package/source/zippackage/ZipPackage.cxx       |   24 +++++++++---------
 package/source/zippackage/ZipPackageFolder.cxx |    6 ++--
 package/source/zippackage/ZipPackageStream.cxx |   21 ++++++---------
 7 files changed, 64 insertions(+), 61 deletions(-)

New commits:
commit 84f6d117808702498dd76f3de175652b04a9b36c
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Sat Sep 7 18:28:22 2024 +0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Sat Sep 7 17:11:48 2024 +0200

    Use unique_ptr to pass ownership of ZipEntry
    
    Change-Id: I09ffa752d2cd0367579e9384b992215b2d79b251
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/172963
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/package/inc/ZipOutputEntry.hxx b/package/inc/ZipOutputEntry.hxx
index 94eb988d8b23..f5649f194d9f 100644
--- a/package/inc/ZipOutputEntry.hxx
+++ b/package/inc/ZipOutputEntry.hxx
@@ -67,7 +67,7 @@ protected:
     ZipOutputEntryBase(
         css::uno::Reference< css::io::XOutputStream > xOutStream,
         css::uno::Reference< css::uno::XComponentContext > xContext,
-        ZipEntry& rEntry, ZipPackageStream* pStream, bool bEncrypt, bool 
checkStream);
+        ZipEntry* pEntry, ZipPackageStream* pStream, bool bEncrypt, bool 
checkStream);
 
     // Inherited classes call this with deflated data buffer.
     void processDeflated( const css::uno::Sequence< sal_Int8 >& deflateBuffer, 
sal_Int32 nLength );
@@ -91,7 +91,7 @@ public:
     ZipOutputEntry(
         const css::uno::Reference< css::io::XOutputStream >& rxOutStream,
         const css::uno::Reference< css::uno::XComponentContext >& rxContext,
-        ZipEntry& rEntry, ZipPackageStream* pStream, bool bEncrypt);
+        ZipEntry* pEntry, ZipPackageStream* pStream, bool bEncrypt);
     void writeStream(const css::uno::Reference< css::io::XInputStream >& 
xInStream) override;
     void write(const css::uno::Sequence< sal_Int8 >& rBuffer);
 
@@ -99,7 +99,7 @@ protected:
     ZipOutputEntry(
         const css::uno::Reference< css::io::XOutputStream >& rxOutStream,
         const css::uno::Reference< css::uno::XComponentContext >& rxContext,
-        ZipEntry& rEntry, ZipPackageStream* pStream, bool bEncrypt, bool 
checkStream);
+        ZipEntry* pEntry, ZipPackageStream* pStream, bool bEncrypt, bool 
checkStream);
     virtual void finishDeflater() override;
     virtual sal_Int64 getDeflaterTotalIn() const override;
     virtual sal_Int64 getDeflaterTotalOut() const override;
@@ -112,6 +112,7 @@ protected:
 class ZipOutputEntryInThread final : public ZipOutputEntry
 {
     class Task;
+    std::unique_ptr<ZipEntry> m_pOwnedZipEntry;
     rtl::Reference<utl::TempFileFastService> m_xTempFile;
     std::exception_ptr m_aParallelDeflateException;
     std::atomic<bool>   m_bFinished;
@@ -119,7 +120,7 @@ class ZipOutputEntryInThread final : public ZipOutputEntry
 public:
     ZipOutputEntryInThread(
         const css::uno::Reference< css::uno::XComponentContext >& rxContext,
-        ZipEntry& rEntry, ZipPackageStream* pStream, bool bEncrypt);
+        std::unique_ptr<ZipEntry>&& pEntry, ZipPackageStream* pStream, bool 
bEncrypt);
     std::unique_ptr<comphelper::ThreadTask> createTask(
         const std::shared_ptr<comphelper::ThreadTaskTag>& pTag,
         const css::uno::Reference< css::io::XInputStream >& xInStream );
@@ -132,6 +133,8 @@ public:
     void closeBufferFile();
     void deleteBufferFile();
     bool isFinished() const { return m_bFinished; }
+    std::unique_ptr<ZipEntry>&& moveZipEntry() { return 
std::move(m_pOwnedZipEntry); }
+
 private:
     void setFinished() { m_bFinished = true; }
 };
@@ -146,7 +149,7 @@ public:
     ZipOutputEntryParallel(
         const css::uno::Reference< css::io::XOutputStream >& rxOutStream,
         const css::uno::Reference< css::uno::XComponentContext >& rxContext,
-        ZipEntry& rEntry, ZipPackageStream* pStream, bool bEncrypt);
+        ZipEntry* pEntry, ZipPackageStream* pStream, bool bEncrypt);
     void writeStream(const css::uno::Reference< css::io::XInputStream >& 
xInStream) override;
 private:
     virtual void finishDeflater() override;
diff --git a/package/inc/ZipOutputStream.hxx b/package/inc/ZipOutputStream.hxx
index d92f140f70f2..5b04a0e41dc8 100644
--- a/package/inc/ZipOutputStream.hxx
+++ b/package/inc/ZipOutputStream.hxx
@@ -37,7 +37,7 @@ class ZipPackageStream;
 class ZipOutputStream
 {
     css::uno::Reference< css::io::XOutputStream > m_xStream;
-    ::std::vector < ZipEntry * > m_aZipList;
+    std::vector<std::unique_ptr<ZipEntry>> m_aZipList;
     std::shared_ptr<comphelper::ThreadTaskTag> mpThreadTaskTag;
 
     ByteChucker         m_aChucker;
@@ -54,7 +54,7 @@ public:
 
     /// @throws css::io::IOException
     /// @throws css::uno::RuntimeException
-    void writeLOC( ZipEntry *pEntry, bool bEncrypt = false );
+    void writeLOC(std::unique_ptr<ZipEntry>&& pEntry, bool bEncrypt = false);
     /// @throws css::io::IOException
     /// @throws css::uno::RuntimeException
     void rawWrite( const css::uno::Sequence< sal_Int8 >& rBuffer );
@@ -68,7 +68,7 @@ public:
     const css::uno::Reference< css::io::XOutputStream >& getStream() const;
 
     static sal_uInt32 getCurrentDosTime();
-    static void setEntry( ZipEntry *pEntry );
+    static void setEntry(ZipEntry& rEntry);
 
 private:
     /// @throws css::io::IOException
diff --git a/package/source/zipapi/ZipOutputEntry.cxx 
b/package/source/zipapi/ZipOutputEntry.cxx
index 9d9c1e9b6f4c..d2180ef0d496 100644
--- a/package/source/zipapi/ZipOutputEntry.cxx
+++ b/package/source/zipapi/ZipOutputEntry.cxx
@@ -44,17 +44,18 @@ using namespace com::sun::star::packages::zip::ZipConstants;
 ZipOutputEntryBase::ZipOutputEntryBase(
         css::uno::Reference< css::io::XOutputStream > xOutput,
         uno::Reference< uno::XComponentContext > xContext,
-        ZipEntry& rEntry,
+        ZipEntry* pEntry,
         ZipPackageStream* pStream,
         bool bEncrypt,
         bool checkStream)
 : m_xContext(std::move(xContext))
 , m_xOutStream(std::move(xOutput))
-, m_pCurrentEntry(&rEntry)
+, m_pCurrentEntry(pEntry)
 , m_nDigested(0)
 , m_pCurrentStream(pStream)
 , m_bEncryptCurrentEntry(bEncrypt)
 {
+    assert(pEntry);
     assert(m_pCurrentEntry->nMethod == DEFLATED && "Use 
ZipPackageStream::rawWrite() for STORED entries");
     (void)checkStream;
     assert(!checkStream || m_xOutStream.is());
@@ -176,11 +177,11 @@ void ZipOutputEntryBase::processInput( const 
uno::Sequence< sal_Int8 >& rBuffer
 ZipOutputEntry::ZipOutputEntry(
         const css::uno::Reference< css::io::XOutputStream >& rxOutput,
         const uno::Reference< uno::XComponentContext >& rxContext,
-        ZipEntry& rEntry,
+        ZipEntry* pEntry,
         ZipPackageStream* pStream,
         bool bEncrypt,
         bool checkStream)
-: ZipOutputEntryBase(rxOutput, rxContext, rEntry, pStream, bEncrypt, 
checkStream)
+: ZipOutputEntryBase(rxOutput, rxContext, pEntry, pStream, bEncrypt, 
checkStream)
 , m_aDeflateBuffer(n_ConstBufferSize)
 , m_aDeflater(DEFAULT_COMPRESSION, true)
 {
@@ -189,10 +190,10 @@ ZipOutputEntry::ZipOutputEntry(
 ZipOutputEntry::ZipOutputEntry(
         const css::uno::Reference< css::io::XOutputStream >& rxOutput,
         const uno::Reference< uno::XComponentContext >& rxContext,
-        ZipEntry& rEntry,
+        ZipEntry* pEntry,
         ZipPackageStream* pStream,
         bool bEncrypt)
-: ZipOutputEntry( rxOutput, rxContext, rEntry, pStream, bEncrypt, true)
+: ZipOutputEntry( rxOutput, rxContext, pEntry, pStream, bEncrypt, true)
 {
 }
 
@@ -243,10 +244,11 @@ bool ZipOutputEntry::isDeflaterFinished() const
 
 ZipOutputEntryInThread::ZipOutputEntryInThread(
         const uno::Reference< uno::XComponentContext >& rxContext,
-        ZipEntry& rEntry,
+        std::unique_ptr<ZipEntry>&& pEntry,
         ZipPackageStream* pStream,
         bool bEncrypt)
-: ZipOutputEntry( uno::Reference< css::io::XOutputStream >(), rxContext, 
rEntry, pStream, bEncrypt, false )
+: ZipOutputEntry( uno::Reference< css::io::XOutputStream >(), rxContext, 
pEntry.get(), pStream, bEncrypt, false )
+, m_pOwnedZipEntry(std::move(pEntry))
 , m_bFinished(false)
 {
 }
@@ -345,10 +347,10 @@ void ZipOutputEntry::writeStream(const uno::Reference< 
io::XInputStream >& xInSt
 ZipOutputEntryParallel::ZipOutputEntryParallel(
         const css::uno::Reference< css::io::XOutputStream >& rxOutput,
         const uno::Reference< uno::XComponentContext >& rxContext,
-        ZipEntry& rEntry,
+        ZipEntry* pEntry,
         ZipPackageStream* pStream,
         bool bEncrypt)
-: ZipOutputEntryBase(rxOutput, rxContext, rEntry, pStream, bEncrypt, true)
+: ZipOutputEntryBase(rxOutput, rxContext, pEntry, pStream, bEncrypt, true)
 , totalIn(0)
 , totalOut(0)
 , finished(false)
diff --git a/package/source/zipapi/ZipOutputStream.cxx 
b/package/source/zipapi/ZipOutputStream.cxx
index 1bbf106d62b5..5f9a8e405427 100644
--- a/package/source/zipapi/ZipOutputStream.cxx
+++ b/package/source/zipapi/ZipOutputStream.cxx
@@ -53,19 +53,19 @@ ZipOutputStream::~ZipOutputStream()
 {
 }
 
-void ZipOutputStream::setEntry( ZipEntry *pEntry )
+void ZipOutputStream::setEntry(ZipEntry& rEntry)
 {
-    if (pEntry->nTime == -1)
-        pEntry->nTime = getCurrentDosTime();
-    if (pEntry->nMethod == -1)
-        pEntry->nMethod = DEFLATED;
-    pEntry->nVersion = 20;
-    pEntry->nFlag = 1 << 11;
-    if (pEntry->nSize == -1 || pEntry->nCompressedSize == -1 ||
-        pEntry->nCrc == -1)
+    if (rEntry.nTime == -1)
+        rEntry.nTime = getCurrentDosTime();
+    if (rEntry.nMethod == -1)
+        rEntry.nMethod = DEFLATED;
+    rEntry.nVersion = 20;
+    rEntry.nFlag = 1 << 11;
+    if (rEntry.nSize == -1 || rEntry.nCompressedSize == -1 ||
+        rEntry.nCrc == -1)
     {
-        pEntry->nSize = pEntry->nCompressedSize = 0;
-        pEntry->nFlag |= 8;
+        rEntry.nSize = rEntry.nCompressedSize = 0;
+        rEntry.nFlag |= 8;
     }
 }
 
@@ -103,7 +103,7 @@ void 
ZipOutputStream::consumeScheduledThreadTaskEntry(std::unique_ptr<ZipOutputE
         return;
     }
 
-    writeLOC(pCandidate->getZipEntry(), pCandidate->isEncrypt());
+    writeLOC(pCandidate->moveZipEntry(), pCandidate->isEncrypt());
 
     sal_Int32 nRead;
     uno::Sequence< sal_Int8 > aSequence(n_ConstBufferSize);
@@ -174,10 +174,9 @@ void ZipOutputStream::finish()
     }
 
     sal_Int32 nOffset= static_cast < sal_Int32 > (m_aChucker.GetPosition());
-    for (ZipEntry* p : m_aZipList)
+    for (auto& p : m_aZipList)
     {
         writeCEN( *p );
-        delete p;
     }
     writeEND( nOffset, static_cast < sal_Int32 > (m_aChucker.GetPosition()) - 
nOffset);
     m_aZipList.clear();
@@ -285,11 +284,11 @@ void ZipOutputStream::writeExtraFields(const ZipEntry& 
rEntry)
     m_aChucker.WriteInt32( 0 );  //Number of the disk on which this file starts
 }
 
-void ZipOutputStream::writeLOC( ZipEntry *pEntry, bool bEncrypt )
+void ZipOutputStream::writeLOC(std::unique_ptr<ZipEntry>&& pEntry, bool 
bEncrypt)
 {
     assert(!m_pCurrentEntry && "Forgot to close an entry with 
rawCloseEntry()?");
-    m_pCurrentEntry = pEntry;
-    m_aZipList.push_back( m_pCurrentEntry );
+    m_aZipList.push_back(std::move(pEntry));
+    m_pCurrentEntry = m_aZipList.back().get();
     const ZipEntry &rEntry = *m_pCurrentEntry;
 
     if ( !::comphelper::OStorageHelper::IsValidZipEntryFileName( rEntry.sPath, 
true ) )
diff --git a/package/source/zippackage/ZipPackage.cxx 
b/package/source/zippackage/ZipPackage.cxx
index 0b6b1ee0503f..d78e6d1cc78d 100644
--- a/package/source/zippackage/ZipPackage.cxx
+++ b/package/source/zippackage/ZipPackage.cxx
@@ -1122,7 +1122,7 @@ void ZipPackage::WriteMimetypeMagicFile( ZipOutputStream& 
aZipOut )
     if ( m_xRootFolder->hasByName( sMime ) )
         m_xRootFolder->removeByName( sMime );
 
-    ZipEntry * pEntry = new ZipEntry;
+    auto pEntry = std::make_unique<ZipEntry>();
     sal_Int32 nBufferLength = m_xRootFolder->GetMediaType().getLength();
     OString sMediaType = OUStringToOString( m_xRootFolder->GetMediaType(), 
RTL_TEXTENCODING_ASCII_US );
     const uno::Sequence< sal_Int8 > aType( reinterpret_cast<sal_Int8 const 
*>(sMediaType.getStr()),
@@ -1139,8 +1139,8 @@ void ZipPackage::WriteMimetypeMagicFile( ZipOutputStream& 
aZipOut )
 
     try
     {
-        ZipOutputStream::setEntry(pEntry);
-        aZipOut.writeLOC(pEntry);
+        ZipOutputStream::setEntry(*pEntry);
+        aZipOut.writeLOC(std::move(pEntry));
         aZipOut.rawWrite(aType);
         aZipOut.rawCloseEntry();
     }
@@ -1158,7 +1158,7 @@ void ZipPackage::WriteManifest( ZipOutputStream& aZipOut, 
const std::vector< uno
 {
     // Write the manifest
     uno::Reference < XManifestWriter > xWriter = ManifestWriter::create( 
m_xContext );
-    ZipEntry * pEntry = new ZipEntry;
+    auto pEntry = std::make_unique<ZipEntry>();
     rtl::Reference<ZipPackageBuffer> pBuffer = new ZipPackageBuffer;
 
     pEntry->sPath = "META-INF/manifest.xml";
@@ -1173,9 +1173,10 @@ void ZipPackage::WriteManifest( ZipOutputStream& 
aZipOut, const std::vector< uno
     pBuffer->realloc( nBufferLength );
 
     // the manifest.xml is never encrypted - so pass an empty reference
-    ZipOutputStream::setEntry(pEntry);
-    aZipOut.writeLOC(pEntry);
-    ZipOutputEntry aZipEntry(aZipOut.getStream(), m_xContext, *pEntry, 
nullptr, /*bEncrypt*/false);
+    ZipOutputStream::setEntry(*pEntry);
+    auto p = pEntry.get();
+    aZipOut.writeLOC(std::move(pEntry));
+    ZipOutputEntry aZipEntry(aZipOut.getStream(), m_xContext, p, nullptr, 
/*bEncrypt*/false);
     aZipEntry.write(pBuffer->getSequence());
     aZipEntry.closeEntry();
     aZipOut.rawCloseEntry();
@@ -1183,7 +1184,7 @@ void ZipPackage::WriteManifest( ZipOutputStream& aZipOut, 
const std::vector< uno
 
 void ZipPackage::WriteContentTypes( ZipOutputStream& aZipOut, const 
std::vector< uno::Sequence < PropertyValue > >& aManList )
 {
-    ZipEntry* pEntry = new ZipEntry;
+    auto pEntry = std::make_unique<ZipEntry>();
     rtl::Reference<ZipPackageBuffer> pBuffer = new ZipPackageBuffer;
 
     pEntry->sPath = "[Content_Types].xml";
@@ -1231,9 +1232,10 @@ void ZipPackage::WriteContentTypes( ZipOutputStream& 
aZipOut, const std::vector<
     pBuffer->realloc( nBufferLength );
 
     // there is no encryption in this format currently
-    ZipOutputStream::setEntry(pEntry);
-    aZipOut.writeLOC(pEntry);
-    ZipOutputEntry aZipEntry(aZipOut.getStream(), m_xContext, *pEntry, 
nullptr, /*bEncrypt*/false);
+    ZipOutputStream::setEntry(*pEntry);
+    auto p = pEntry.get();
+    aZipOut.writeLOC(std::move(pEntry));
+    ZipOutputEntry aZipEntry(aZipOut.getStream(), m_xContext, p, nullptr, 
/*bEncrypt*/false);
     aZipEntry.write(pBuffer->getSequence());
     aZipEntry.closeEntry();
     aZipOut.rawCloseEntry();
diff --git a/package/source/zippackage/ZipPackageFolder.cxx 
b/package/source/zippackage/ZipPackageFolder.cxx
index 4f4da97f5704..c52836379692 100644
--- a/package/source/zippackage/ZipPackageFolder.cxx
+++ b/package/source/zippackage/ZipPackageFolder.cxx
@@ -269,15 +269,15 @@ void ZipPackageFolder::saveContents(
     if ( maContents.empty() && !rPath.isEmpty() && m_nFormat != 
embed::StorageFormats::OFOPXML )
     {
         // it is an empty subfolder, use workaround to store it
-        ZipEntry* pTempEntry = new ZipEntry(aEntry);
+        auto pTempEntry = std::make_unique<ZipEntry>(aEntry);
         pTempEntry->nPathLen = static_cast<sal_Int16>( OUStringToOString( 
rPath, RTL_TEXTENCODING_UTF8 ).getLength() );
         pTempEntry->nExtraLen = -1;
         pTempEntry->sPath = rPath;
 
         try
         {
-            ZipOutputStream::setEntry(pTempEntry);
-            rZipOut.writeLOC(pTempEntry);
+            ZipOutputStream::setEntry(*pTempEntry);
+            rZipOut.writeLOC(std::move(pTempEntry));
             rZipOut.rawCloseEntry();
         }
         catch ( ZipException& )
diff --git a/package/source/zippackage/ZipPackageStream.cxx 
b/package/source/zippackage/ZipPackageStream.cxx
index 2cdfc72a7376..937a322c303c 100644
--- a/package/source/zippackage/ZipPackageStream.cxx
+++ b/package/source/zippackage/ZipPackageStream.cxx
@@ -688,10 +688,8 @@ bool ZipPackageStream::saveChild(
             if ( m_bRawStream )
                 xStream->skipBytes( m_nMagicalHackPos );
 
-            ZipOutputStream::setEntry(pTempEntry);
-            rZipOut.writeLOC(pTempEntry);
-            // coverity[leaked_storage] - the entry is provided to the 
ZipOutputStream that will delete it
-            pAutoTempEntry.release();
+            ZipOutputStream::setEntry(*pTempEntry);
+            rZipOut.writeLOC(std::move(pAutoTempEntry));
 
             uno::Sequence < sal_Int8 > aSeq ( n_ConstBufferSize );
             sal_Int32 nLength;
@@ -757,15 +755,14 @@ bool ZipPackageStream::saveChild(
 
         try
         {
-            ZipOutputStream::setEntry(pTempEntry);
+            ZipOutputStream::setEntry(*pTempEntry);
             // the entry is provided to the ZipOutputStream that will delete it
-            pAutoTempEntry.release();
 
             if (pTempEntry->nMethod == STORED)
             {
                 sal_Int32 nLength;
                 uno::Sequence< sal_Int8 > aSeq(n_ConstBufferSize);
-                rZipOut.writeLOC(pTempEntry, bToBeEncrypted);
+                rZipOut.writeLOC(std::move(pAutoTempEntry), bToBeEncrypted);
                 do
                 {
                     nLength = xStream->readBytes(aSeq, n_ConstBufferSize);
@@ -792,8 +789,8 @@ bool ZipPackageStream::saveChild(
                     // them in threads, but not in background (i.e. 
writeStream() will block).
                     // This is suitable for large data.
                     bBackgroundThreadDeflate = false;
-                    rZipOut.writeLOC(pTempEntry, bToBeEncrypted);
-                    ZipOutputEntryParallel aZipEntry(rZipOut.getStream(), 
m_xContext, *pTempEntry, this, bToBeEncrypted);
+                    rZipOut.writeLOC(std::move(pAutoTempEntry), 
bToBeEncrypted);
+                    ZipOutputEntryParallel aZipEntry(rZipOut.getStream(), 
m_xContext, pTempEntry, this, bToBeEncrypted);
                     aZipEntry.writeStream(xStream);
                     rZipOut.rawCloseEntry(bToBeEncrypted);
                 }
@@ -809,15 +806,15 @@ bool ZipPackageStream::saveChild(
 
                     // Start a new thread task deflating this zip entry
                     ZipOutputEntryInThread *pZipEntry = new 
ZipOutputEntryInThread(
-                            m_xContext, *pTempEntry, this, bToBeEncrypted);
+                            m_xContext, std::move(pAutoTempEntry), this, 
bToBeEncrypted);
                     rZipOut.addDeflatingThreadTask( pZipEntry,
                             pZipEntry->createTask( rZipOut.getThreadTaskTag(), 
xStream) );
                 }
                 else
                 {
                     bBackgroundThreadDeflate = false;
-                    rZipOut.writeLOC(pTempEntry, bToBeEncrypted);
-                    ZipOutputEntry aZipEntry(rZipOut.getStream(), m_xContext, 
*pTempEntry, this, bToBeEncrypted);
+                    rZipOut.writeLOC(std::move(pAutoTempEntry), 
bToBeEncrypted);
+                    ZipOutputEntry aZipEntry(rZipOut.getStream(), m_xContext, 
pTempEntry, this, bToBeEncrypted);
                     aZipEntry.writeStream(xStream);
                     rZipOut.rawCloseEntry(bToBeEncrypted);
                 }

Reply via email to