[Libreoffice-commits] core.git: sdext/source

Libreoffice Gerrit user logerrit at kemper.freedesktop.org
Thu Aug 30 06:31:20 UTC 2018


 sdext/source/pdfimport/inc/pdfparse.hxx        |    2 -
 sdext/source/pdfimport/pdfparse/pdfentries.cxx |   43 +++++++++++--------------
 2 files changed, 20 insertions(+), 25 deletions(-)

New commits:
commit 27d03cc31acb6a3c06dc56ac0a45a56062b873ec
Author:     Noel Grandin <noel.grandin at collabora.co.uk>
AuthorDate: Wed Aug 29 13:07:32 2018 +0200
Commit:     Noel Grandin <noel.grandin at collabora.co.uk>
CommitDate: Thu Aug 30 08:30:52 2018 +0200

    loplugin:useuniqueptr in PDFObject
    
    Change-Id: I605dd193f4fd32b07762208766db5f529adf7998
    Reviewed-on: https://gerrit.libreoffice.org/59774
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>

diff --git a/sdext/source/pdfimport/inc/pdfparse.hxx b/sdext/source/pdfimport/inc/pdfparse.hxx
index 62edc35a015c..0636ad412e63 100644
--- a/sdext/source/pdfimport/inc/pdfparse.hxx
+++ b/sdext/source/pdfimport/inc/pdfparse.hxx
@@ -275,7 +275,7 @@ private:
     // fills *ppStream and *pBytes with start of stream and count of bytes
     // memory returned in *ppStream must be freed with std::free afterwards
     // fills in NULL and 0 in case of error
-    bool getDeflatedStream( char** ppStream, unsigned int* pBytes, const PDFContainer* pObjectContainer, EmitContext& rContext ) const;
+    bool getDeflatedStream( std::unique_ptr<char[]>& rpStream, unsigned int* pBytes, const PDFContainer* pObjectContainer, EmitContext& rContext ) const;
 };
 
 struct PDFPart : public PDFContainer
diff --git a/sdext/source/pdfimport/pdfparse/pdfentries.cxx b/sdext/source/pdfimport/pdfparse/pdfentries.cxx
index 9c6b169b266e..a0c15c9d14ef 100644
--- a/sdext/source/pdfimport/pdfparse/pdfentries.cxx
+++ b/sdext/source/pdfimport/pdfparse/pdfentries.cxx
@@ -650,7 +650,7 @@ PDFObject::~PDFObject()
 {
 }
 
