From d7b4c9175e89c68aadd8e4a0e1cfb8530e473533 Mon Sep 17 00:00:00 2001 From: filipe oliveira Date: Thu, 15 Dec 2022 20:25:38 +0000 Subject: [PATCH] Fixed small distance replies on GEODIST and GEO commands WITHDIST (#11631) Fixes a regression introduced by #11552 in 7.0.6. it causes replies in the GEO commands to contain garbage when the result is a very small distance (less than 1) Includes test to confirm indeed with junk in buffer now we properly reply --- src/util.c | 24 +++++++++++++++++------- tests/unit/geo.tcl | 7 +++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/util.c b/src/util.c index d094b3b55..74508a70f 100644 --- a/src/util.c +++ b/src/util.c @@ -656,9 +656,7 @@ int fixedpoint_d2string(char *dst, size_t dstlen, double dvalue, int fractional_ if (dvalue == 0) { dst[0] = '0'; dst[1] = '.'; - for (int i = 0; i < fractional_digits; i++) { - dst[2 + i] = '0'; - } + memset(dst + 2, '0', fractional_digits); dst[fractional_digits+2] = '\0'; return fractional_digits + 2; } @@ -707,10 +705,8 @@ int fixedpoint_d2string(char *dst, size_t dstlen, double dvalue, int fractional_ } dst[integer_digits] = '.'; int size = integer_digits + 1 + fractional_digits; - /* fill with 0 if required until fractional_digits */ - for (int i = integer_digits + 1; i < fractional_digits; i++) { - dst[i] = '0'; - } + /* fill with 0 from fractional digits until size */ + memset(dst + integer_digits + 1, '0', fractional_digits); int next = size - 1; while (value >= 100) { int const i = (value % 100) * 2; @@ -1282,6 +1278,20 @@ static void test_fixedpoint_d2string(void) { sz = fixedpoint_d2string(buf, sizeof buf, v, 1); assert(sz == 3); assert(!strcmp(buf, "0.0")); + /* set junk in buffer */ + memset(buf,'A',32); + v = 0.0001; + sz = fixedpoint_d2string(buf, sizeof buf, v, 4); + assert(sz == 6); + assert(buf[sz] == '\0'); + assert(!strcmp(buf, "0.0001")); + /* set junk in buffer */ + memset(buf,'A',32); + v = 6.0642951598391699e-05; + sz = fixedpoint_d2string(buf, sizeof buf, v, 4); + assert(sz == 6); + assert(buf[sz] == '\0'); + assert(!strcmp(buf, "0.0001")); v = 0.01; sz = fixedpoint_d2string(buf, sizeof buf, v, 4); assert(sz == 6); diff --git a/tests/unit/geo.tcl b/tests/unit/geo.tcl index a6a6cbcb6..e07a6784c 100644 --- a/tests/unit/geo.tcl +++ b/tests/unit/geo.tcl @@ -516,6 +516,13 @@ start_server {tags {"geo"}} { assert_equal {point2 point1} [r geosearch points fromlonlat -179 37 bybox 400 400 km asc] } + test {GEOSEARCH with small distance} { + r del points + r geoadd points -122.407107 37.794300 1 + r geoadd points -122.227336 37.794300 2 + assert_equal {{1 0.0001} {2 9.8182}} [r GEORADIUS points -122.407107 37.794300 30 mi ASC WITHDIST] + } + foreach {type} {byradius bybox} { test "GEOSEARCH fuzzy test - $type" { if {$::accurate} { set attempt 300 } else { set attempt 30 }