Fix RULE-7-0-4 false positives for non-built-in operators - #1178
Conversation
`InappropriateBitwiseOrShiftOperands.ql` reported operations that do not use the built-in bitwise or shift operators, most notably the stream insertion and extraction operators, e.g. `stream << value`. Resolved calls to a user provided operator are modelled as `FunctionCall`s and therefore never reach this query. The reported operations were instead the syntactic `LShiftExpr`/`RShiftExpr` nodes emitted for uninstantiated template bodies, where the operands are either unresolved (`unknown`, `auto &&`) or are the non-dependent result of an already resolved overload, such as `std::basic_ostream<char, std::char_traits<char>> &`. The built-in bitwise and shift operators are only applicable to integral and unscoped enumeration operands, so an operation with an operand of any other type does not use them. The query now requires both operands to be of such a type before reporting a violation. This does not introduce false negatives for dependent operands, because the template instantiations, in which the operand types are known, are still reported. Fixes github#1177
|
Thank you @castler! I remember seeing a lot of these, but I had filed a deviation and then forgot. Thank you for the fixes! Great analysis of the FP, nicely done, very subtle and good catch! In general, we often use Adding
Your version solves those decltype FPs, which is great. Note for unscoped enums, the standard says to treat them as their explicit underlying type if specified. For unscoped enums with no explicit type they should be ignored and are covered by rule 10.2.3. For This makes me wonder what our query missed, since we're already using that module ourselves... It looks like the general mistake we made in this query is using So the query could then look something like: This would only leaves FPs for:
The char32_t FP is caused by a bug in Alternatively, instead of This would fix the If my analysis seems right to you, and I haven't missed anything, then feel free to solve with whatever approach resonates with you! |
Description
InappropriateBitwiseOrShiftOperands.qlreported operations that do not use the built-in bitwise or shift operators, most notably the stream insertion and extraction operators, e.g.stream << value.Resolved calls to a user provided operator are modelled as
FunctionCalls and therefore never reach this query. The reported operations were instead the syntacticLShiftExpr/RShiftExprnodes emitted for uninstantiated template bodies, where the operands are either unresolved (unknown,auto &&) or are the non-dependent result of an already resolved overload, such asstd::basic_ostream<char, std::char_traits<char>> &.The built-in bitwise and shift operators are only applicable to integral and unscoped enumeration operands, so an operation with an operand of any other type does not use them. The query now requires both operands to be of such a type before reporting a violation.
This does not introduce false negatives for dependent operands, because the template instantiations, in which the operand types are known, are still reported.
Fixes #1177
Change request type
.ql,.qll,.qlsor unit tests)Rules with added or modified queries
Release change checklist
A change note (development_handbook.md#change-notes) is required for any pull request which modifies:
If you are only adding new rule queries, a change note is not required.
Author: Is a change note required?
🚨🚨🚨
Reviewer: Confirm that format of shared queries (not the .qll file, the
.ql file that imports it) is valid by running them within VS Code.
Reviewer: Confirm that either a change note is not required or the change note is required and has been added.
Query development review checklist
For PRs that add new queries or modify existing queries, the following checklist should be completed by both the author and reviewer:
Author
As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process.
Reviewer
As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process.