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 all weird out parameters int *ret in the codebase #1479

Merged
merged 15 commits into from
May 31, 2023

Conversation

jihuayu
Copy link
Member

@jihuayu jihuayu commented May 30, 2023

I have followed the rules below and completed all the modifications.

#1451 (comment)

The files match types/redis_*.h

  • For the function similar to Set#IsMember or String#SetNX that ret means the flag, I will change it to bool *flag

  • For the function similar to Set#Remove that ret means the number of removed elements, I will change it to uint64_t *removed_cnt

  • For the function similar to List#Size that ret means the size of list, I will change it to uint64_t *size

  • For the function similar to List#Size that ret means the size of list, I will change it to uint64_t *size

  • For the function similar to String#IncrBy that ret means the new value, I will change it to int64_t *new_value

  • In cases where ret may be negative, I will set the type to int*.

The files match commands/cmd_*.cc

I will change the type of int ret to the correct type, but I will not change the variable name because it represents the return value of the Redis command.

The test files

I will change the type of int ret to the correct type, and I will decide whether to modify the variable name based on the situation.

All files

I will remove all unnecessary static_cast statements.

I will use uint64_t(Metadata#size type) as the type for length everywhere, instead of the size_t.

@git-hulk git-hulk requested review from torwig, PragmaTwice and git-hulk and removed request for torwig May 30, 2023 11:39
@torwig
Copy link
Contributor

torwig commented May 30, 2023

@jihuayu In CommandSIsMember you changed int to bool which is good. However, the line *output = redis::Integer(ret); means the boolean value is implicitly converted to an integer value. Am I right?

@torwig
Copy link
Contributor

torwig commented May 30, 2023

@jihuayu Please, have a look at String::CAD where flag is of type *int and inside a function, you assign boolean and integer values to the flag variable. The first issue is that it's not consistent. The second one is, again, an implicit conversion between an integer value and a boolean value.

@jihuayu
Copy link
Member Author

jihuayu commented May 30, 2023

@jihuayu Please, have a look at String::CAD where flag is of type *int and inside a function, you assign boolean and integer values to the flag variable. The first issue is that it's not consistent. The second one is, again, an implicit conversion between an integer value and a boolean value.

I will fix it.

@jihuayu In CommandSIsMember you changed int to bool which is good. However, the line *output = redis::Integer(ret); means the boolean value is implicitly converted to an integer value. Am I right?

@torwig Thank you for bringing this to my attention. While it is true that bool is a integral types that can be passed to the Integer function, there is no std::to_string function for bool parameters. This could potentially lead to implicit type conversion.

There are two possible solutions to the current issue:

  1. Change *output = redis::Integer(ret) to *output = redis::Integer(static_cast<int>(ret)) in order to prevent implicit type conversion.
  2. Add a new Integer function to handle cases where the parameter type is bool.
std::string Integer(bool data) { return Integer(data ? 1 : 0); }

Personally, I am inclined towards the second solution. Do you have any other suggestions?

@git-hulk
Copy link
Member

@jihuayu How about adding the redis::Bool function to convert the boolean value to RESP?

@jihuayu
Copy link
Member Author

jihuayu commented May 30, 2023

@git-hulk
I don't think using redis::Bool is appropriate.
The meaning of redis::Integer function is to return a Integer RESP, not process a Integer.
Even though we are passing in a bool here, we need to return a Integer RESP, so I think redis::Integer function is more appropriate.

@torwig
Copy link
Contributor

torwig commented May 30, 2023

@jihuayu I think instead of adding a new function we can simply write *output = redis::Integer(ret ? 1 : 0); where ret is boolean.

@jihuayu
Copy link
Member Author

jihuayu commented May 30, 2023

@torwig Sounds good to me too.
There are only 4 places where ret is boolean. Maybe we don't need to add an new function for these 4 calls. Keeping it simple is also a good idea.
I will change it to *output = redis::Integer(ret ? 1 : 0);

@jihuayu
Copy link
Member Author

jihuayu commented May 30, 2023

But now there's another issue. If someone passes a bool type arguments to the redis::Integer function, our clang-tidy tool won't be able to find it.
Shouldn't we modify redis::Integer function to prevent others from passing bool type arguments?

@torwig
Copy link
Contributor

torwig commented May 30, 2023

@jihuayu Perhaps, we can add readability-implicit-bool-conversion to the clang-tidy list of checks with the AllowPointerConditions options set to true. However, it may be the topic of another PR if other contributors think it's a good idea because adding this check needs refactoring some code.

Copy link
Contributor

@torwig torwig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, LGTM.
Good job, @jihuayu !

@git-hulk
Copy link
Member

git-hulk commented May 30, 2023

@git-hulk I don't think using redis::Bool is appropriate. The meaning of redis::Integer function is to return a Integer RESP, not process a Integer. Even though we are passing in a bool here, we need to return a Integer RESP, so I think redis::Integer function is more appropriate.

@jihuayu We already have redis::Integer function now, what I mean is to add redis::Bool based on the Integer function like below:

std::string Bool(b bool) {
  return redis::Integer(b ? 1 : 0)
}

@PragmaTwice
Copy link
Member

PragmaTwice commented May 30, 2023

@git-hulk I don't think using redis::Bool is appropriate. The meaning of redis::Integer function is to return a Integer RESP, not process a Integer. Even though we are passing in a bool here, we need to return a Integer RESP, so I think redis::Integer function is more appropriate.

@jihuayu We already have redis::Integer function now, what I mean is to add redis::Bool based on the Integer function like below:

std::string Bool(b bool) {
  return redis::Integer(b ? 1 : 0)
}

I think function name Bool is not concrete here. The returned string is an integer string, like 1, 0 rather than true false.
The current solution LGTM.

@git-hulk
Copy link
Member

@git-hulk I don't think using redis::Bool is appropriate. The meaning of redis::Integer function is to return a Integer RESP, not process a Integer. Even though we are passing in a bool here, we need to return a Integer RESP, so I think redis::Integer function is more appropriate.

@jihuayu We already have redis::Integer function now, what I mean is to add redis::Bool based on the Integer function like below:

std::string Bool(b bool) {
  return redis::Integer(b ? 1 : 0)
}

I think function name Bool is not concrete here. The returned string is an integer string, like 1, 0 rather than true false. The current solution LGTM.

The current solution is good for me. My point is from the reply API, we indeed have the boolean type though it will convert to the integer string in the underly.

@jihuayu
Copy link
Member Author

jihuayu commented May 31, 2023

Hello, I have fixed all mentioned above. Is there anything else that needs to be corrected?

@git-hulk git-hulk merged commit 4218a2b into apache:unstable May 31, 2023
@git-hulk
Copy link
Member

@jihuayu Thanks for your contribution.

@jihuayu jihuayu deleted the pr-1 branch May 31, 2023 05:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants