Skip to content

Commit

Permalink
fix hincrby issue
Browse files Browse the repository at this point in the history
  • Loading branch information
hahahashen committed Oct 21, 2024
1 parent 8d41ffd commit 4079e97
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/pstd/pstd_string.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ int String2int(const char* s, size_t slen, T* val) {
}
}

/* Convert a string into a integral. Returns 1 if the all char of string could be parsed
* into a integer, 0 otherwise. The value will be set to
* the parsed value when appropriate. */
template <std::integral T>
int String2intStrict(const char* s, size_t slen, T* val) {
auto [ptr, ec] = std::from_chars(s, s + slen, *val);
if (ec != std::errc()) {
return 0;
}
if (ptr != s + slen) {
return 0;
}
return 1;
}

template <std::integral T>
inline int String2int(const std::string& s, T* val) {
return String2int(s.data(), s.size(), val);
Expand Down
1 change: 1 addition & 0 deletions src/storage/include/storage/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace storage {

int Int64ToStr(char* dst, size_t dstlen, int64_t svalue);
int StrToInt64(const char* s, size_t slen, int64_t* value);
int StrToInt64Strict(const char* s, size_t slen, int64_t* value);
int StringMatch(const char* pattern, uint64_t pattern_len, const char* string, uint64_t string_len, int nocase);
int StrToLongDouble(const char* s, size_t slen, long double* ldval);
int LongDoubleToStr(long double ldval, std::string* value);
Expand Down
2 changes: 1 addition & 1 deletion src/storage/src/redis_hashes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ Status Redis::HIncrby(const Slice& key, const Slice& field, int64_t value, int64
ParsedBaseDataValue parsed_internal_value(&old_value);
parsed_internal_value.StripSuffix();
int64_t ival = 0;
if (StrToInt64(old_value.data(), old_value.size(), &ival) == 0) {
if (StrToInt64Strict(old_value.data(), old_value.size(), &ival) == 0) {
return Status::Corruption("hash value is not an integer");
}
if ((value >= 0 && LLONG_MAX - value < ival) || (value < 0 && LLONG_MIN - value > ival)) {
Expand Down
5 changes: 5 additions & 0 deletions src/storage/src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ int Int64ToStr(char* dst, size_t dstlen, int64_t svalue) { return pstd::Ll2strin
* the parsed value when appropriate. */
int StrToInt64(const char* s, size_t slen, int64_t* value) { return pstd::String2int(s, slen, value); }

/* Convert a string into a long long. Returns 1 if all char of string could be parsed
* into a (non-overflowing) long long, 0 otherwise. The value will be set to
* the parsed value when appropriate. */
int StrToInt64Strict(const char* s, size_t slen, int64_t* value) { return pstd::String2intStrict(s, slen, value); }

/* Glob-style pattern matching. */
int StringMatch(const char* pattern, uint64_t pattern_len, const char* str, uint64_t string_len, int nocase) {
return pstd::StringMatchLen(pattern, static_cast<int32_t>(pattern_len), str, static_cast<int32_t>(string_len),
Expand Down

0 comments on commit 4079e97

Please sign in to comment.