Skip to content

Commit 0c14d09

Browse files
markchandlera-maurice
authored andcommitted
Firebase cpp: Ensure RTDB sorts return values of int64 and double correctly if they have large differences
PiperOrigin-RevId: 277768146
1 parent 35877f1 commit 0c14d09

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

database/src/desktop/query_params_comparator.cc

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "database/src/desktop/query_params_comparator.h"
1616

1717
#include <cassert>
18+
#include <cstdint>
1819

1920
#include "app/src/util.h"
2021
#include "database/src/desktop/util_desktop.h"
@@ -212,7 +213,12 @@ int QueryParamsComparator::CompareValues(const Variant& variant_a,
212213
case kPrecedenceNumber: {
213214
// If they're both integers.
214215
if (type_a == Variant::kTypeInt64 && type_b == Variant::kTypeInt64) {
215-
return value_a->int64_value() - value_b->int64_value();
216+
int64_t int64_a = value_a->int64_value();
217+
int64_t int64_b = value_b->int64_value();
218+
219+
if (int64_a < int64_b) return -1;
220+
if (int64_a > int64_b) return 1;
221+
return 0;
216222
}
217223

218224
// At least one of them is a double, so we treat them both as doubles.
@@ -227,8 +233,10 @@ int QueryParamsComparator::CompareValues(const Variant& variant_a,
227233
double double_b = (type_b == Variant::kTypeDouble)
228234
? value_b->double_value()
229235
: static_cast<double>(value_b->int64_value());
230-
double result = double_a - double_b;
231-
return (result == 0.0) ? 0 : (result > 0.0 ? 1 : -1);
236+
237+
if (double_a < double_b) return -1;
238+
if (double_a > double_b) return 1;
239+
return 0;
232240
}
233241
case kPrecedenceString: {
234242
return strcmp(value_a->string_value(), value_b->string_value());

0 commit comments

Comments
 (0)