summaryrefslogtreecommitdiff
path: root/sot
diff options
context:
space:
mode:
authorTakeshi Abe <tabe@fixedpoint.jp>2014-07-12 23:10:34 +0900
committerTakeshi Abe <tabe@fixedpoint.jp>2014-07-12 23:10:34 +0900
commit1bae012c997a3cf5b99a6772746107c0a6800458 (patch)
tree02c294e2b192ce3754e6e5198f9642cd6a77ddc5 /sot
parent717f8a87a7f009b821fd512c26cb35b2cac14993 (diff)
Avoid possible memory leaks in case of exceptions
Change-Id: Id0304366c4e6191db85527935f5bc5cdb0aeb8d8
Diffstat (limited to 'sot')
-rw-r--r--sot/source/sdstor/stgdir.cxx9
-rw-r--r--sot/source/sdstor/stgole.cxx8
-rw-r--r--sot/source/sdstor/stgstrms.cxx16
-rw-r--r--sot/source/sdstor/storage.cxx15
-rw-r--r--sot/source/sdstor/storinfo.cxx9
-rw-r--r--sot/source/sdstor/ucbstorage.cxx42
6 files changed, 46 insertions, 53 deletions
diff --git a/sot/source/sdstor/stgdir.cxx b/sot/source/sdstor/stgdir.cxx
index f4903a57f1db..d7dce359888b 100644
--- a/sot/source/sdstor/stgdir.cxx
+++ b/sot/source/sdstor/stgdir.cxx
@@ -26,7 +26,7 @@
#include "stgstrms.hxx"
#include "stgdir.hxx"
#include "stgio.hxx"
-
+#include <boost/scoped_array.hpp>
//////////////////////////// class StgDirEntry
@@ -350,13 +350,12 @@ bool StgDirEntry::SetSize( sal_Int32 nNewSize )
// if so, we probably need to copy the old data
if( nOldSize )
{
- void* pBuf = new sal_uInt8[ nOldSize ];
+ boost::scoped_array<sal_uInt8> pBuf(new sal_uInt8[ nOldSize ]);
pOld->Pos2Page( 0L );
pStgStrm->Pos2Page( 0L );
- if( pOld->Read( pBuf, nOldSize )
- && pStgStrm->Write( pBuf, nOldSize ) )
+ if( pOld->Read( pBuf.get(), nOldSize )
+ && pStgStrm->Write( pBuf.get(), nOldSize ) )
bRes = true;
- delete[] static_cast<sal_uInt8*>(pBuf);
}
else
bRes = true;
diff --git a/sot/source/sdstor/stgole.cxx b/sot/source/sdstor/stgole.cxx
index 0b9d0b564d2d..9bbf3eef087d 100644
--- a/sot/source/sdstor/stgole.cxx
+++ b/sot/source/sdstor/stgole.cxx
@@ -20,6 +20,7 @@
#include "rtl/string.h"
#include "stgole.hxx"
#include "sot/storinfo.hxx"
+#include <boost/scoped_array.hpp>
#ifdef _MSC_VER
#pragma warning(disable: 4342)
@@ -117,9 +118,9 @@ bool StgCompObjStream::Load()
// higher bits are ignored
sal_uLong nStrLen = ::std::min( nLen1, (sal_Int32)0xFFFE );
- sal_Char* p = new sal_Char[ nStrLen+1 ];
+ boost::scoped_array<sal_Char> p(new sal_Char[ nStrLen+1 ]);
p[nStrLen] = 0;
- if( Read( p, nStrLen ) == nStrLen )
+ if( Read( p.get(), nStrLen ) == nStrLen )
{
//The encoding here is "ANSI", which is pretty useless seeing as
//the actual codepage used doesn't seem to be specified/stored
@@ -127,12 +128,11 @@ bool StgCompObjStream::Load()
//all platforms and envs
//https://issues.apache.org/ooo/attachment.cgi?id=68668
//for a good edge-case example
- aUserName = nStrLen ? OUString( p, nStrLen, RTL_TEXTENCODING_MS_1252 ) : OUString();
+ aUserName = nStrLen ? OUString( p.get(), nStrLen, RTL_TEXTENCODING_MS_1252 ) : OUString();
nCbFormat = ReadClipboardFormat( *this );
}
else
SetError( SVSTREAM_GENERALERROR );
- delete [] p;
}
}
return GetError() == SVSTREAM_OK;
diff --git a/sot/source/sdstor/stgstrms.cxx b/sot/source/sdstor/stgstrms.cxx
index e4214b2f2d71..f2450c944b4e 100644
--- a/sot/source/sdstor/stgstrms.cxx
+++ b/sot/source/sdstor/stgstrms.cxx
@@ -31,6 +31,7 @@
#include "stgstrms.hxx"
#include "stgdir.hxx"
#include "stgio.hxx"
+#include <boost/scoped_array.hpp>
///////////////////////////// class StgFAT
@@ -1136,7 +1137,7 @@ bool StgTmpStrm::Copy( StgTmpStrm& rSrc )
SetSize( n );
if( GetError() == SVSTREAM_OK )
{
- sal_uInt8* p = new sal_uInt8[ 4096 ];
+ boost::scoped_array<sal_uInt8> p(new sal_uInt8[ 4096 ]);
rSrc.Seek( 0L );
Seek( 0L );
while( n )
@@ -1144,13 +1145,13 @@ bool StgTmpStrm::Copy( StgTmpStrm& rSrc )
sal_uLong nn = n;
if( nn > 4096 )
nn = 4096;
- if( rSrc.Read( p, nn ) != nn )
+ if( rSrc.Read( p.get(), nn ) != nn )
break;
- if( Write( p, nn ) != nn )
+ if( Write( p.get(), nn ) != nn )
break;
n -= nn;
}
- delete [] p;
+ p.reset();
rSrc.Seek( nCur );
Seek( nCur );
return n == 0;
@@ -1197,18 +1198,17 @@ void StgTmpStrm::SetSize(sal_uInt64 n)
sal_uLong i = nEndOfData;
if( i )
{
- sal_uInt8* p = new sal_uInt8[ 4096 ];
+ boost::scoped_array<sal_uInt8> p(new sal_uInt8[ 4096 ]);
Seek( 0L );
while( i )
{
sal_uLong nb = ( i > 4096 ) ? 4096 : i;
- if( Read( p, nb ) == nb
- && s->Write( p, nb ) == nb )
+ if( Read( p.get(), nb ) == nb
+ && s->Write( p.get(), nb ) == nb )
i -= nb;
else
break;
}
- delete [] p;
}
if( !i && n > nEndOfData )
{
diff --git a/sot/source/sdstor/storage.cxx b/sot/source/sdstor/storage.cxx
index e4242e132f9b..87fe013bfbe7 100644
--- a/sot/source/sdstor/storage.cxx
+++ b/sot/source/sdstor/storage.cxx
@@ -36,6 +36,8 @@
#include <unotools/localfilehelper.hxx>
#include <unotools/ucbhelper.hxx>
#include <comphelper/processfactory.hxx>
+#include <boost/scoped_array.hpp>
+#include <boost/scoped_ptr.hpp>
using namespace ::com::sun::star;
@@ -230,17 +232,17 @@ bool SotStorageStream::CopyTo( SotStorageStream * pDestStm )
Seek( 0L );
pDestStm->SetSize( 0 ); // Ziel-Stream leeren
- void * pMem = new sal_uInt8[ 8192 ];
+ boost::scoped_array<sal_uInt8> pMem(new sal_uInt8[ 8192 ]);
sal_uLong nRead;
- while( 0 != (nRead = Read( pMem, 8192 )) )
+ while( 0 != (nRead = Read( pMem.get(), 8192 )) )
{
- if( nRead != pDestStm->Write( pMem, nRead ) )
+ if( nRead != pDestStm->Write( pMem.get(), nRead ) )
{
SetError( SVSTREAM_GENERALERROR );
break;
}
}
- delete [] static_cast<sal_uInt8*>(pMem);
+ pMem.reset();
// Position setzen
pDestStm->Seek( nPos );
Seek( nPos );
@@ -581,9 +583,8 @@ bool SotStorage::IsStorageFile( const OUString & rFileName )
aName = aObj.GetMainURL( INetURLObject::NO_DECODE );
}
- SvStream * pStm = ::utl::UcbStreamHelper::CreateStream( aName, STREAM_STD_READ );
- bool bRet = SotStorage::IsStorageFile( pStm );
- delete pStm;
+ boost::scoped_ptr<SvStream> pStm(::utl::UcbStreamHelper::CreateStream( aName, STREAM_STD_READ ));
+ bool bRet = SotStorage::IsStorageFile( pStm.get() );
return bRet;
}
diff --git a/sot/source/sdstor/storinfo.cxx b/sot/source/sdstor/storinfo.cxx
index 03e4a06efd58..2d53d3029301 100644
--- a/sot/source/sdstor/storinfo.cxx
+++ b/sot/source/sdstor/storinfo.cxx
@@ -21,7 +21,7 @@
#include <sot/stg.hxx>
#include <sot/storinfo.hxx>
#include <sot/exchange.hxx>
-
+#include <boost/scoped_array.hpp>
/************** class SvStorageInfo **************************************
*************************************************************************/
@@ -35,14 +35,13 @@ sal_uLong ReadClipboardFormat( SvStream & rStm )
if( nLen > 0 )
{
// get a string name
- sal_Char * p = new( ::std::nothrow ) sal_Char[ nLen ];
- if( p && rStm.Read( p, nLen ) == (sal_uLong) nLen )
+ boost::scoped_array<sal_Char> p(new( ::std::nothrow ) sal_Char[ nLen ]);
+ if( p && rStm.Read( p.get(), nLen ) == (sal_uLong) nLen )
{
- nFormat = SotExchange::RegisterFormatName(OUString(p, nLen-1, RTL_TEXTENCODING_ASCII_US));
+ nFormat = SotExchange::RegisterFormatName(OUString(p.get(), nLen-1, RTL_TEXTENCODING_ASCII_US));
}
else
rStm.SetError( SVSTREAM_GENERALERROR );
- delete [] p;
}
else if( nLen == -1L )
// Windows clipboard format
diff --git a/sot/source/sdstor/ucbstorage.cxx b/sot/source/sdstor/ucbstorage.cxx
index df435babfe4f..879d372ac169 100644
--- a/sot/source/sdstor/ucbstorage.cxx
+++ b/sot/source/sdstor/ucbstorage.cxx
@@ -43,6 +43,7 @@
#include <com/sun/star/packages/manifest/ManifestReader.hpp>
#include <com/sun/star/ucb/InteractiveIOException.hpp>
+#include <boost/scoped_array.hpp>
#include <boost/scoped_ptr.hpp>
#include <rtl/digest.h>
#include <tools/ref.hxx>
@@ -1402,7 +1403,7 @@ bool UCBStorageStream::CopyTo( BaseStorageStream* pDestStm )
if( pDestStm->SetSize( n ) && n )
{
- sal_uInt8* p = new sal_uInt8[ 4096 ];
+ boost::scoped_array<sal_uInt8> p(new sal_uInt8[ 4096 ]);
Seek( 0L );
pDestStm->Seek( 0L );
while( n )
@@ -1410,14 +1411,12 @@ bool UCBStorageStream::CopyTo( BaseStorageStream* pDestStm )
sal_uInt32 nn = n;
if( nn > 4096 )
nn = 4096;
- if( Read( p, nn ) != nn )
+ if( Read( p.get(), nn ) != nn )
break;
- if( pDestStm->Write( p, nn ) != nn )
+ if( pDestStm->Write( p.get(), nn ) != nn )
break;
n -= nn;
}
-
- delete[] p;
}
return true;
@@ -1644,13 +1643,13 @@ UCBStorage_Impl::UCBStorage_Impl( SvStream& rStream, UCBStorage* pStorage, bool
m_aURL = aTemp;
// copy data into the temporary file
- SvStream* pStream = ::utl::UcbStreamHelper::CreateStream( m_pTempFile->GetURL(), STREAM_STD_READWRITE, true /* bFileExists */ );
+ boost::scoped_ptr<SvStream> pStream(::utl::UcbStreamHelper::CreateStream( m_pTempFile->GetURL(), STREAM_STD_READWRITE, true /* bFileExists */ ));
if ( pStream )
{
rStream.Seek(0);
rStream.ReadStream( *pStream );
pStream->Flush();
- DELETEZ( pStream );
+ pStream.reset();
}
// close stream and let content access the file
@@ -1694,7 +1693,7 @@ void UCBStorage_Impl::Init()
aObj.Append( OUString( "manifest.xml" ) );
// create input stream
- SvStream* pStream = ::utl::UcbStreamHelper::CreateStream( aObj.GetMainURL( INetURLObject::NO_DECODE ), STREAM_STD_READ );
+ boost::scoped_ptr<SvStream> pStream(::utl::UcbStreamHelper::CreateStream( aObj.GetMainURL( INetURLObject::NO_DECODE ), STREAM_STD_READ ));
// no stream means no manifest.xml
if ( pStream )
{
@@ -1714,8 +1713,6 @@ void UCBStorage_Impl::Init()
xInputStream = NULL;
SetProps( aProps, OUString() );
}
-
- delete pStream;
}
}
}
@@ -2273,7 +2270,7 @@ sal_Int16 UCBStorage_Impl::Commit()
{
// create a stream to write the manifest file - use a temp file
OUString aURL( aNewSubFolder.getURL() );
- ::utl::TempFile* pTempFile = new ::utl::TempFile( &aURL );
+ boost::scoped_ptr< ::utl::TempFile> pTempFile(new ::utl::TempFile( &aURL ));
// get the stream from the temp file and create an output stream wrapper
SvStream* pStream = pTempFile->GetStream( STREAM_STD_READWRITE );
@@ -2294,7 +2291,7 @@ sal_Int16 UCBStorage_Impl::Commit()
Content aSource( pTempFile->GetURL(), Reference < XCommandEnvironment >(), comphelper::getProcessComponentContext() );
xWriter = NULL;
xOutputStream = NULL;
- DELETEZ( pTempFile );
+ pTempFile.reset();
aNewSubFolder.transferContent( aSource, InsertOperation_MOVE, OUString("manifest.xml"), NameClash::OVERWRITE );
}
}
@@ -2309,11 +2306,11 @@ sal_Int16 UCBStorage_Impl::Commit()
m_pContent->executeCommand( OUString("flush"), aAny );
if ( m_pSource != 0 )
{
- SvStream* pStream = ::utl::UcbStreamHelper::CreateStream( m_pTempFile->GetURL(), STREAM_STD_READ );
+ boost::scoped_ptr<SvStream> pStream(::utl::UcbStreamHelper::CreateStream( m_pTempFile->GetURL(), STREAM_STD_READ ));
m_pSource->SetStreamSize(0);
// m_pSource->Seek(0);
pStream->ReadStream( *m_pSource );
- DELETEZ( pStream );
+ pStream.reset();
m_pSource->Seek(0);
}
}
@@ -2534,7 +2531,7 @@ bool UCBStorage::CopyStorageElement_Impl( UCBStorageElement_Impl& rElement, Base
{
// copy the streams data
// the destination stream must not be open
- BaseStorageStream* pOtherStream = pDest->OpenStream( rNew, STREAM_WRITE | STREAM_SHARE_DENYALL, pImp->m_bDirect );
+ boost::scoped_ptr<BaseStorageStream> pOtherStream(pDest->OpenStream( rNew, STREAM_WRITE | STREAM_SHARE_DENYALL, pImp->m_bDirect ));
BaseStorageStream* pStream = NULL;
bool bDeleteStream = false;
@@ -2547,7 +2544,7 @@ bool UCBStorage::CopyStorageElement_Impl( UCBStorageElement_Impl& rElement, Base
bDeleteStream = true;
}
- pStream->CopyTo( pOtherStream );
+ pStream->CopyTo( pOtherStream.get() );
SetError( pStream->GetError() );
if( pOtherStream->GetError() )
pDest->SetError( pOtherStream->GetError() );
@@ -2556,7 +2553,6 @@ bool UCBStorage::CopyStorageElement_Impl( UCBStorageElement_Impl& rElement, Base
if ( bDeleteStream )
delete pStream;
- delete pOtherStream;
}
else
{
@@ -2578,9 +2574,9 @@ bool UCBStorage::CopyStorageElement_Impl( UCBStorageElement_Impl& rElement, Base
UCBStorage* pUCBCopy = PTR_CAST( UCBStorage, pStorage );
bool bOpenUCBStorage = pUCBDest && pUCBCopy;
- BaseStorage* pOtherStorage = bOpenUCBStorage ?
+ boost::scoped_ptr<BaseStorage> pOtherStorage(bOpenUCBStorage ?
pDest->OpenUCBStorage( rNew, STREAM_WRITE | STREAM_SHARE_DENYALL, pImp->m_bDirect ) :
- pDest->OpenOLEStorage( rNew, STREAM_WRITE | STREAM_SHARE_DENYALL, pImp->m_bDirect );
+ pDest->OpenOLEStorage( rNew, STREAM_WRITE | STREAM_SHARE_DENYALL, pImp->m_bDirect ));
// For UCB storages, the class id and the format id may differ,
// do passing the class id is not sufficient.
@@ -2590,7 +2586,7 @@ bool UCBStorage::CopyStorageElement_Impl( UCBStorageElement_Impl& rElement, Base
pUCBCopy->pImp->m_aUserTypeName );
else
pOtherStorage->SetClassId( pStorage->GetClassId() );
- pStorage->CopyTo( pOtherStorage );
+ pStorage->CopyTo( pOtherStorage.get() );
SetError( pStorage->GetError() );
if( pOtherStorage->GetError() )
pDest->SetError( pOtherStorage->GetError() );
@@ -2599,7 +2595,6 @@ bool UCBStorage::CopyStorageElement_Impl( UCBStorageElement_Impl& rElement, Base
if ( bDeleteStorage )
delete pStorage;
- delete pOtherStorage;
}
return Good() && pDest->Good();
@@ -3181,7 +3176,7 @@ OUString UCBStorage::CreateLinkFile( const OUString& rName )
OUString aName = aFolderObj.GetName();
aFolderObj.removeSegment();
OUString aFolderURL( aFolderObj.GetMainURL( INetURLObject::NO_DECODE ) );
- ::utl::TempFile* pTempFile = new ::utl::TempFile( &aFolderURL );
+ boost::scoped_ptr< ::utl::TempFile> pTempFile(new ::utl::TempFile( &aFolderURL ));
// get the stream from the temp file
SvStream* pStream = pTempFile->GetStream( STREAM_STD_READWRITE | STREAM_TRUNC );
@@ -3236,13 +3231,12 @@ OUString UCBStorage::CreateLinkFile( const OUString& rName )
// move the stream to its desired location
Content aSource( pTempFile->GetURL(), Reference < XCommandEnvironment >(), comphelper::getProcessComponentContext() );
- DELETEZ( pTempFile );
+ pTempFile.reset();
aFolder.transferContent( aSource, InsertOperation_MOVE, aName, NameClash::OVERWRITE );
return aURL;
}
pTempFile->EnableKillingFile( true );
- delete pTempFile;
return OUString();
}