Skip to content
Permalink
Browse files

Added memory allocation failure tests

  • Loading branch information
homann committed Sep 13, 2012
1 parent 24a047d commit 6b228c999a92786a111eff7cf755c7dcfb5bd3fc
@@ -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;
@@ -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;
@@ -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 )
@@ -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;
@@ -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() );
}

@@ -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
@@ -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;
@@ -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() );
}

@@ -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 )
{
@@ -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() );
}

@@ -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 );
@@ -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;

@@ -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() );
}

@@ -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();
@@ -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++ )
{
@@ -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;
}

0 comments on commit 6b228c9

Please sign in to comment.
You can’t perform that action at this time.