Skip to content

Commit

Permalink
Silence invalid cppcheck container out of bounds warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson authored and github-actions[bot] committed Apr 14, 2023
1 parent b8d6e99 commit 219575c
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 17 deletions.
5 changes: 4 additions & 1 deletion src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -5974,7 +5974,7 @@ static QVariant fncColorPart( const QVariantList &values, const QgsExpressionCon
static QVariant fcnCreateRamp( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
const QVariantMap map = QgsExpressionUtils::getMapValue( values.at( 0 ), parent );
if ( map.count() < 1 )
if ( map.empty() )
{
parent->setEvalErrorString( QObject::tr( "A minimum of two colors is required to create a ramp" ) );
return QVariant();
Expand Down Expand Up @@ -6009,6 +6009,9 @@ static QVariant fcnCreateRamp( const QVariantList &values, const QgsExpressionCo
}
bool discrete = values.at( 1 ).toBool();

if ( colors.empty() )
return QVariant();

return QVariant::fromValue( QgsGradientColorRamp( colors.first(), colors.last(), discrete, stops ) );
}

Expand Down
14 changes: 10 additions & 4 deletions src/core/geometry/qgscircularstring.cpp
Expand Up @@ -449,14 +449,20 @@ bool QgsCircularString::fromWkb( QgsConstWkbPtr &wkbPtr )
mWkbType = type;

//type
bool hasZ = is3D();
bool hasM = isMeasure();
const bool hasZ = is3D();
const bool hasM = isMeasure();
int nVertices = 0;
wkbPtr >> nVertices;
mX.resize( nVertices );
mY.resize( nVertices );
hasZ ? mZ.resize( nVertices ) : mZ.clear();
hasM ? mM.resize( nVertices ) : mM.clear();
if ( hasZ )
mZ.resize( nVertices );
else
mZ.clear();
if ( hasM )
mM.resize( nVertices );
else
mM.clear();
for ( int i = 0; i < nVertices; ++i )
{
wkbPtr >> mX[i];
Expand Down
6 changes: 4 additions & 2 deletions src/core/geometry/qgscurvepolygon.cpp
Expand Up @@ -1171,12 +1171,14 @@ bool QgsCurvePolygon::moveVertex( QgsVertexId vId, const QgsPoint &newPos )

bool QgsCurvePolygon::deleteVertex( QgsVertexId vId )
{
if ( !mExteriorRing || vId.ring < 0 || vId.ring >= 1 + mInteriorRings.size() )
const int interiorRingId = vId.ring - 1;
if ( !mExteriorRing || vId.ring < 0 || interiorRingId >= mInteriorRings.size() )
{
return false;
}

QgsCurve *ring = vId.ring == 0 ? mExteriorRing.get() : mInteriorRings.at( vId.ring - 1 );
// cppcheck-suppress containerOutOfBounds
QgsCurve *ring = vId.ring == 0 ? mExteriorRing.get() : mInteriorRings.at( interiorRingId );
int n = ring->numPoints();
if ( n <= 4 )
{
Expand Down
10 changes: 6 additions & 4 deletions src/core/raster/qgsrasterattributetable.cpp
Expand Up @@ -1106,12 +1106,13 @@ QgsRasterAttributeTable *QgsRasterAttributeTable::createFromRaster( QgsRasterLay
rat->appendField( QStringLiteral( "BlueMax" ), Qgis::RasterAttributeTableFieldUsage::BlueMax, QVariant::Type::Int );
rat->appendField( QStringLiteral( "AlphaMax" ), Qgis::RasterAttributeTableFieldUsage::AlphaMax, QVariant::Type::Int );
const QList<QgsColorRampShader::ColorRampItem> rampItems { shaderFunction->colorRampItemList() };
if ( rampItems.count( ) > 1 )
if ( rampItems.size() > 1 )
{
QColor color1 { rampItems.at( 0 ).color };
QString label1 { rampItems.at( 0 ).label };
QVariant value1( rampItems.at( 0 ).value );
for ( int i = 1; i < rampItems.count( ); ++i )
const int rampItemSize = rampItems.size();
for ( int i = 1; i < rampItemSize; ++i )
{
const QgsColorRampShader::ColorRampItem &rampItem { rampItems.at( i )};
rat->appendRow( QVariantList() << value1 << rampItem.value << QStringLiteral( "%1 - %2" ).arg( label1, rampItem.label ) << 0 << 0 << 0 << 255 << 0 << 0 << 0 << 255 );
Expand All @@ -1134,12 +1135,13 @@ QgsRasterAttributeTable *QgsRasterAttributeTable::createFromRaster( QgsRasterLay
rat->appendField( QStringLiteral( "Blue" ), Qgis::RasterAttributeTableFieldUsage::Blue, QVariant::Type::Int );
rat->appendField( QStringLiteral( "Alpha" ), Qgis::RasterAttributeTableFieldUsage::Alpha, QVariant::Type::Int );
const QList<QgsColorRampShader::ColorRampItem> rampItems { shaderFunction->colorRampItemList() };
if ( rampItems.count( ) > 1 )
if ( rampItems.size( ) > 1 )
{
QColor color1 { rampItems.at( 0 ).color };
QString label1 { rampItems.at( 0 ).label };
QVariant value1( rampItems.at( 0 ).value );
for ( int i = 1; i < rampItems.count( ); ++i )
const int rampItemSize = rampItems.size();
for ( int i = 1; i < rampItemSize; ++i )
{
const QgsColorRampShader::ColorRampItem &rampItem { rampItems.at( i )};
rat->appendRow( QVariantList() << value1 << rampItem.value << QStringLiteral( "%1 - %2" ).arg( label1, rampItem.label ) << 0 << 0 << 0 << 255 << 0 << 0 << 0 << 255 );
Expand Down
5 changes: 3 additions & 2 deletions src/providers/delimitedtext/qgsdelimitedtextprovider.cpp
Expand Up @@ -170,9 +170,10 @@ QgsDelimitedTextProvider::QgsDelimitedTextProvider( const QString &uri, const Pr
if ( queryItem.first.compare( QStringLiteral( "field" ), Qt::CaseSensitivity::CaseInsensitive ) == 0 )
{
const QStringList parts { queryItem.second.split( ':' ) };
if ( parts.count() == 2 )
if ( parts.size() == 2 )
{
mUserDefinedFieldTypes.insert( parts[0], parts [1] );
// cppcheck-suppress containerOutOfBounds
mUserDefinedFieldTypes.insert( parts[0], parts[1] );
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/providers/mssql/qgsmssqltablemodel.cpp
Expand Up @@ -270,6 +270,10 @@ void QgsMssqlTableModel::setGeometryTypesForTable( QgsMssqlLayerProperty layerPr
schemaItem = schemaItems.at( 0 );

int n = schemaItem->rowCount();
const int columns = columnCount();
if ( columns == 0 )
return;

for ( int i = 0; i < n; i++ )
{
QModelIndex currentChildIndex = indexFromItem( schemaItem->child( i, DbtmSchema ) );
Expand All @@ -279,13 +283,16 @@ void QgsMssqlTableModel::setGeometryTypesForTable( QgsMssqlLayerProperty layerPr
}

QList<QStandardItem *> row;
row.reserve( columnCount() );
row.reserve( columns );

for ( int j = 0; j < columnCount(); j++ )
for ( int j = 0; j < columns; j++ )
{
row << itemFromIndex( currentChildIndex.sibling( i, j ) );
}

if ( row.empty() )
continue;

if ( row[ DbtmTable ]->text() == layerProperty.tableName && row[ DbtmGeomCol ]->text() == layerProperty.geometryColName )
{
row[ DbtmSrid ]->setText( layerProperty.srid );
Expand Down
3 changes: 3 additions & 0 deletions src/server/qgis_mapserver.cpp
Expand Up @@ -243,6 +243,7 @@ class TcpServerWorker: public QObject
throw HttpException( QStringLiteral( "HTTP error unsupported method: %1" ).arg( methodString ) );
}

// cppcheck-suppress containerOutOfBounds
const QString protocol { firstLinePieces.at( 2 )};
if ( protocol != QLatin1String( "HTTP/1.0" ) && protocol != QLatin1String( "HTTP/1.1" ) )
{
Expand Down Expand Up @@ -291,6 +292,7 @@ class TcpServerWorker: public QObject
// ... or from server ip/port and request path
if ( url.isEmpty() )
{
// cppcheck-suppress containerOutOfBounds
const QString path { firstLinePieces.at( 1 )};
// Take Host header if defined
if ( headers.contains( QStringLiteral( "Host" ) ) )
Expand Down Expand Up @@ -590,6 +592,7 @@ int main( int argc, char *argv[] )
if ( addressAndPort.size() == 2 )
{
ipAddress = addressAndPort.at( 0 );
// cppcheck-suppress containerOutOfBounds
serverPort = addressAndPort.at( 1 );
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/server/qgsserverapiutils.cpp
Expand Up @@ -120,10 +120,11 @@ template<typename T, class T2> T QgsServerApiUtils::parseTemporalInterval( const
}
};
const QStringList parts { interval.split( '/' ) };
if ( parts.length() != 2 )
if ( parts.size() != 2 )
{
throw QgsServerApiBadRequestException( QStringLiteral( "%1 is not a valid datetime interval." ).arg( interval ), QStringLiteral( "Server" ) );
}
// cppcheck-suppress containerOutOfBounds
T result { parseDate( parts[0] ), parseDate( parts[1] ) };
// Check validity
if ( result.isEmpty() )
Expand Down Expand Up @@ -273,6 +274,7 @@ QgsExpression QgsServerApiUtils::temporalFilterExpression( const QgsVectorLayer
testType = parts[0];
if ( testType.isEmpty() || testType == QLatin1String( ".." ) )
{
// cppcheck-suppress containerOutOfBounds
testType = parts[1];
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/services/wmts/qgswmtsutils.cpp
Expand Up @@ -738,7 +738,7 @@ namespace QgsWmts
//defining TileMatrix idx
const int tm_idx = params.tileMatrixAsInt();
//read TileMatrix
if ( tm_idx < 0 || tms.tileMatrixList.count() < tm_idx )
if ( tm_idx < 0 || tm_idx >= tms.tileMatrixList.size() )
{
throw QgsRequestNotWellFormedException( QStringLiteral( "TileMatrix is unknown" ) );
}
Expand Down

0 comments on commit 219575c

Please sign in to comment.