summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHåvard Pettersen <havardpe@yahooinc.com>2022-06-22 14:02:41 +0000
committerHåvard Pettersen <havardpe@yahooinc.com>2022-06-22 14:02:41 +0000
commit6a8aa76692c25cf920379d30c7224c9557944979 (patch)
tree5ace3a48c760ebe84c459bc5eefd6a8ac74f8643
parent07c6eff98909eb10a3cad8c27acc46edec1198ed (diff)
fix undefined behavior in geo distance
-rw-r--r--searchlib/src/vespa/searchlib/common/geo_location.cpp10
1 files changed, 8 insertions, 2 deletions
diff --git a/searchlib/src/vespa/searchlib/common/geo_location.cpp b/searchlib/src/vespa/searchlib/common/geo_location.cpp
index 1806ba1338c..20408a93a82 100644
--- a/searchlib/src/vespa/searchlib/common/geo_location.cpp
+++ b/searchlib/src/vespa/searchlib/common/geo_location.cpp
@@ -8,6 +8,12 @@ namespace search::common {
namespace {
+uint64_t abs_diff(int32_t a, int32_t b) {
+ return (a > b)
+ ? (int64_t(a) - int64_t(b))
+ : (int64_t(b) - int64_t(a));
+}
+
ZCurve::BoundingBox to_z(GeoLocation::Box box) {
return ZCurve::BoundingBox(box.x.low, box.x.high,
box.y.low, box.y.high);
@@ -158,13 +164,13 @@ GeoLocation::GeoLocation(Box b, Point p, uint32_t r, Aspect xa)
uint64_t GeoLocation::sq_distance_to(Point p) const {
if (has_point) {
- uint64_t dx = (p.x > point.x) ? (p.x - point.x) : (point.x - p.x);
+ uint64_t dx = abs_diff(p.x, point.x);
if (x_aspect.active()) {
// x_aspect is a 32-bit fixed-point number in range [0,1]
// this implements dx = (dx * x_aspect)
dx = (dx * x_aspect.multiplier) >> 32;
}
- uint64_t dy = (p.y > point.y) ? (p.y - point.y) : (point.y - p.y);
+ uint64_t dy = abs_diff(p.y, point.y);
return dx*dx + dy*dy;
}
return 0;