@@ -659,7 +659,7 @@ QgsRasterBlock *QgsGdalProvider::block( int bandNo, const QgsRectangle &extent,
659659 return block;
660660}
661661
662- void QgsGdalProvider::readBlock ( int bandNo, int xBlock, int yBlock, void *block )
662+ void QgsGdalProvider::readBlock ( int bandNo, int xBlock, int yBlock, void *data )
663663{
664664 QMutexLocker locker ( mpMutex );
665665 if ( !initIfNeeded () )
@@ -677,25 +677,25 @@ void QgsGdalProvider::readBlock( int bandNo, int xBlock, int yBlock, void *block
677677 // We have to read with correct data type consistent with other readBlock functions
678678 int xOff = xBlock * mXBlockSize ;
679679 int yOff = yBlock * mYBlockSize ;
680- gdalRasterIO ( myGdalBand, GF_Read, xOff, yOff, mXBlockSize , mYBlockSize , block , mXBlockSize , mYBlockSize , ( GDALDataType ) mGdalDataType .at ( bandNo - 1 ), 0 , 0 );
680+ gdalRasterIO ( myGdalBand, GF_Read, xOff, yOff, mXBlockSize , mYBlockSize , data , mXBlockSize , mYBlockSize , ( GDALDataType ) mGdalDataType .at ( bandNo - 1 ), 0 , 0 );
681681}
682682
683- void QgsGdalProvider::readBlock ( int bandNo, QgsRectangle const &extent, int pixelWidth, int pixelHeight, void *block , QgsRasterBlockFeedback *feedback )
683+ void QgsGdalProvider::readBlock ( int bandNo, QgsRectangle const &extent, int pixelWidth, int pixelHeight, void *data , QgsRasterBlockFeedback *feedback )
684684{
685685 QMutexLocker locker ( mpMutex );
686686 if ( !initIfNeeded () )
687687 return ;
688688
689- QgsDebugMsgLevel ( " thePixelWidth = " + QString::number ( pixelWidth ), 5 );
690- QgsDebugMsgLevel ( " thePixelHeight = " + QString::number ( pixelHeight ), 5 );
691- QgsDebugMsgLevel ( " theExtent : " + extent.toString (), 5 );
689+ QgsDebugMsgLevel ( " pixelWidth = " + QString::number ( pixelWidth ), 5 );
690+ QgsDebugMsgLevel ( " pixelHeight = " + QString::number ( pixelHeight ), 5 );
691+ QgsDebugMsgLevel ( " extent : " + extent.toString (), 5 );
692692
693693 for ( int i = 0 ; i < 6 ; i++ )
694694 {
695695 QgsDebugMsgLevel ( QStringLiteral ( " transform : %1" ).arg ( mGeoTransform [i] ), 5 );
696696 }
697697
698- int dataSize = dataTypeSize ( bandNo );
698+ size_t dataSize = static_cast < size_t >( dataTypeSize ( bandNo ) );
699699
700700 // moved to block()
701701#if 0
@@ -713,14 +713,14 @@ void QgsGdalProvider::readBlock( int bandNo, QgsRectangle const &extent, int pi
713713 }
714714#endif
715715
716- QgsRectangle myRasterExtent = extent.intersect ( mExtent );
717- if ( myRasterExtent .isEmpty () )
716+ QgsRectangle rasterExtent = extent.intersect ( mExtent );
717+ if ( rasterExtent .isEmpty () )
718718 {
719719 QgsDebugMsg ( QStringLiteral ( " draw request outside view extent." ) );
720720 return ;
721721 }
722- QgsDebugMsgLevel ( " mExtent : " + mExtent .toString (), 5 );
723- QgsDebugMsgLevel ( " myRasterExtent : " + myRasterExtent .toString (), 5 );
722+ QgsDebugMsgLevel ( " extent : " + mExtent .toString (), 5 );
723+ QgsDebugMsgLevel ( " rasterExtent : " + rasterExtent .toString (), 5 );
724724
725725 double xRes = extent.width () / pixelWidth;
726726 double yRes = extent.height () / pixelHeight;
@@ -751,7 +751,7 @@ void QgsGdalProvider::readBlock( int bandNo, QgsRectangle const &extent, int pi
751751 right = std::round( ( myRasterExtent.xMaximum() - extent.xMinimum() ) / xRes ) - 1;
752752 }
753753#endif
754- QRect subRect = QgsRasterBlock::subRect ( extent, pixelWidth, pixelHeight, myRasterExtent );
754+ QRect subRect = QgsRasterBlock::subRect ( extent, pixelWidth, pixelHeight, rasterExtent );
755755 int top = subRect.top ();
756756 int bottom = subRect.bottom ();
757757 int left = subRect.left ();
@@ -804,23 +804,23 @@ void QgsGdalProvider::readBlock( int bandNo, QgsRectangle const &extent, int pi
804804 // another resampling here which appeares to be quite fast
805805
806806 // Get necessary src extent aligned to src resolution
807- if ( mExtent .xMinimum () < myRasterExtent .xMinimum () )
807+ if ( mExtent .xMinimum () < rasterExtent .xMinimum () )
808808 {
809- srcLeft = static_cast <int >( std::floor ( ( myRasterExtent .xMinimum () - mExtent .xMinimum () ) / srcXRes ) );
809+ srcLeft = static_cast <int >( std::floor ( ( rasterExtent .xMinimum () - mExtent .xMinimum () ) / srcXRes ) );
810810 }
811- if ( mExtent .xMaximum () > myRasterExtent .xMaximum () )
811+ if ( mExtent .xMaximum () > rasterExtent .xMaximum () )
812812 {
813- srcRight = static_cast <int >( std::floor ( ( myRasterExtent .xMaximum () - mExtent .xMinimum () ) / srcXRes ) );
813+ srcRight = static_cast <int >( std::floor ( ( rasterExtent .xMaximum () - mExtent .xMinimum () ) / srcXRes ) );
814814 }
815815
816816 // GDAL states that mGeoTransform[3] is top, may it also be bottom and mGeoTransform[5] positive?
817- if ( mExtent .yMaximum () > myRasterExtent .yMaximum () )
817+ if ( mExtent .yMaximum () > rasterExtent .yMaximum () )
818818 {
819- srcTop = static_cast <int >( std::floor ( -1 . * ( mExtent .yMaximum () - myRasterExtent .yMaximum () ) / srcYRes ) );
819+ srcTop = static_cast <int >( std::floor ( -1 . * ( mExtent .yMaximum () - rasterExtent .yMaximum () ) / srcYRes ) );
820820 }
821- if ( mExtent .yMinimum () < myRasterExtent .yMinimum () )
821+ if ( mExtent .yMinimum () < rasterExtent .yMinimum () )
822822 {
823- srcBottom = static_cast <int >( std::floor ( -1 . * ( mExtent .yMaximum () - myRasterExtent .yMinimum () ) / srcYRes ) );
823+ srcBottom = static_cast <int >( std::floor ( -1 . * ( mExtent .yMaximum () - rasterExtent .yMinimum () ) / srcYRes ) );
824824 }
825825
826826 QgsDebugMsgLevel ( QStringLiteral ( " srcTop = %1 srcBottom = %2 srcLeft = %3 srcRight = %4" ).arg ( srcTop ).arg ( srcBottom ).arg ( srcLeft ).arg ( srcRight ), 5 );
@@ -847,19 +847,29 @@ void QgsGdalProvider::readBlock( int bandNo, QgsRectangle const &extent, int pi
847847 QgsDebugMsgLevel ( QStringLiteral ( " tmpXMin = %1 tmpYMax = %2 tmpWidth = %3 tmpHeight = %4" ).arg ( tmpXMin ).arg ( tmpYMax ).arg ( tmpWidth ).arg ( tmpHeight ), 5 );
848848
849849 // Allocate temporary block
850- char *tmpBlock = ( char * )qgsMalloc ( dataSize * tmpWidth * tmpHeight );
850+ size_t bufferSize = dataSize * static_cast <size_t >( tmpWidth ) * static_cast <size_t >( tmpHeight );
851+ #ifdef Q_PROCESSOR_X86_32
852+ // Safety check for 32 bit systems
853+ qint64 _buffer_size = dataSize * static_cast <qint64>( tmpWidth ) * static_cast <qint64>( tmpHeight );
854+ if ( _buffer_size != static_cast <qint64>( bufferSize ) )
855+ {
856+ QgsDebugMsg ( QStringLiteral ( " Integer overflow calculating buffer size on a 32 bit system." ) );
857+ return ;
858+ }
859+ #endif
860+ char *tmpBlock = static_cast <char *>( qgsMalloc ( bufferSize ) );
851861 if ( ! tmpBlock )
852862 {
853863 QgsDebugMsgLevel ( QStringLiteral ( " Couldn't allocate temporary buffer of %1 bytes" ).arg ( dataSize * tmpWidth * tmpHeight ), 5 );
854864 return ;
855865 }
856866 GDALRasterBandH gdalBand = getBand ( bandNo );
857- GDALDataType type = ( GDALDataType ) mGdalDataType .at ( bandNo - 1 );
867+ GDALDataType type = static_cast <GDALDataType>( mGdalDataType .at ( bandNo - 1 ) );
858868 CPLErrorReset ();
859869
860870 CPLErr err = gdalRasterIO ( gdalBand, GF_Read,
861871 srcLeft, srcTop, srcWidth, srcHeight,
862- ( void * ) tmpBlock,
872+ static_cast < void *>( tmpBlock ) ,
863873 tmpWidth, tmpHeight, type,
864874 0 , 0 , feedback );
865875
@@ -873,15 +883,15 @@ void QgsGdalProvider::readBlock( int bandNo, QgsRectangle const &extent, int pi
873883 double tmpXRes = srcWidth * srcXRes / tmpWidth;
874884 double tmpYRes = srcHeight * srcYRes / tmpHeight; // negative
875885
876- double y = myRasterExtent .yMaximum () - 0.5 * yRes;
886+ double y = rasterExtent .yMaximum () - 0.5 * yRes;
877887 for ( int row = 0 ; row < height; row++ )
878888 {
879889 int tmpRow = static_cast <int >( std::floor ( -1 . * ( tmpYMax - y ) / tmpYRes ) );
880890
881891 char *srcRowBlock = tmpBlock + dataSize * tmpRow * tmpWidth;
882- char *dstRowBlock = ( char * )block + dataSize * ( top + row ) * pixelWidth;
892+ char *dstRowBlock = ( char * )data + dataSize * ( top + row ) * pixelWidth;
883893
884- double x = ( myRasterExtent .xMinimum () + 0.5 * xRes - tmpXMin ) / tmpXRes; // cell center
894+ double x = ( rasterExtent .xMinimum () + 0.5 * xRes - tmpXMin ) / tmpXRes; // cell center
885895 double increment = xRes / tmpXRes;
886896
887897 char *dst = dstRowBlock + dataSize * left;
0 commit comments