Fix handling on non-float values in INCRBYFLOAT. Fixes #22

This commit is contained in:
Roman Gershman 2022-04-25 12:07:50 +03:00
parent 2c8cb23098
commit 69c8658be4
2 changed files with 19 additions and 2 deletions

View File

@ -758,11 +758,14 @@ OpResult<double> StringFamily::OpIncrFloat(const OpArgs& op_args, std::string_vi
string tmp; string tmp;
string_view slice = it->second.GetSlice(&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; return OpStatus::INVALID_FLOAT;
} }
base += val; base += val;
if (isnan(base) || isinf(base)) { if (isnan(base) || isinf(base)) {

View File

@ -307,4 +307,18 @@ TEST_F(StringFamilyTest, Range) {
EXPECT_EQ(Run({"getrange","num", "-5000", "10000"}), "1234"); 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 } // namespace dfly