diff options
author | Mikhail Voytenko <mav@openoffice.org> | 2012-07-27 17:30:37 +0200 |
---|---|---|
committer | Michael Stahl <mstahl@redhat.com> | 2012-07-27 17:50:22 +0200 |
commit | 10dc090b31776f21a09e32dd409348e2ddb00cc5 (patch) | |
tree | fb4337041dd3d259b404ae3590a3757406e8931a /sot/source/sdstor/stg.cxx | |
parent | c75e2ed27d5da71891ed7a148ff9603c58eaa58e (diff) |
sot: prevent some null pointer crashes
Diffstat (limited to 'sot/source/sdstor/stg.cxx')
-rw-r--r-- | sot/source/sdstor/stg.cxx | 117 |
1 files changed, 76 insertions, 41 deletions
diff --git a/sot/source/sdstor/stg.cxx b/sot/source/sdstor/stg.cxx index d2c66aa09d5b..0dff1f49ecb0 100644 --- a/sot/source/sdstor/stg.cxx +++ b/sot/source/sdstor/stg.cxx @@ -91,7 +91,8 @@ const SvStream* OLEStorageBase::GetSvStream_Impl() const OLEStorageBase::OLEStorageBase( StgIo* p, StgDirEntry* pe, StreamMode& nMode ) : nStreamMode( nMode ), pIo( p ), pEntry( pe ) { - p->IncRef(); + if ( p ) + p->IncRef(); if( pe ) pe->nRefCnt++; } @@ -108,21 +109,28 @@ OLEStorageBase::~OLEStorageBase() else pEntry->Close(); } + + pEntry = NULL; } - if( !pIo->DecRef() ) + if( pIo && !pIo->DecRef() ) + { delete pIo; + pIo = NULL; + } } // Validate the instance for I/O sal_Bool OLEStorageBase::Validate_Impl( sal_Bool bWrite ) const { - if( pEntry + if( pIo + && pIo->pTOC + && pEntry && !pEntry->bInvalid && ( !bWrite || !pEntry->bDirect || ( nStreamMode & STREAM_WRITE ) ) ) - return sal_True; + return sal_True; return sal_False; } @@ -161,7 +169,7 @@ StorageStream::StorageStream( StgIo* p, StgDirEntry* q, StreamMode m ) : OLEStorageBase( p, q, m_nMode ), nPos( 0L ) { // The dir entry may be 0; this means that the stream is invalid. - if( q ) + if( q && p ) { if( q->nRefCnt == 1 ) { @@ -269,14 +277,21 @@ sal_Bool StorageStream::Commit() sal_Bool StorageStream::Revert() { - pEntry->Revert(); - pIo->MoveError( *this ); - return Good(); + sal_Bool bResult = sal_False; + + if ( Validate() ) + { + pEntry->Revert(); + pIo->MoveError( *this ); + bResult = Good(); + } + + return bResult; } sal_Bool StorageStream::CopyTo( BaseStorageStream* pDest ) { - if( !Validate() || !pDest->Validate( sal_True ) || Equals( *pDest ) ) + if( !Validate() || !pDest || !pDest->Validate( sal_True ) || Equals( *pDest ) ) return sal_False; pEntry->Copy( *pDest ); pDest->Commit(); @@ -328,14 +343,20 @@ sal_Bool Storage::IsStorageFile( const String & rFileName ) sal_Bool Storage::IsStorageFile( SvStream* pStream ) { - StgHeader aHdr; - sal_uLong nPos = pStream->Tell(); - sal_Bool bRet = ( aHdr.Load( *pStream ) && aHdr.Check() ); + sal_Bool bRet = sal_False; + + if ( pStream ) + { + StgHeader aHdr; + sal_uLong nPos = pStream->Tell(); + bRet = ( aHdr.Load( *pStream ) && aHdr.Check() ); + + // It's not a stream error if it is too small for a OLE storage header + if ( pStream->GetErrorCode() == ERRCODE_IO_CANTSEEK ) + pStream->ResetError(); + pStream->Seek( nPos ); + } - // It's not a stream error if it is too small for a OLE storage header - if ( pStream->GetErrorCode() == ERRCODE_IO_CANTSEEK ) - pStream->ResetError(); - pStream->Seek( nPos ); return bRet; } @@ -450,7 +471,9 @@ void Storage::Init( sal_Bool bCreate ) pEntry = NULL; sal_Bool bHdrLoaded = sal_False; bIsRoot = sal_True; - if( pIo->Good() ) + + OSL_ENSURE( pIo, "The pointer may not be empty at this point!" ); + if( pIo->Good() && pIo->GetStrm() ) { sal_uLong nSize = pIo->GetStrm()->Seek( STREAM_SEEK_TO_END ); pIo->GetStrm()->Seek( 0L ); @@ -471,7 +494,7 @@ void Storage::Init( sal_Bool bCreate ) // the file is empty if( !bHdrLoaded ) pIo->Init(); - if( pIo->Good() ) + if( pIo->Good() && pIo->pTOC ) { pEntry = pIo->pTOC->GetRoot(); pEntry->nRefCnt++; @@ -526,7 +549,7 @@ const String& Storage::GetName() const void Storage::FillInfoList( SvStorageInfoList* pList ) const { - if( Validate() ) + if( Validate() && pList ) { StgIterator aIter( *pEntry ); StgDirEntry* p = aIter.First(); @@ -709,21 +732,24 @@ sal_Bool Storage::CopyTo( const String& rElem, BaseStorage* pDest, const String& BaseStorage* p1 = OpenStorage( rElem, INTERNAL_MODE ); BaseStorage* p2 = pDest->OpenOLEStorage( rNew, STREAM_WRITE | STREAM_SHARE_DENYALL, pEntry->bDirect ); - sal_uLong nTmpErr = p2->GetError(); - if( !nTmpErr ) + if ( p2 ) { - p2->SetClassId( p1->GetClassId() ); - p1->CopyTo( p2 ); - SetError( p1->GetError() ); - - nTmpErr = p2->GetError(); + sal_uLong nTmpErr = p2->GetError(); if( !nTmpErr ) - p2->Commit(); + { + p2->SetClassId( p1->GetClassId() ); + p1->CopyTo( p2 ); + SetError( p1->GetError() ); + + nTmpErr = p2->GetError(); + if( !nTmpErr ) + p2->Commit(); + else + pDest->SetError( nTmpErr ); + } else pDest->SetError( nTmpErr ); } - else - pDest->SetError( nTmpErr ); delete p1; delete p2; @@ -735,20 +761,23 @@ sal_Bool Storage::CopyTo( const String& rElem, BaseStorage* pDest, const String& BaseStorageStream* p1 = OpenStream( rElem, INTERNAL_MODE ); BaseStorageStream* p2 = pDest->OpenStream( rNew, STREAM_WRITE | STREAM_SHARE_DENYALL, pEntry->bDirect ); - sal_uLong nTmpErr = p2->GetError(); - if( !nTmpErr ) + if ( p2 ) { - p1->CopyTo( p2 ); - SetError( p1->GetError() ); - - nTmpErr = p2->GetError(); + sal_uLong nTmpErr = p2->GetError(); if( !nTmpErr ) - p2->Commit(); + { + p1->CopyTo( p2 ); + SetError( p1->GetError() ); + + nTmpErr = p2->GetError(); + if( !nTmpErr ) + p2->Commit(); + else + pDest->SetError( nTmpErr ); + } else pDest->SetError( nTmpErr ); } - else - pDest->SetError( nTmpErr ); delete p1; delete p2; @@ -999,17 +1028,23 @@ sal_Bool Storage::ValidateFAT() void Storage::SetDirty() { - pEntry->SetDirty(); + if ( pEntry ) + pEntry->SetDirty(); } void Storage::SetClassId( const ClsId& rId ) { - pEntry->aEntry.SetClassId( rId ); + if ( pEntry ) + pEntry->aEntry.SetClassId( rId ); } const ClsId& Storage::GetClassId() const { - return pEntry->aEntry.GetClassId(); + if ( pEntry ) + return pEntry->aEntry.GetClassId(); + + static ClsId aDummyId = {0,0,0,0,0,0,0,0,0,0,0}; + return aDummyId; } const SvStream* Storage::GetSvStream() const |