You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This rule finds relational comparisons between the result of an unsigned subtraction and the value 0. Such comparisons are likely to be wrong as the value of an unsigned subtraction can never be negative. So the relational comparison ends up checking whether the result of the subtraction is equal to 0. This is probably not what the programmer intended.
Fix the issue, we need to ensure that the subtraction operation does not underflow. The best approach is to cast the unsigned values to a signed type (e.g., int64_t) before performing the subtraction. This ensures that the result of the subtraction can represent negative values if lastDeletedTenants.size() is greater than idx2Tid.size(). The comparison will then behave as intended.
The specific change involves modifying the expression idx2Tid.size() - lastDeletedTenants.size() > 0 to (int64_t)idx2Tid.size() - (int64_t)lastDeletedTenants.size() > 0.
uint32_t limit = get_limit();
uint32_t total = 0;
while (limit - total > 0) { // BAD: if `total` is greater than `limit` this will underflow and continue executing the loop.
total += get_data();
}
while (total < limit) { // GOOD: never underflows here because there is no arithmetic.
total += get_data();
}
while ((int64_t)limit - total > 0) { // GOOD: never underflows here because the result always fits in an `int64_t`.
total += get_data();
}
The general pull request guidelines can be found here.
Please check each of the following things and check all boxes before accepting a PR.
The PR has a description, explaining both the problem and the solution.
The description mentions which forms of testing were done and the testing seems reasonable.
Every function/class/actor that was touched is reasonably well documented.
For Release-Branches
If this PR is made against a release-branch, please also check the following:
This change/bugfix is a cherry-pick from the next younger branch (younger release-branch or main if this is the youngest branch)
There is a good reason why this PR needs to go into a release branch and this reason is documented (either in the description above or in a linked GitHub issue)
Appreciate your contribution! It is always not-so-nice to do substractions over unsigned variables. And I am certain this function is problematic. However, this patch is assuming that the value of idx2Tid.size() and lastDeletedTenants.size() are both smaller than std::numeric_limits::int64_t::max(). This may be true or may not be true. So I would recommend a fix looks like
idx2Tid.size() > lastDeletedTenants.size()
and the assumption will be unnecessary in this case. Would you mind update the patch? Thanks a lot!
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
foundationdb/fdbserver/workloads/RawTenantAccessWorkload.actor.cpp
Line 90 in c289917
This rule finds relational comparisons between the result of an unsigned subtraction and the value
0
. Such comparisons are likely to be wrong as the value of an unsigned subtraction can never be negative. So the relational comparison ends up checking whether the result of the subtraction is equal to0
. This is probably not what the programmer intended.Fix the issue, we need to ensure that the subtraction operation does not underflow. The best approach is to cast the unsigned values to a signed type (e.g.,
int64_t
) before performing the subtraction. This ensures that the result of the subtraction can represent negative values iflastDeletedTenants.size()
is greater thanidx2Tid.size()
. The comparison will then behave as intended.The specific change involves modifying the expression
idx2Tid.size() - lastDeletedTenants.size() > 0
to(int64_t)idx2Tid.size() - (int64_t)lastDeletedTenants.size() > 0
.INT02-C. Understand integer conversion rules
Code-Reviewer Section
The general pull request guidelines can be found here.
Please check each of the following things and check all boxes before accepting a PR.
For Release-Branches
If this PR is made against a release-branch, please also check the following:
release-branch
ormain
if this is the youngest branch)