Skip to content

Commit

Permalink
Consider maximum image width/height also for GetLegendGrahic
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Jul 31, 2021
1 parent af9a72e commit 661bde4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
19 changes: 12 additions & 7 deletions src/server/services/wms/qgswmsrendercontext.cpp
Expand Up @@ -621,6 +621,11 @@ int QgsWmsRenderContext::mapHeight() const
}

bool QgsWmsRenderContext::isValidWidthHeight() const
{
return isValidWidthHeight( mapWidth(), mapHeight() );
}

bool QgsWmsRenderContext::isValidWidthHeight( int width, int height ) const
{
//test if maxWidth / maxHeight are set in the project or as an env variable
//and WIDTH / HEIGHT parameter is in the range allowed range
Expand All @@ -639,7 +644,7 @@ bool QgsWmsRenderContext::isValidWidthHeight() const
wmsMaxWidth = std::max( wmsMaxWidthProj, wmsMaxWidthEnv );
}

if ( wmsMaxWidth != -1 && mapWidth() > wmsMaxWidth )
if ( wmsMaxWidth != -1 && width > wmsMaxWidth )
{
return false;
}
Expand All @@ -659,7 +664,7 @@ bool QgsWmsRenderContext::isValidWidthHeight() const
wmsMaxHeight = std::max( wmsMaxHeightProj, wmsMaxHeightEnv );
}

if ( wmsMaxHeight != -1 && mapHeight() > wmsMaxHeight )
if ( wmsMaxHeight != -1 && height > wmsMaxHeight )
{
return false;
}
Expand All @@ -680,13 +685,13 @@ bool QgsWmsRenderContext::isValidWidthHeight() const
depth = 32;
}

const int bytes_per_line = ( ( mapWidth() * depth + 31 ) >> 5 ) << 2; // bytes per scanline (must be multiple of 4)
const int bytes_per_line = ( ( width * depth + 31 ) >> 5 ) << 2; // bytes per scanline (must be multiple of 4)

if ( std::numeric_limits<int>::max() / depth < static_cast<uint>( mapWidth() )
if ( std::numeric_limits<int>::max() / depth < static_cast<uint>( width )
|| bytes_per_line <= 0
|| mapHeight() <= 0
|| std::numeric_limits<int>::max() / static_cast<uint>( bytes_per_line ) < static_cast<uint>( mapHeight() )
|| std::numeric_limits<int>::max() / sizeof( uchar * ) < static_cast<uint>( mapHeight() ) )
|| height <= 0
|| std::numeric_limits<int>::max() / static_cast<uint>( bytes_per_line ) < static_cast<uint>( height )
|| std::numeric_limits<int>::max() / sizeof( uchar * ) < static_cast<uint>( height ) )
{
return false;
}
Expand Down
8 changes: 8 additions & 0 deletions src/server/services/wms/qgswmsrendercontext.h
Expand Up @@ -246,6 +246,14 @@ namespace QgsWms
*/
bool isValidWidthHeight() const;

/**
* Returns true if width and height are valid according to the maximum image width/height
* \param width the image width in pixels
* \param height the image height in pixels
* \since QGIS 3.22
*/
bool isValidWidthHeight( int width, int height ) const;

/**
* Returns WIDTH or SRCWIDTH according to \a UseSrcWidthHeight flag.
*/
Expand Down
9 changes: 9 additions & 0 deletions src/server/services/wms/qgswmsrenderer.cpp
Expand Up @@ -131,6 +131,10 @@ namespace QgsWms
const qreal dpmm = mContext.dotsPerMm();
const QSizeF minSize = renderer.minimumSize();
const QSize size( static_cast<int>( minSize.width() * dpmm ), static_cast<int>( minSize.height() * dpmm ) );
if ( !mContext.isValidWidthHeight( size.width(), size.height() ) )
{
throw ( QgsServerException( QStringLiteral( "Legend image is too large" ) ) );
}
image.reset( createImage( size ) );

// configure painter
Expand Down Expand Up @@ -163,6 +167,11 @@ namespace QgsWms

// create image
const QSize size( mWmsParameters.widthAsInt(), mWmsParameters.heightAsInt() );
//test if legend image is larger than max width/height
if ( !mContext.isValidWidthHeight( size.width(), size.height() ) )
{
throw ( QgsServerException( QStringLiteral( "Legend image is too large" ) ) );
}
std::unique_ptr<QImage> image( createImage( size ) );

// configure painter
Expand Down
2 changes: 1 addition & 1 deletion src/ui/qgsprojectpropertiesbase.ui
Expand Up @@ -2139,7 +2139,7 @@
<item row="0" column="0" colspan="5">
<widget class="QLabel" name="label_21">
<property name="text">
<string>Maximums for GetMap request</string>
<string>Maximum image size for GetMap and GetLegendGraphic requests</string>
</property>
</widget>
</item>
Expand Down

0 comments on commit 661bde4

Please sign in to comment.