-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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: Fix Ctest on String #1782
fix: Fix Ctest on String #1782
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -468,11 +468,11 @@ Status RedisStrings::Incrby(const Slice& key, int64_t value, int64_t* ret) { | |
std::string new_value; | ||
ScopeRecordLock l(lock_mgr_, key); | ||
Status s = db_->Get(default_read_options_, key, &old_value); | ||
char buf[32] = {0}; | ||
if (s.ok()) { | ||
ParsedStringsValue parsed_strings_value(&old_value); | ||
if (parsed_strings_value.IsStale()) { | ||
*ret = value; | ||
char buf[32]; | ||
Int64ToStr(buf, 32, value); | ||
StringsValue strings_value(buf); | ||
return db_->Put(default_write_options_, key, strings_value.Encode()); | ||
|
@@ -495,7 +495,6 @@ Status RedisStrings::Incrby(const Slice& key, int64_t value, int64_t* ret) { | |
} | ||
} else if (s.IsNotFound()) { | ||
*ret = value; | ||
char buf[32]; | ||
Int64ToStr(buf, 32, value); | ||
StringsValue strings_value(buf); | ||
return db_->Put(default_write_options_, key, strings_value.Encode()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here are some observations and recommendations for the code patch you provided:
Overall, the provided code patch seems to have areas for improvement, such as addressing code duplication, error handling, and ensuring proper variable scoping. |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here are some observations and suggestions for the code patch:
The code initializes
buf
to zero at the beginning of the function:char buf[32] = {0};
. Later, there is another initialization ofbuf
without the zeroing:char buf[32];
. It would be cleaner to remove the first initialization since it is not needed.Considering that the variable
buf
is used multiple times within the function, it might be better to declare it at the top of the block, before theif (s.ok())
condition. This way, you don't need to repeat its declaration in each branch of theif
statement.In the else-if block (
else if (s.IsNotFound())
), the code repeats the same logic for setting the value ofbuf
and creating aStringsValue
object. You can avoid this repetition by moving those lines outside theif-else
block and using them after the conditional branches.It's important to verify if the integer-to-string conversion
Int64ToStr
will always produce a string representation that fits within the 32-character buffer. If there is a possibility of the converted string exceeding 32 characters, it could lead to buffer overflow issues. Make sure to handle such cases appropriately.Consider adding error handling for the database
Put
operation returns an error. It's good practice to handle errors and propagate them upwards so that the caller can be aware of any failures.Overall, the code patch seems reasonable with these suggested improvements. However, a comprehensive review would require more context and understanding of the surrounding code and requirements.