Skip to content

Commit 94e4c8e

Browse files
authored
fix(geo): use strict weak ordering in sortGeoPointDESC comparator (#3439)
The GEO sort DESC comparator used >= instead of >, violating the strict weak ordering requirement of std::sort and causing undefined behavior when elements have equal distances.
1 parent 61f4dc2 commit 94e4c8e

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/types/redis_geo.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,6 @@ bool Geo::appendIfWithinShape(std::vector<GeoPoint> *geo_points, const GeoShape
421421

422422
bool Geo::sortGeoPointASC(const GeoPoint &gp1, const GeoPoint &gp2) { return gp1.dist < gp2.dist; }
423423

424-
bool Geo::sortGeoPointDESC(const GeoPoint &gp1, const GeoPoint &gp2) { return gp1.dist >= gp2.dist; }
424+
bool Geo::sortGeoPointDESC(const GeoPoint &gp1, const GeoPoint &gp2) { return gp1.dist > gp2.dist; }
425425

426426
} // namespace redis

tests/gocase/unit/geo/geo_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,4 +562,17 @@ var testGeo = func(t *testing.T, configs util.KvrocksServerConfigs) {
562562
rdb.GeoRadiusStore(ctx, "points", 13.361389, 38.115556, &redis.GeoRadiusQuery{Radius: 500, Unit: "km", Store: "points2"})
563563
require.EqualValues(t, rdb.ZRange(ctx, "points", 0, -1).Val(), rdb.ZRange(ctx, "points2", 0, -1).Val())
564564
})
565+
566+
t.Run("GEORADIUS DESC with equal distances should not crash", func(t *testing.T) {
567+
require.NoError(t, rdb.Del(ctx, "geokey").Err())
568+
require.NoError(t, rdb.GeoAdd(ctx, "geokey",
569+
&redis.GeoLocation{Name: "A", Longitude: 13.361389, Latitude: 38.115556},
570+
&redis.GeoLocation{Name: "B", Longitude: 13.361389, Latitude: 38.115556},
571+
&redis.GeoLocation{Name: "C", Longitude: 13.361389, Latitude: 38.115556},
572+
&redis.GeoLocation{Name: "D", Longitude: 15.087269, Latitude: 37.502669},
573+
).Err())
574+
results := rdb.GeoRadius(ctx, "geokey", 13.361389, 38.115556,
575+
&redis.GeoRadiusQuery{Radius: 500, Unit: "km", Sort: "DESC"}).Val()
576+
require.Len(t, results, 4)
577+
})
565578
}

0 commit comments

Comments
 (0)