loplugin:useuniqueptr in SbiImage

Change-Id: I24ee5de3628d6436dc045cb543d35b16074ef189
Reviewed-on: https://gerrit.libreoffice.org/53700
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
This commit is contained in:
Noel Grandin
2018-04-29 18:40:45 +02:00
parent 73414e3fe8
commit 1cb646dc5f
3 changed files with 33 additions and 38 deletions

View File

@@ -53,9 +53,9 @@ SbiImage::~SbiImage()
void SbiImage::Clear()
{
mvStringOffsets.clear();
delete[] pStrings;
delete[] pCode;
ReleaseLegacyBuffer();
pStrings.reset();
pCode.reset();
pLegacyPCode.reset();
pStrings = nullptr;
pCode = nullptr;
nFlags = SbiImageFlags::NONE;
@@ -160,18 +160,17 @@ bool SbiImage::Load( SvStream& r, sal_uInt32& nVersion )
}
case FileOffset::PCode:
if( bBadVer ) break;
pCode = new char[ nLen ];
pCode.reset(new char[ nLen ]);
nCodeSize = nLen;
r.ReadBytes(pCode, nCodeSize);
r.ReadBytes(pCode.get(), nCodeSize);
if ( bLegacy )
{
ReleaseLegacyBuffer(); // release any previously held buffer
nLegacyCodeSize = static_cast<sal_uInt16>(nCodeSize);
pLegacyPCode = pCode;
pLegacyPCode = std::move(pCode);
PCodeBuffConvertor< sal_uInt16, sal_uInt32 > aLegacyToNew( reinterpret_cast<sal_uInt8*>(pLegacyPCode), nLegacyCodeSize );
PCodeBuffConvertor< sal_uInt16, sal_uInt32 > aLegacyToNew( reinterpret_cast<sal_uInt8*>(pLegacyPCode.get()), nLegacyCodeSize );
aLegacyToNew.convert();
pCode = reinterpret_cast<char*>(aLegacyToNew.GetBuffer());
pCode.reset(reinterpret_cast<char*>(aLegacyToNew.GetBuffer()));
nCodeSize = aLegacyToNew.GetSize();
// we don't release the legacy buffer
// right now, that's because the module
@@ -209,8 +208,7 @@ bool SbiImage::Load( SvStream& r, sal_uInt32& nVersion )
r.ReadUInt32( nLen );
if( SbiGood( r ) )
{
delete [] pStrings;
pStrings = new sal_Unicode[ nLen ];
pStrings.reset(new sal_Unicode[ nLen ]);
nStringSize = static_cast<sal_uInt16>(nLen);
std::unique_ptr<char[]> pByteStrings(new char[ nLen ]);
@@ -219,7 +217,7 @@ bool SbiImage::Load( SvStream& r, sal_uInt32& nVersion )
{
sal_uInt16 nOff2 = static_cast<sal_uInt16>(mvStringOffsets[ j ]);
OUString aStr( pByteStrings.get() + nOff2, strlen(pByteStrings.get() + nOff2), eCharSet );
memcpy( pStrings + nOff2, aStr.getStr(), (aStr.getLength() + 1) * sizeof( sal_Unicode ) );
memcpy( pStrings.get() + nOff2, aStr.getStr(), (aStr.getLength() + 1) * sizeof( sal_Unicode ) );
}
}
break;
@@ -405,16 +403,15 @@ bool SbiImage::Save( SvStream& r, sal_uInt32 nVer )
nPos = SbiOpenRecord( r, FileOffset::PCode, 1 );
if ( bLegacy )
{
ReleaseLegacyBuffer(); // release any previously held buffer
PCodeBuffConvertor< sal_uInt32, sal_uInt16 > aNewToLegacy( reinterpret_cast<sal_uInt8*>(pCode), nCodeSize );
PCodeBuffConvertor< sal_uInt32, sal_uInt16 > aNewToLegacy( reinterpret_cast<sal_uInt8*>(pCode.get()), nCodeSize );
aNewToLegacy.convert();
pLegacyPCode = reinterpret_cast<char*>(aNewToLegacy.GetBuffer());
pLegacyPCode.reset(reinterpret_cast<char*>(aNewToLegacy.GetBuffer()));
nLegacyCodeSize = aNewToLegacy.GetSize();
r.WriteBytes(pLegacyPCode, nLegacyCodeSize);
r.WriteBytes(pLegacyPCode.get(), nLegacyCodeSize);
}
else
{
r.WriteBytes(pCode, nCodeSize);
r.WriteBytes(pCode.get(), nCodeSize);
}
SbiCloseRecord( r, nPos );
}
@@ -433,7 +430,7 @@ bool SbiImage::Save( SvStream& r, sal_uInt32 nVer )
for( size_t i = 0; i < mvStringOffsets.size(); i++ )
{
sal_uInt16 nOff = static_cast<sal_uInt16>(mvStringOffsets[ i ]);
OString aStr(OUStringToOString(OUString(pStrings + nOff), eCharSet));
OString aStr(OUStringToOString(OUString(pStrings.get() + nOff), eCharSet));
memcpy( pByteStrings.get() + nOff, aStr.getStr(), (aStr.getLength() + 1) * sizeof( char ) );
}
r.WriteUInt32( nStringSize );
@@ -534,10 +531,10 @@ void SbiImage::MakeStrings( short nSize )
nStringIdx = 0;
nStringOff = 0;
nStringSize = 1024;
pStrings = new sal_Unicode[ nStringSize ];
pStrings.reset( new sal_Unicode[ nStringSize ]);
mvStringOffsets.resize(nSize);
memset( mvStringOffsets.data(), 0, nSize * sizeof( sal_uInt32 ) );
memset( pStrings, 0, nStringSize * sizeof( sal_Unicode ) );
memset( pStrings.get(), 0, nStringSize * sizeof( sal_Unicode ) );
}
// Add a string to StringPool. The String buffer is dynamically
@@ -560,16 +557,15 @@ void SbiImage::AddString( const OUString& r )
{
sal_uInt32 nNewLen = needed + 1024;
nNewLen &= 0xFFFFFC00; // trim to 1K border
sal_Unicode* p = new sal_Unicode[nNewLen];
memcpy( p, pStrings, nStringSize * sizeof( sal_Unicode ) );
delete[] pStrings;
pStrings = p;
std::unique_ptr<sal_Unicode[]> p(new sal_Unicode[nNewLen]);
memcpy( p.get(), pStrings.get(), nStringSize * sizeof( sal_Unicode ) );
pStrings = std::move(p);
nStringSize = sal::static_int_cast< sal_uInt16 >(nNewLen);
}
if( !bError )
{
mvStringOffsets[ nStringIdx++ ] = nStringOff;
memcpy( pStrings + nStringOff, r.getStr(), len * sizeof( sal_Unicode ) );
memcpy( pStrings.get() + nStringOff, r.getStr(), len * sizeof( sal_Unicode ) );
nStringOff = nStringOff + len;
// Last String? The update the size of the buffer
if( nStringIdx >= short(mvStringOffsets.size()) )
@@ -584,9 +580,9 @@ void SbiImage::AddString( const OUString& r )
// The block was fetched by the compiler from class SbBuffer and
// is already created with new. Additionally it contains all Integers
// in Big Endian format, so can be directly read/written.
void SbiImage::AddCode( char* p, sal_uInt32 s )
void SbiImage::AddCode( std::unique_ptr<char[]> p, sal_uInt32 s )
{
pCode = p;
pCode = std::move(p);
nCodeSize = s;
}
@@ -616,7 +612,7 @@ OUString SbiImage::GetString( short nId ) const
if( nId && nId <= short(mvStringOffsets.size()) )
{
sal_uInt32 nOff = mvStringOffsets[ nId - 1 ];
sal_Unicode* pStr = pStrings + nOff;
sal_Unicode* pStr = pStrings.get() + nOff;
// #i42467: Special treatment for vbNullChar
if( *pStr == 0 )
@@ -645,18 +641,17 @@ const SbxObject* SbiImage::FindType (const OUString& aTypeName) const
sal_uInt16 SbiImage::CalcLegacyOffset( sal_Int32 nOffset )
{
return SbiCodeGen::calcLegacyOffSet( reinterpret_cast<sal_uInt8*>(pCode), nOffset ) ;
return SbiCodeGen::calcLegacyOffSet( reinterpret_cast<sal_uInt8*>(pCode.get()), nOffset ) ;
}
sal_uInt32 SbiImage::CalcNewOffset( sal_Int16 nOffset )
{
return SbiCodeGen::calcNewOffSet( reinterpret_cast<sal_uInt8*>(pLegacyPCode), nOffset ) ;
return SbiCodeGen::calcNewOffSet( reinterpret_cast<sal_uInt8*>(pLegacyPCode.get()), nOffset ) ;
}
void SbiImage::ReleaseLegacyBuffer()
{
delete[] pLegacyPCode;
pLegacyPCode = nullptr;
pLegacyPCode.reset();
nLegacyCodeSize = 0;
}