-bool PDFObject::getDeflatedStream( char** ppStream, unsigned int* pBytes, const PDFContainer* pObjectContainer, EmitContext& rContext ) const
+bool PDFObject::getDeflatedStream( std::unique_ptr<char[]>& rpStream, unsigned int* pBytes, const PDFContainer* pObjectContainer, EmitContext& rContext ) const
 {
     bool bIsDeflated = false;
     if( m_pStream && m_pStream->m_pDict &&
@@ -658,12 +658,11 @@ bool PDFObject::getDeflatedStream( char** ppStream, unsigned int* pBytes, const
         )
     {
         unsigned int nOuterStreamLen = m_pStream->m_nEndOffset - m_pStream->m_nBeginOffset;
-        *ppStream = static_cast<char*>(std::malloc( nOuterStreamLen ));
-        unsigned int nRead = rContext.readOrigBytes( m_pStream->m_nBeginOffset, nOuterStreamLen, *ppStream );
+        rpStream.reset(new char[ nOuterStreamLen ]);
+        unsigned int nRead = rContext.readOrigBytes( m_pStream->m_nBeginOffset, nOuterStreamLen, rpStream.get() );
         if( nRead != nOuterStreamLen )
         {
-            std::free( *ppStream );
-            *ppStream = nullptr;
+            rpStream.reset();
             *pBytes = 0;
             return false;
         }
@@ -689,7 +688,7 @@ bool PDFObject::getDeflatedStream( char** ppStream, unsigned int* pBytes, const
             }
         }
         // prepare compressed data section
-        char* pStream = *ppStream;
+        char* pStream = rpStream.get();
         if( pStream[0] == 's' )
             pStream += 6; // skip "stream"
         // skip line end after "stream"
@@ -697,14 +696,14 @@ bool PDFObject::getDeflatedStream( char** ppStream, unsigned int* pBytes, const
             pStream++;
         // get the compressed length
         *pBytes = m_pStream->getDictLength( pObjectContainer );
-        if( pStream != *ppStream )
-            memmove( *ppStream, pStream, *pBytes );
+        if( pStream != rpStream.get() )
+            memmove( rpStream.get(), pStream, *pBytes );
         if( rContext.m_bDecrypt )
         {
             EmitImplData* pEData = getEmitData( rContext );
-            pEData->decrypt( reinterpret_cast<const sal_uInt8*>(*ppStream),
+            pEData->decrypt( reinterpret_cast<const sal_uInt8*>(rpStream.get()),
                              *pBytes,
-                             reinterpret_cast<sal_uInt8*>(*ppStream),
+                             reinterpret_cast<sal_uInt8*>(rpStream.get()),
                              m_nNumber,
                              m_nGeneration
                              ); // decrypt inplace
@@ -712,7 +711,6 @@ bool PDFObject::getDeflatedStream( char** ppStream, unsigned int* pBytes, const
     }
     else
     {
-        *ppStream = nullptr;
         *pBytes = 0;
     }
     return bIsDeflated;
@@ -769,19 +767,18 @@ void PDFObject::writeStream( EmitContext& rWriteContext, const PDFFile* pParsedF
 {
     if( m_pStream )
     {
-        char* pStream = nullptr;
+        std::unique_ptr<char[]> pStream;
         unsigned int nBytes = 0;
-        if( getDeflatedStream( &pStream, &nBytes, pParsedFile, rWriteContext ) && nBytes && rWriteContext.m_bDeflate )
+        if( getDeflatedStream( pStream, &nBytes, pParsedFile, rWriteContext ) && nBytes && rWriteContext.m_bDeflate )
         {
             sal_uInt8* pOutBytes = nullptr;
             sal_uInt32 nOutBytes = 0;
-            unzipToBuffer( pStream, nBytes, &pOutBytes, &nOutBytes );
+            unzipToBuffer( pStream.get(), nBytes, &pOutBytes, &nOutBytes );
             rWriteContext.write( pOutBytes, nOutBytes );
             std::free( pOutBytes );
         }
         else if( pStream && nBytes )
-            rWriteContext.write( pStream, nBytes );
-        std::free( pStream );
+            rWriteContext.write( pStream.get(), nBytes );
     }
 }
 
@@ -806,20 +803,20 @@ bool PDFObject::emit( EmitContext& rWriteContext ) const
         pEData->setDecryptObject( m_nNumber, m_nGeneration );
     if( (rWriteContext.m_bDeflate || rWriteContext.m_bDecrypt) && pEData )
     {
-        char* pStream = nullptr;
+        std::unique_ptr<char[]> pStream;
         unsigned int nBytes = 0;
-        bool bDeflate = getDeflatedStream( &pStream, &nBytes, pEData->m_pObjectContainer, rWriteContext );
+        bool bDeflate = getDeflatedStream( pStream, &nBytes, pEData->m_pObjectContainer, rWriteContext );
         if( pStream && nBytes )
         {
             // unzip the stream
             sal_uInt8* pOutBytes = nullptr;
             sal_uInt32 nOutBytes = 0;
             if( bDeflate && rWriteContext.m_bDeflate )
-                unzipToBuffer( pStream, nBytes, &pOutBytes, &nOutBytes );
+                unzipToBuffer( pStream.get(), nBytes, &pOutBytes, &nOutBytes );
             else
             {
                 // nothing to deflate, but decryption has happened
-                pOutBytes = reinterpret_cast<sal_uInt8*>(pStream);
+                pOutBytes = reinterpret_cast<sal_uInt8*>(pStream.get());
                 nOutBytes = static_cast<sal_uInt32>(nBytes);
             }
 
@@ -873,16 +870,14 @@ bool PDFObject::emit( EmitContext& rWriteContext ) const
                     bRet = rWriteContext.write( pOutBytes, nOutBytes );
                 if( bRet )
                     bRet = rWriteContext.write( "\nendstream\nendobj\n", 18 );
-                if( pOutBytes != reinterpret_cast<sal_uInt8*>(pStream) )
+                if( pOutBytes != reinterpret_cast<sal_uInt8*>(pStream.get()) )
                     std::free( pOutBytes );
-                std::free( pStream );
                 pEData->setDecryptObject( 0, 0 );
                 return bRet;
             }
-            if( pOutBytes != reinterpret_cast<sal_uInt8*>(pStream) )
+            if( pOutBytes != reinterpret_cast<sal_uInt8*>(pStream.get()) )
                 std::free( pOutBytes );
         }
-        std::free( pStream );
     }
 
     bool bRet = emitSubElements( rWriteContext ) &&


More information about the Libreoffice-commits mailing list