summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Shalamov <alexander.shalamov@mapbox.com>2019-07-29 17:48:22 +0300
committerAlexander Shalamov <alexander.shalamov@mapbox.com>2019-07-30 14:46:18 +0300
commit3b3e4a4772e98bac7ee43a5a02adc7596dff557a (patch)
treeb752a105a86ba8e027993ddb58b6e1efdfee1543
parente338e1d515074b8b370bb38c25b16ff68d16cef1 (diff)
downloadqtlocation-mapboxgl-3b3e4a4772e98bac7ee43a5a02adc7596dff557a.tar.gz
[core] Fix int overflow issue in GridIndex
-rw-r--r--src/mbgl/util/grid_index.cpp24
-rw-r--r--src/mbgl/util/grid_index.hpp10
2 files changed, 18 insertions, 16 deletions
diff --git a/src/mbgl/util/grid_index.cpp b/src/mbgl/util/grid_index.cpp
index f6b59b1bac..f33e889ddb 100644
--- a/src/mbgl/util/grid_index.cpp
+++ b/src/mbgl/util/grid_index.cpp
@@ -9,14 +9,16 @@ namespace mbgl {
template <class T>
-GridIndex<T>::GridIndex(const float width_, const float height_, const int16_t cellSize_) :
+GridIndex<T>::GridIndex(const float width_, const float height_, const uint32_t cellSize_) :
width(width_),
height(height_),
- xCellCount(std::ceil(width_ / cellSize_)),
- yCellCount(std::ceil(height_ / cellSize_)),
- xScale(xCellCount / width_),
- yScale(yCellCount / height_)
+ xCellCount(std::ceil(width / cellSize_)),
+ yCellCount(std::ceil(height / cellSize_)),
+ xScale(xCellCount / width),
+ yScale(yCellCount / height)
{
+ assert(width > 0.0f);
+ assert(height > 0.0f);
boxCells.resize(xCellCount * yCellCount);
circleCells.resize(xCellCount * yCellCount);
}
@@ -30,7 +32,7 @@ void GridIndex<T>::insert(T&& t, const BBox& bbox) {
auto cx2 = convertToXCellCoord(bbox.max.x);
auto cy2 = convertToYCellCoord(bbox.max.y);
- int16_t x, y, cellIndex;
+ std::size_t x, y, cellIndex;
for (x = cx1; x <= cx2; ++x) {
for (y = cy1; y <= cy2; ++y) {
cellIndex = xCellCount * y + x;
@@ -50,7 +52,7 @@ void GridIndex<T>::insert(T&& t, const BCircle& bcircle) {
auto cx2 = convertToXCellCoord(bcircle.center.x + bcircle.radius);
auto cy2 = convertToYCellCoord(bcircle.center.y + bcircle.radius);
- int16_t x, y, cellIndex;
+ std::size_t x, y, cellIndex;
for (x = cx1; x <= cx2; ++x) {
for (y = cy1; y <= cy2; ++y) {
cellIndex = xCellCount * y + x;
@@ -151,7 +153,7 @@ void GridIndex<T>::query(const BBox& queryBBox, std::function<bool (const T&, co
auto cx2 = convertToXCellCoord(queryBBox.max.x);
auto cy2 = convertToYCellCoord(queryBBox.max.y);
- int16_t x, y, cellIndex;
+ std::size_t x, y, cellIndex;
for (x = cx1; x <= cx2; ++x) {
for (y = cy1; y <= cy2; ++y) {
cellIndex = xCellCount * y + x;
@@ -214,7 +216,7 @@ void GridIndex<T>::query(const BCircle& queryBCircle, std::function<bool (const
auto cx2 = convertToXCellCoord(queryBCircle.center.x + queryBCircle.radius);
auto cy2 = convertToYCellCoord(queryBCircle.center.y + queryBCircle.radius);
- int16_t x, y, cellIndex;
+ std::size_t x, y, cellIndex;
for (x = cx1; x <= cx2; ++x) {
for (y = cy1; y <= cy2; ++y) {
cellIndex = xCellCount * y + x;
@@ -252,12 +254,12 @@ void GridIndex<T>::query(const BCircle& queryBCircle, std::function<bool (const
}
template <class T>
-int16_t GridIndex<T>::convertToXCellCoord(const float x) const {
+std::size_t GridIndex<T>::convertToXCellCoord(const float x) const {
return util::max(0.0, util::min(xCellCount - 1.0, std::floor(x * xScale)));
}
template <class T>
-int16_t GridIndex<T>::convertToYCellCoord(const float y) const {
+std::size_t GridIndex<T>::convertToYCellCoord(const float y) const {
return util::max(0.0, util::min(yCellCount - 1.0, std::floor(y * yScale)));
}
diff --git a/src/mbgl/util/grid_index.hpp b/src/mbgl/util/grid_index.hpp
index 4c2d7dccc8..cb38023e59 100644
--- a/src/mbgl/util/grid_index.hpp
+++ b/src/mbgl/util/grid_index.hpp
@@ -57,7 +57,7 @@ template <class T>
class GridIndex {
public:
- GridIndex(const float width_, const float height_, const int16_t cellSize_);
+ GridIndex(const float width_, const float height_, const uint32_t cellSize_);
using BBox = mapbox::geometry::box<float>;
using BCircle = geometry::circle<float>;
@@ -81,8 +81,8 @@ private:
void query(const BBox&, std::function<bool (const T&, const BBox&)>) const;
void query(const BCircle&, std::function<bool (const T&, const BBox&)>) const;
- int16_t convertToXCellCoord(const float x) const;
- int16_t convertToYCellCoord(const float y) const;
+ std::size_t convertToXCellCoord(const float x) const;
+ std::size_t convertToYCellCoord(const float y) const;
bool boxesCollide(const BBox&, const BBox&) const;
bool circlesCollide(const BCircle&, const BCircle&) const;
@@ -91,8 +91,8 @@ private:
const float width;
const float height;
- const int16_t xCellCount;
- const int16_t yCellCount;
+ const std::size_t xCellCount;
+ const std::size_t yCellCount;
const double xScale;
const double yScale;