diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2021-07-13 18:22:51 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-07-14 13:59:56 +0000 |
commit | e5fa0540c1529155d5c122f29edcc1c3837589cf (patch) | |
tree | a396abe16a42ad5a58f3de06424e0366919c7fc6 /src | |
parent | 5bd10f771727af7953843c27dcc6386c445cec7c (diff) | |
download | qtlocation-e5fa0540c1529155d5c122f29edcc1c3837589cf.tar.gz |
Add warning messages when elements count exceeds INT_MAX
Since Qt 6 we use qsizetype for indexing in QList. This means that all
the APIs should be changed accordingly. However, QML still has only
an int type.
As a result, some APIs have a risk of overflowing and returning incorrect
indexes when used from QML.
In QtPositioning such classes are QGeoPath and QGeoPolygon.
Considering that in the real usecases it's very unlikely that someone
will ever add more than std::numeric_limits<int>::max() elements to the
path or polygon, this patch just adds warning messages for such cases.
As a drive-by: refactor QGeoPolygon::addHole() so that one overload of
this method calls another to avoid code duplication.
Change-Id: Ib8167f729a70ec511c964ea98fa8f8400a56bb1f
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
(cherry picked from commit 22aad092552dbcd8cfd3dc085109199e2c53914e)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/positioning/qgeopath.cpp | 11 | ||||
-rw-r--r-- | src/positioning/qgeopolygon.cpp | 25 |
2 files changed, 30 insertions, 6 deletions
diff --git a/src/positioning/qgeopath.cpp b/src/positioning/qgeopath.cpp index aeec2315..88e4d5a4 100644 --- a/src/positioning/qgeopath.cpp +++ b/src/positioning/qgeopath.cpp @@ -50,6 +50,10 @@ #include "qdoublevector3d_p.h" QT_BEGIN_NAMESPACE +constexpr int kMaxInt = std::numeric_limits<int>::max(); +constexpr auto kWarningString = u"The path has more elements than fit into an int. " + "This can cause errors while querying elements from QML"; + /*! \class QGeoPath \inmodule QtPositioning @@ -311,7 +315,10 @@ double QGeoPath::length(qsizetype indexFrom, qsizetype indexTo) const qsizetype QGeoPath::size() const { Q_D(const QGeoPath); - return d->size(); + const qsizetype result = d->size(); + if (result > kMaxInt) + qWarning() << kWarningString; + return result; } /*! @@ -321,6 +328,8 @@ void QGeoPath::addCoordinate(const QGeoCoordinate &coordinate) { Q_D(QGeoPath); d->addCoordinate(coordinate); + if (d->size() > kMaxInt) + qWarning() << kWarningString; } /*! diff --git a/src/positioning/qgeopolygon.cpp b/src/positioning/qgeopolygon.cpp index cba8b7b5..c34f3631 100644 --- a/src/positioning/qgeopolygon.cpp +++ b/src/positioning/qgeopolygon.cpp @@ -53,6 +53,12 @@ QT_BEGIN_NAMESPACE +constexpr int kMaxInt = std::numeric_limits<int>::max(); +constexpr auto kTooManyHoles = u"The polygon has more holes than fit into an int. " + "This can cause errors while querying holes from QML"; +constexpr auto kTooManyElements = u"The polygon has more elements than fit into an int. " + "This can cause errors while querying elements from QML"; + /*! \class QGeoPolygon \inmodule QtPositioning @@ -298,7 +304,10 @@ double QGeoPolygon::length(qsizetype indexFrom, qsizetype indexTo) const qsizetype QGeoPolygon::size() const { Q_D(const QGeoPolygon); - return d->size(); + const qsizetype result = d->size(); + if (result > kMaxInt) + qWarning() << kTooManyElements; + return result; } /*! @@ -308,6 +317,8 @@ void QGeoPolygon::addCoordinate(const QGeoCoordinate &coordinate) { Q_D(QGeoPolygon); d->addCoordinate(coordinate); + if (d->size() > kMaxInt) + qWarning() << kTooManyElements; } /*! @@ -389,7 +400,6 @@ QString QGeoPolygon::toString() const */ void QGeoPolygon::addHole(const QVariant &holePath) { - Q_D(QGeoPolygon); QList<QGeoCoordinate> qgcHolePath; if (holePath.canConvert<QVariantList>()) { const QVariantList qvlHolePath = holePath.toList(); @@ -399,7 +409,7 @@ void QGeoPolygon::addHole(const QVariant &holePath) } } //ToDo: add QGeoShape support - return d->addHole(qgcHolePath); + addHole(qgcHolePath); } /*! @@ -410,7 +420,9 @@ void QGeoPolygon::addHole(const QVariant &holePath) void QGeoPolygon::addHole(const QList<QGeoCoordinate> &holePath) { Q_D(QGeoPolygon); - return d->addHole(holePath); + d->addHole(holePath); + if (d->holesCount() > kMaxInt) + qDebug() << kTooManyHoles; } /*! @@ -458,7 +470,10 @@ void QGeoPolygon::removeHole(qsizetype index) qsizetype QGeoPolygon::holesCount() const { Q_D(const QGeoPolygon); - return d->holesCount(); + const qsizetype result = d->holesCount(); + if (result > kMaxInt) + qWarning() << kTooManyHoles; + return result; } /******************************************************************************* |