diff options
author | Herbert Dürr <hdu@apache.org> | 2012-07-18 08:40:50 +0000 |
---|---|---|
committer | Herbert Dürr <hdu@apache.org> | 2012-07-18 08:40:50 +0000 |
commit | 7b6990763f759f2de1902f8d22a22eb8e66797f7 (patch) | |
tree | 95aaec1ca855a2c710e6dc737dc64cdcffd3c7f0 /vcl | |
parent | 7a8c7f6a6ceb29bf754e24f07ef92fcee4b925c3 (diff) |
#i120306# better input checks in WinSalBitmap::ImplCreateDIB()
Patch-by: hdu, orw
Notes
Notes:
merged as: a9129e49e23e2a8d0ee9b92e00c2bddb39395d4e
Diffstat (limited to 'vcl')
-rw-r--r-- | vcl/win/source/gdi/salbmp.cxx | 76 |
1 files changed, 43 insertions, 33 deletions
diff --git a/vcl/win/source/gdi/salbmp.cxx b/vcl/win/source/gdi/salbmp.cxx index a5350ec955d5..add628e2e282 100644 --- a/vcl/win/source/gdi/salbmp.cxx +++ b/vcl/win/source/gdi/salbmp.cxx @@ -313,42 +313,52 @@ HGLOBAL WinSalBitmap::ImplCreateDIB( const Size& rSize, sal_uInt16 nBits, const HGLOBAL hDIB = 0; - if ( rSize.Width() && rSize.Height() ) + if( rSize.Width() <= 0 || rSize.Height() <= 0 ) + return hDIB; + + // calculate bitmap size in Bytes + const sal_uLong nAlignedWidth4Bytes = AlignedWidth4Bytes( nBits * rSize.Width() ); + const sal_uLong nImageSize = nAlignedWidth4Bytes * rSize.Height(); + bool bOverflow = (nImageSize / nAlignedWidth4Bytes) != rSize.Height(); + if( bOverflow ) + return hDIB; + + // allocate bitmap memory including header and palette + const sal_uInt16 nColors = (nBits <= 8) ? (1 << nBits) : 0; + const sal_uLong nHeaderSize = sizeof( BITMAPINFOHEADER ) + nColors * sizeof( RGBQUAD ); + bOverflow = (nHeaderSize + nImageSize) < nImageSize; + if( bOverflow ) + return hDIB; + + hDIB = GlobalAlloc( GHND, nHeaderSize + nImageSize ); + if( !hDIB ) + return hDIB; + + PBITMAPINFO pBI = static_cast<PBITMAPINFO>( GlobalLock( hDIB ) ); + PBITMAPINFOHEADER pBIH = static_cast<PBITMAPINFOHEADER>( pBI ); + + pBIH->biSize = sizeof( BITMAPINFOHEADER ); + pBIH->biWidth = rSize.Width(); + pBIH->biHeight = rSize.Height(); + pBIH->biPlanes = 1; + pBIH->biBitCount = nBits; + pBIH->biCompression = BI_RGB; + pBIH->biSizeImage = nImageSize; + pBIH->biXPelsPerMeter = 0; + pBIH->biYPelsPerMeter = 0; + pBIH->biClrUsed = 0; + pBIH->biClrImportant = 0; + + if( nColors ) { - const sal_uLong nImageSize = AlignedWidth4Bytes( nBits * rSize.Width() ) * rSize.Height(); - const sal_uInt16 nColors = ( nBits <= 8 ) ? ( 1 << nBits ) : 0; - - hDIB = GlobalAlloc( GHND, sizeof( BITMAPINFOHEADER ) + nColors * sizeof( RGBQUAD ) + nImageSize ); - - if( hDIB ) - { - PBITMAPINFO pBI = (PBITMAPINFO) GlobalLock( hDIB ); - PBITMAPINFOHEADER pBIH = (PBITMAPINFOHEADER) pBI; - - pBIH->biSize = sizeof( BITMAPINFOHEADER ); - pBIH->biWidth = rSize.Width(); - pBIH->biHeight = rSize.Height(); - pBIH->biPlanes = 1; - pBIH->biBitCount = nBits; - pBIH->biCompression = BI_RGB; - pBIH->biSizeImage = nImageSize; - pBIH->biXPelsPerMeter = 0; - pBIH->biYPelsPerMeter = 0; - pBIH->biClrUsed = 0; - pBIH->biClrImportant = 0; - - if ( nColors ) - { - const sal_uInt16 nMinCount = Min( nColors, rPal.GetEntryCount() ); - - if( nMinCount ) - memcpy( pBI->bmiColors, rPal.ImplGetColorBuffer(), nMinCount * sizeof( RGBQUAD ) ); - } - - GlobalUnlock( hDIB ); - } + // copy the palette entries if any + const sal_uInt16 nMinCount = Min( nColors, rPal.GetEntryCount() ); + if( nMinCount ) + memcpy( pBI->bmiColors, rPal.ImplGetColorBuffer(), nMinCount * sizeof(RGBQUAD) ); } + GlobalUnlock( hDIB ); + return hDIB; } |