diff options
author | Caolán McNamara <caolanm@redhat.com> | 2015-08-31 16:45:14 +0100 |
---|---|---|
committer | Caolán McNamara <caolanm@redhat.com> | 2015-08-31 16:47:55 +0100 |
commit | 4ce8c21a096d17be9cc40afe11f683c7b3d9fd70 (patch) | |
tree | 46ec8f314396ff2f8933642071e9dc248ed40e45 /vcl | |
parent | b15be387b163ef5e2e338c4701643aac152ec407 (diff) |
other bitmap methods check for bad_alloc
Change-Id: Ic1083fdb566de0b5487b92197f33070ef42146f7
Diffstat (limited to 'vcl')
-rw-r--r-- | vcl/source/gdi/bmpacc.cxx | 36 | ||||
-rw-r--r-- | vcl/source/gdi/dibtools.cxx | 179 |
2 files changed, 112 insertions, 103 deletions
diff --git a/vcl/source/gdi/bmpacc.cxx b/vcl/source/gdi/bmpacc.cxx index ef2983c19393..9917203f565a 100644 --- a/vcl/source/gdi/bmpacc.cxx +++ b/vcl/source/gdi/bmpacc.cxx @@ -127,16 +127,21 @@ BitmapReadAccess::~BitmapReadAccess() void BitmapReadAccess::ImplInitScanBuffer( Bitmap& rBitmap ) { + if (!mpBuffer) + return; + ImpBitmap* pImpBmp = rBitmap.ImplGetImpBitmap(); + if (!pImpBmp) + return; - if( pImpBmp && mpBuffer ) - { - const long nHeight = mpBuffer->mnHeight; - Scanline pTmpLine = mpBuffer->mpBits; + maColorMask = mpBuffer->maColorMask; + bool bOk(true); + const long nHeight = mpBuffer->mnHeight; + Scanline pTmpLine = mpBuffer->mpBits; + try + { mpScanBuf = new Scanline[ nHeight ]; - maColorMask = mpBuffer->maColorMask; - if( BMP_SCANLINE_ADJUSTMENT( mpBuffer->mnFormat ) == BMP_FORMAT_TOP_DOWN ) { for( long nY = 0L; nY < nHeight; nY++, pTmpLine += mpBuffer->mnScanlineSize ) @@ -147,15 +152,20 @@ void BitmapReadAccess::ImplInitScanBuffer( Bitmap& rBitmap ) for( long nY = nHeight - 1; nY >= 0; nY--, pTmpLine += mpBuffer->mnScanlineSize ) mpScanBuf[ nY ] = pTmpLine; } + bOk = ImplSetAccessPointers(BMP_SCANLINE_FORMAT(mpBuffer->mnFormat)); + } + catch (const std::bad_alloc&) + { + bOk = false; + } - if( !ImplSetAccessPointers( BMP_SCANLINE_FORMAT( mpBuffer->mnFormat ) ) ) - { - delete[] mpScanBuf; - mpScanBuf = NULL; + if (!bOk) + { + delete[] mpScanBuf; + mpScanBuf = NULL; - pImpBmp->ImplReleaseBuffer( mpBuffer, mnAccessMode ); - mpBuffer = NULL; - } + pImpBmp->ImplReleaseBuffer( mpBuffer, mnAccessMode ); + mpBuffer = NULL; } } diff --git a/vcl/source/gdi/dibtools.cxx b/vcl/source/gdi/dibtools.cxx index 7ad623cd0a03..d597ee15d1b9 100644 --- a/vcl/source/gdi/dibtools.cxx +++ b/vcl/source/gdi/dibtools.cxx @@ -715,12 +715,14 @@ bool ImplReadDIBBody( SvStream& rIStm, Bitmap& rBmp, Bitmap* pBmpAlpha, sal_uLon const Size aSizePixel(aHeader.nWidth, aHeader.nHeight); BitmapPalette aDummyPal; Bitmap aNewBmp(aSizePixel, nBitCount, &aDummyPal); - Bitmap aNewBmpAlpha; BitmapWriteAccess* pAcc = aNewBmp.AcquireWriteAccess(); + if (!pAcc || pAcc->Width() != aHeader.nWidth || pAcc->Height() != aHeader.nHeight) + return false; + Bitmap aNewBmpAlpha; BitmapWriteAccess* pAccAlpha = 0; bool bAlphaPossible(pBmpAlpha && aHeader.nBitCount == 32); - if(bAlphaPossible) + if (bAlphaPossible) { const bool bRedSet(0 != aHeader.nV5RedMask); const bool bGreenSet(0 != aHeader.nV5GreenMask); @@ -736,120 +738,117 @@ bool ImplReadDIBBody( SvStream& rIStm, Bitmap& rBmp, Bitmap* pBmpAlpha, sal_uLon } } - if(bAlphaPossible) + if (bAlphaPossible) { aNewBmpAlpha = Bitmap(aSizePixel, 8); pAccAlpha = aNewBmpAlpha.AcquireWriteAccess(); } - if(pAcc) - { - sal_uInt16 nColors(0); - SvStream* pIStm; - SvMemoryStream* pMemStm = NULL; - sal_uInt8* pData = NULL; - - if (aHeader.nBitCount <= 8) - { - if(aHeader.nColsUsed) - { - nColors = (sal_uInt16)aHeader.nColsUsed; - } - else - { - nColors = ( 1 << aHeader.nBitCount ); - } - } + sal_uInt16 nColors(0); + SvStream* pIStm; + SvMemoryStream* pMemStm = NULL; + sal_uInt8* pData = NULL; - if(ZCOMPRESS == aHeader.nCompression) + if (aHeader.nBitCount <= 8) + { + if(aHeader.nColsUsed) { - ZCodec aCodec; - sal_uInt32 nCodedSize(0); - sal_uInt32 nUncodedSize(0); - sal_uLong nCodedPos(0); - - // read coding information - rIStm.ReadUInt32( nCodedSize ).ReadUInt32( nUncodedSize ).ReadUInt32( aHeader.nCompression ); - pData = static_cast<sal_uInt8*>(rtl_allocateMemory( nUncodedSize )); - - // decode buffer - nCodedPos = rIStm.Tell(); - aCodec.BeginCompression(); - aCodec.Read( rIStm, pData, nUncodedSize ); - aCodec.EndCompression(); - - // skip unread bytes from coded buffer - rIStm.SeekRel( nCodedSize - ( rIStm.Tell() - nCodedPos ) ); - - // set decoded bytes to memory stream, - // from which we will read the bitmap data - pIStm = pMemStm = new SvMemoryStream; - pMemStm->SetBuffer( pData, nUncodedSize, false, nUncodedSize ); - nOffset = 0; + nColors = (sal_uInt16)aHeader.nColsUsed; } else { - pIStm = &rIStm; + nColors = ( 1 << aHeader.nBitCount ); } + } + + if(ZCOMPRESS == aHeader.nCompression) + { + ZCodec aCodec; + sal_uInt32 nCodedSize(0); + sal_uInt32 nUncodedSize(0); + sal_uLong nCodedPos(0); + + // read coding information + rIStm.ReadUInt32( nCodedSize ).ReadUInt32( nUncodedSize ).ReadUInt32( aHeader.nCompression ); + pData = static_cast<sal_uInt8*>(rtl_allocateMemory( nUncodedSize )); + + // decode buffer + nCodedPos = rIStm.Tell(); + aCodec.BeginCompression(); + aCodec.Read( rIStm, pData, nUncodedSize ); + aCodec.EndCompression(); + + // skip unread bytes from coded buffer + rIStm.SeekRel( nCodedSize - ( rIStm.Tell() - nCodedPos ) ); + + // set decoded bytes to memory stream, + // from which we will read the bitmap data + pIStm = pMemStm = new SvMemoryStream; + pMemStm->SetBuffer( pData, nUncodedSize, false, nUncodedSize ); + nOffset = 0; + } + else + { + pIStm = &rIStm; + } - // read palette - if(nColors) + // read palette + if(nColors) + { + pAcc->SetPaletteEntryCount(nColors); + ImplReadDIBPalette(*pIStm, *pAcc, aHeader.nSize != DIBCOREHEADERSIZE); + } + + // read bits + bool bAlphaUsed(false); + + if(!pIStm->GetError()) + { + if(nOffset) { - pAcc->SetPaletteEntryCount(nColors); - ImplReadDIBPalette(*pIStm, *pAcc, aHeader.nSize != DIBCOREHEADERSIZE); + pIStm->SeekRel(nOffset - (pIStm->Tell() - nStmPos)); } - // read bits - bool bAlphaUsed(false); + bRet = ImplReadDIBBits(*pIStm, aHeader, *pAcc, pAccAlpha, bTopDown, bAlphaUsed); - if(!pIStm->GetError()) + if(bRet && aHeader.nXPelsPerMeter && aHeader.nYPelsPerMeter) { - if(nOffset) - { - pIStm->SeekRel(nOffset - (pIStm->Tell() - nStmPos)); - } + MapMode aMapMode( + MAP_MM, + Point(), + Fraction(1000, aHeader.nXPelsPerMeter), + Fraction(1000, aHeader.nYPelsPerMeter)); + + aNewBmp.SetPrefMapMode(aMapMode); + aNewBmp.SetPrefSize(Size(aHeader.nWidth, aHeader.nHeight)); + } + } - bRet = ImplReadDIBBits(*pIStm, aHeader, *pAcc, pAccAlpha, bTopDown, bAlphaUsed); + if( pData ) + { + rtl_freeMemory(pData); + } - if(bRet && aHeader.nXPelsPerMeter && aHeader.nYPelsPerMeter) - { - MapMode aMapMode( - MAP_MM, - Point(), - Fraction(1000, aHeader.nXPelsPerMeter), - Fraction(1000, aHeader.nYPelsPerMeter)); - - aNewBmp.SetPrefMapMode(aMapMode); - aNewBmp.SetPrefSize(Size(aHeader.nWidth, aHeader.nHeight)); - } - } + delete pMemStm; + Bitmap::ReleaseAccess(pAcc); - if( pData ) + if(bAlphaPossible) + { + Bitmap::ReleaseAccess(pAccAlpha); + + if(!bAlphaUsed) { - rtl_freeMemory(pData); + bAlphaPossible = false; } + } - delete pMemStm; - Bitmap::ReleaseAccess(pAcc); + if(bRet) + { + rBmp = aNewBmp; if(bAlphaPossible) { - Bitmap::ReleaseAccess(pAccAlpha); - - if(!bAlphaUsed) - { - bAlphaPossible = false; - } - } - - if(bRet) - { - rBmp = aNewBmp; - - if(bAlphaPossible) - { - *pBmpAlpha = aNewBmpAlpha; - } + *pBmpAlpha = aNewBmpAlpha; } } } |