Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hincrby/hincrbyfloat on wrong value and float precision check #168

Merged
merged 1 commit into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/redis_hash.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "redis_hash.h"
#include <limits>
#include <cmath>
#include <iostream>
#include <rocksdb/status.h>

Expand Down Expand Up @@ -50,14 +51,18 @@ rocksdb::Status Hash::IncrBy(const Slice &user_key, const Slice &field, int64_t
InternalKey(ns_key, field, metadata.version).Encode(&sub_key);
if (s.ok()) {
std::string value_bytes;
std::size_t idx = 0;
s = db_->Get(rocksdb::ReadOptions(), sub_key, &value_bytes);
if (!s.ok() && !s.IsNotFound()) return s;
if (s.ok()) {
try {
old_value = std::stoll(value_bytes);
old_value = std::stoll(value_bytes, &idx);
} catch (std::exception &e) {
return rocksdb::Status::InvalidArgument(e.what());
}
if (isspace(value_bytes[0]) || idx != value_bytes.size()) {
return rocksdb::Status::InvalidArgument("value is not an integer");
}
exists = true;
}
}
Expand Down Expand Up @@ -96,23 +101,27 @@ rocksdb::Status Hash::IncrByFloat(const Slice &user_key, const Slice &field, dou
InternalKey(ns_key, field, metadata.version).Encode(&sub_key);
if (s.ok()) {
std::string value_bytes;
std::size_t idx = 0;
s = db_->Get(rocksdb::ReadOptions(), sub_key, &value_bytes);
if (!s.ok() && !s.IsNotFound()) return s;
if (s.ok()) {
try {
old_value = std::stod(value_bytes);
old_value = std::stod(value_bytes, &idx);
} catch (std::exception &e) {
return rocksdb::Status::InvalidArgument(e.what());
}
if (isspace(value_bytes[0]) || idx != value_bytes.size()) {
return rocksdb::Status::InvalidArgument("value is not an float");
}
exists = true;
}
}
if ((increment < 0 && old_value < 0 && increment < (std::numeric_limits<double>::lowest()-old_value))
|| (increment > 0 && old_value > 0 && increment > (std::numeric_limits<double>::max()-old_value))) {
return rocksdb::Status::InvalidArgument("increment or decrement would overflow");
double n = old_value + increment;
if (std::isinf(n) || std::isnan(n)) {
return rocksdb::Status::InvalidArgument("increment would produce NaN or Infinity");
}

*ret = old_value + increment;
*ret = n;
rocksdb::WriteBatch batch;
WriteBatchLogData log_data(kRedisHash);
batch.PutLogData(log_data.Encode());
Expand Down
6 changes: 6 additions & 0 deletions tests/functional/hash_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ def test_hincrbyfloat():
assert(is_double_eq(ret, i*1.234))
ret = conn.delete(key)
assert(ret == 1)
ret = conn.hincrbyfloat(key, "fi", 1.11)
assert(is_double_eq(ret, 1.11))
ret = conn.hincrbyfloat(key, "fi", -1.11)
assert(is_double_eq(ret, 0.0))
ret = conn.delete(key)
assert(ret == 1)
# TODO(linty): not number of overflow case
assert_raise(redis.RedisError, conn.hincrbyfloat, key, "f1", "invalid")

Expand Down