Skip to content

Fix RULE-7-0-4 false positives for non-built-in operators - #1178

Open
castler wants to merge 1 commit into
github:mainfrom
castler:fix_7_0_4_overloaded_operators_fp
Open

Fix RULE-7-0-4 false positives for non-built-in operators#1178
castler wants to merge 1 commit into
github:mainfrom
castler:fix_7_0_4_overloaded_operators_fp

Conversation

@castler

@castler castler commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Description

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 FunctionCalls 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 #1177

Change request type

  • Release or process automation (GitHub workflows, internal scripts)
  • Internal documentation
  • External documentation
  • Query files (.ql, .qll, .qls or unit tests)
  • External scripts (analysis report or other code shipped as part of a release)

Rules with added or modified queries

  • No rules added
  • Queries have been added for the following rules:
    • rule number here
  • Queries have been modified for the following rules:
    • RULE-7-0-4

Release change checklist

A change note (development_handbook.md#change-notes) is required for any pull request which modifies:

  • The structure or layout of the release artifacts.
  • The evaluation performance (memory, execution time) of an existing query.
  • The results of an existing query in any circumstance.

If you are only adding new rule queries, a change note is not required.

Author: Is a change note required?

  • Yes
  • No

🚨🚨🚨
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.

  • Confirmed

Reviewer: Confirm that either a change note is not required or the change note is required and has been added.

  • Confirmed

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

  • Have all the relevant rule package description files been checked in?
  • Have you verified that the metadata properties of each new query is set appropriately?
  • Do all the unit tests contain both "COMPLIANT" and "NON_COMPLIANT" cases?
  • Are the alert messages properly formatted and consistent with the style guide?
  • Have you run the queries on OpenPilot and verified that the performance and results are acceptable?
    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.
  • Does the query have an appropriate level of in-query comments/documentation?
  • Have you considered/identified possible edge cases?
  • Does the query not reinvent features in the standard library?
  • Can the query be simplified further (not golfed!)

Reviewer

  • Have all the relevant rule package description files been checked in?
  • Have you verified that the metadata properties of each new query is set appropriately?
  • Do all the unit tests contain both "COMPLIANT" and "NON_COMPLIANT" cases?
  • Are the alert messages properly formatted and consistent with the style guide?
  • Have you run the queries on OpenPilot and verified that the performance and results are acceptable?
    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.
  • Does the query have an appropriate level of in-query comments/documentation?
  • Have you considered/identified possible edge cases?
  • Does the query not reinvent features in the standard library?
  • Can the query be simplified further (not golfed!)

`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
@MichaelRFairhurst

Copy link
Copy Markdown
Collaborator

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 not thing.isFromUninstantiatedTemplate(_), and I think that would make sense for this. Technically this may mean we don't report warnings in uninstantiated template code where we otherwise could have...but uninstantiated template code analysis is generally tricky to get right, as you've seen here.

Adding not x.isFromUninstantiatedTemplate(_) seems to fix most FP cases. The remaining FPs from here I see in my databases are:

  • decltype(declval<std::ostream &>() << std::declval<T const &>()) for some reason still is a BinaryShiftExpr
  • unsigned __int128 shifts
  • unscoped enums without explicit types e.g. enum e { ... }
  • char32_t

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 isBuiltInOperandType, I think we should prefer to use expr instanceof MisraCpp23BuiltInTypes::MisraBuiltInType, which should handle all the various cases well related to enums, typedefs, decltypes, references, specifiers, etc.

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 not ...::isUnsignedType(type) instead of ...::isSignedType(type) (without the not). For something like a user defined class, ...::is[Un]SignedType(classType) is false, and therefore not ...::is[Un]SignedType(classType) is always true, and that's a wider net than we want to cast.

So the query could then look something like:

from Expr x, string message
where
  not isExcluded(x, ...) and
  not x.isFromUninstantiatedTemplate(_) and
  x instanceof MisraCpp23BuiltinTypes::MisraBuiltInType and

This would only leaves FPs for:

  • char32_t
  • unsigned __int128

The char32_t FP is caused by a bug in MisraCpp23BuiltInTypes::NumericTypes::getSignedness() -- we should have also added getSignedness() to CharacterType.

Alternatively, instead of x instanceof MisraCpp23BuiltinType::MisraBuiltInType, you could update everything to:

   ...
   MisraCpp23BiltInTypes::isSignedType(opType) and
   ...

This would fix the char32_t case by excluding them from analysis...which is sort of incidentally correct because the char_x_ts are always unsigned.

If my analysis seems right to you, and I haven't missed anything, then feel free to solve with whatever approach resonates with you!

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.

RULE-7-0-4: false positives on overloaded operator<</operator>> in uninstantiated template bodies

2 participants