Skip to content

Commit

Permalink
Added memory allocation failure tests
Browse files Browse the repository at this point in the history
  • Loading branch information
homann committed Sep 13, 2012
1 parent 24a047d commit 6b228c9
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/core/qgsrasterdataprovider.cpp
Expand Up @@ -101,6 +101,11 @@ void * QgsRasterDataProvider::readBlock( int bandNo, QgsRectangle const & exten

// TODO: replace VSIMalloc, it is GDAL function
void * data = VSIMalloc( dataTypeSize( bandNo ) * width * height );
if ( ! data )
{
QgsDebugMsg( QString( "Couldn't allocate data memory of % bytes" ).arg( dataTypeSize( bandNo ) * width * height ) );
return 0;
}
readBlock( bandNo, extent, width, height, data );

return data;
Expand Down
7 changes: 7 additions & 0 deletions src/core/qgsrasterprojector.cpp
Expand Up @@ -686,6 +686,13 @@ void * QgsRasterProjector::readBlock( int bandNo, QgsRectangle const & extent,
int outputSize = width * height * pixelSize;
void * outputData = malloc( outputSize );

// Check for allcoation error
if ( ! outputData )
{
QgsDebugMsg( QString( "Couldn't malloc %1 bytes!" ).arg( outputSize ) );
free( inputData );
return 0;
}
// TODO: fill by transparent

int srcRow, srcCol;
Expand Down
28 changes: 27 additions & 1 deletion src/core/raster/qgsmultibandcolorrenderer.cpp
Expand Up @@ -195,7 +195,17 @@ void * QgsMultiBandColorRenderer::readBlock( int bandNo, QgsRectangle const & e
for ( ; bandIt != bands.constEnd(); ++bandIt )
{
bandData[*bandIt] = mInput->block( *bandIt, extent, width, height );
if ( !bandData[*bandIt] ) return 0;
if ( !bandData[*bandIt] )
{
// We should free the alloced mem from block().
QgsDebugMsg("No input band" );
bandIt--;
for ( ; bandIt != bands.constBegin(); bandIt-- )
{
VSIFree( bandData[*bandIt] );
}
return 0;
}
}

if ( mRedBand > 0 )
Expand All @@ -216,6 +226,17 @@ void * QgsMultiBandColorRenderer::readBlock( int bandNo, QgsRectangle const & e
}

QImage img( width, height, QImage::Format_ARGB32_Premultiplied );
if ( img.isNull() )
{
QgsDebugMsg( "Could not create QImage" );
bandIt = bands.constBegin();
for ( ; bandIt != bands.constEnd(); ++bandIt )
{
VSIFree( bandData[*bandIt] );
}
return 0;
}

QRgb* imageScanLine = 0;
int currentRasterPos = 0;
int redVal = 0;
Expand Down Expand Up @@ -334,6 +355,11 @@ void * QgsMultiBandColorRenderer::readBlock( int bandNo, QgsRectangle const & e
}

void * data = VSIMalloc( img.byteCount() );
if ( ! data )
{
QgsDebugMsg( QString( "Couldn't allocate output data memory of % bytes" ).arg( img.byteCount() ) );
return 0;
}
return memcpy( data, img.bits(), img.byteCount() );
}

Expand Down
19 changes: 19 additions & 0 deletions src/core/raster/qgspalettedrasterrenderer.cpp
Expand Up @@ -103,6 +103,12 @@ void * QgsPalettedRasterRenderer::readBlock( int bandNo, QgsRectangle const & e

QgsRasterInterface::DataType rasterType = ( QgsRasterInterface::DataType )mInput->dataType( mBandNumber );
void* rasterData = mInput->block( bandNo, extent, width, height );
if ( ! rasterData )
{
QgsDebugMsg("No raster data!" );
return 0;
}

double currentOpacity = mOpacity;

//rendering is faster without considering user-defined transparency
Expand All @@ -120,6 +126,13 @@ void * QgsPalettedRasterRenderer::readBlock( int bandNo, QgsRectangle const & e

//create image
QImage img( width, height, QImage::Format_ARGB32_Premultiplied );
if ( img.isNull() )
{
QgsDebugMsg( "Could not create QImage" );
VSIFree( rasterData );
return 0;
}

QRgb* imageScanLine = 0;
int val = 0;
int currentRasterPos = 0;
Expand Down Expand Up @@ -174,7 +187,13 @@ void * QgsPalettedRasterRenderer::readBlock( int bandNo, QgsRectangle const & e
}

VSIFree( rasterData );

void * data = VSIMalloc( img.byteCount() );
if ( ! data )
{
QgsDebugMsg( QString( "Couldn't allocate output data memory of % bytes" ).arg( img.byteCount() ) );
return 0;
}
return memcpy( data, img.bits(), img.byteCount() );
}

Expand Down
17 changes: 17 additions & 0 deletions src/core/raster/qgssinglebandcolordatarenderer.cpp
Expand Up @@ -62,9 +62,21 @@ void * QgsSingleBandColorDataRenderer::readBlock( int bandNo, QgsRectangle cons
bool hasTransparency = usesTransparency();

void* rasterData = mInput->block( bandNo, extent, width, height );
if ( ! rasterData )
{
QgsDebugMsg("No raster data!" );
return 0;
}

currentRasterPos = 0;
QImage img( width, height, QImage::Format_ARGB32 );
if ( img.isNull() )
{
QgsDebugMsg( "Could not create QImage" );
VSIFree( rasterData );
return 0;
}

uchar* scanLine = 0;
for ( int i = 0; i < height; ++i )
{
Expand All @@ -91,6 +103,11 @@ void * QgsSingleBandColorDataRenderer::readBlock( int bandNo, QgsRectangle cons
VSIFree( rasterData );

void * data = VSIMalloc( img.byteCount() );
if ( ! data )
{
QgsDebugMsg( QString( "Couldn't allocate output data memory of % bytes" ).arg( img.byteCount() ) );
return 0;
}
return memcpy( data, img.bits(), img.byteCount() );
}

Expand Down
17 changes: 17 additions & 0 deletions src/core/raster/qgssinglebandpseudocolorrenderer.cpp
Expand Up @@ -89,6 +89,11 @@ void * QgsSingleBandPseudoColorRenderer::readBlock( int bandNo, QgsRectangle co
QgsRasterInterface::DataType rasterType = ( QgsRasterInterface::DataType )mInput->dataType( mBand );

void* rasterData = mInput->block( mBand, extent, width, height );
if ( ! rasterData )
{
QgsDebugMsg("No raster data!" );
return 0;
}

int red, green, blue;
QRgb myDefaultColor = qRgba( 255, 255, 255, 0 );
Expand All @@ -107,6 +112,13 @@ void * QgsSingleBandPseudoColorRenderer::readBlock( int bandNo, QgsRectangle co

//create image
QImage img( width, height, QImage::Format_ARGB32_Premultiplied );
if ( img.isNull() )
{
QgsDebugMsg( "Could not create QImage" );
VSIFree( rasterData );
return 0;
}

QRgb* imageScanLine = 0;
double val = 0;

Expand Down Expand Up @@ -156,6 +168,11 @@ void * QgsSingleBandPseudoColorRenderer::readBlock( int bandNo, QgsRectangle co
VSIFree( rasterData );

void * data = VSIMalloc( img.byteCount() );
if ( ! data )
{
QgsDebugMsg( QString( "Couldn't allocate output data memory of % bytes" ).arg( img.byteCount() ) );
return 0;
}
return memcpy( data, img.bits(), img.byteCount() );
}

Expand Down
6 changes: 5 additions & 1 deletion src/providers/gdal/qgsgdalprovider.cpp
Expand Up @@ -520,7 +520,11 @@ void QgsGdalProvider::readBlock( int theBandNo, QgsRectangle const & theExtent,

// Allocate temporary block
char *tmpBlock = ( char * )malloc( dataSize * tmpWidth * tmpHeight );

if ( ! tmpBlock )
{
QgsDebugMsg( QString( "Coudn't allocate temporary buffer of %1 bytes" ).arg( dataSize * tmpWidth * tmpHeight ) );
return;
}
GDALRasterBandH gdalBand = GDALGetRasterBand( mGdalDataset, theBandNo );
GDALDataType type = ( GDALDataType )mGdalDataType[theBandNo-1];
CPLErrorReset();
Expand Down
5 changes: 5 additions & 0 deletions src/providers/wcs/qgswcsprovider.cpp
Expand Up @@ -537,6 +537,11 @@ void QgsWcsProvider::readBlock( int bandNo, QgsRectangle const & viewExtent, in
QgsDebugMsg( QString( "pixelSize = %1" ).arg( pixelSize ) );
int size = width * height * pixelSize;
void * tmpData = malloc( size );
if ( ! tmpData )
{
QgsDebugMsg( QString( "Couldn't allocate memory of %1 bytes" ).arg( size ) );
return;
}
GDALRasterIO( gdalBand, GF_Read, 0, 0, width, height, tmpData, width, height, ( GDALDataType ) mGdalDataType[bandNo-1], 0, 0 );
for ( int i = 0; i < pixelHeight; i++ )
{
Expand Down
6 changes: 5 additions & 1 deletion src/providers/wms/qgswmsprovider.cpp
Expand Up @@ -968,7 +968,11 @@ void QgsWmsProvider::readBlock( int bandNo, QgsRectangle const & viewExtent, in
}

uchar * ptr = image->bits() ;
memcpy( block, ptr, myExpectedSize );
if ( ptr )
{
// If image is too large, ptr can be NULL
memcpy( block, ptr, myExpectedSize );
}
// do not delete the image, it is handled by draw()
//delete image;
}
Expand Down

0 comments on commit 6b228c9

Please sign in to comment.