diff --git a/src/server/string_family.cc b/src/server/string_family.cc index c6cef39..af0a7e8 100644 --- a/src/server/string_family.cc +++ b/src/server/string_family.cc @@ -758,11 +758,14 @@ OpResult StringFamily::OpIncrFloat(const OpArgs& op_args, std::string_vi string tmp; string_view slice = it->second.GetSlice(&tmp); - double base; - if (!absl::SimpleAtod(slice, &base)) { + StringToDoubleConverter stod(StringToDoubleConverter::NO_FLAGS, 0, 0, NULL, NULL); + int processed_digits = 0; + double base = stod.StringToDouble(slice.data(), slice.size(), &processed_digits); + if (unsigned(processed_digits) != slice.size()) { return OpStatus::INVALID_FLOAT; } + base += val; if (isnan(base) || isinf(base)) { diff --git a/src/server/string_family_test.cc b/src/server/string_family_test.cc index f7313fb..3f3fa0d 100644 --- a/src/server/string_family_test.cc +++ b/src/server/string_family_test.cc @@ -307,4 +307,18 @@ TEST_F(StringFamilyTest, Range) { EXPECT_EQ(Run({"getrange","num", "-5000", "10000"}), "1234"); } +TEST_F(StringFamilyTest, IncrByFloat) { + Run({"SET", "nonum", " 11"}); + auto resp = Run({"INCRBYFLOAT", "nonum", "1.0"}); + EXPECT_THAT(resp, ErrArg("not a valid float")); + + Run({"SET", "nonum", "11 "}); + resp = Run({"INCRBYFLOAT", "nonum", "1.0"}); + EXPECT_THAT(resp, ErrArg("not a valid float")); + + Run({"SET", "num", "2.566"}); + resp = Run({"INCRBYFLOAT", "num", "1.0"}); + EXPECT_EQ(resp, "3.566"); +} + } // namespace dfly