Sitelet https://github.com/github/codeql-coding-standards/pull/1181
Skip to content

RULE-5-10-1: fix false positives for locals/parameters in permitted std specializations (e.g. std::hash) - #1181

Open
castler wants to merge 1 commit into
github:mainfrom
castler:fix-rule-5-10-1-hash-specialization-reserved-namespace
Open

RULE-5-10-1: fix false positives for locals/parameters in permitted std specializations (e.g. std::hash)#1181
castler wants to merge 1 commit into
github:mainfrom
castler:fix-rule-5-10-1-hash-specialization-reserved-namespace

Conversation

@castler

@castler castler commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Description

RULE-5-10-1 ("User-defined identifiers shall have an appropriate form") reports
Identifier 'X' is defined in reserved namespace. for ordinary, well-formed,
snake_case local variables and function parameters — e.g. value, result,
view, identifier, instance_identifier, skeleton_instance_identifier
whenever they are declared inside the body of an explicit template
specialization that C++ explicitly permits users to add to namespace std,
most commonly std::hash<UserType>::operator():

namespace std
{
template <>
struct hash<score::mw::com::impl::ServiceIdentifierType>
{
    size_t operator()(const score::mw::com::impl::ServiceIdentifierType& value) const noexcept
    //                                                                     ^^^^^ flagged: "Identifier 'value' is defined in reserved namespace."
    {
        return std::hash<std::string_view>{}(value.ToHashString());
    }
};
}  // namespace std

[namespace.std]/[hash.requirements] explicitly permit a program to add a
template specialization for a standard library template (like std::hash) to
namespace std, provided the specialization depends on a user-defined type.
value here is not a new member of namespace std in the sense the rule is
trying to prevent — it is an ordinary parameter name scoped to the function
body, which merely happens to be lexically nested inside namespace std { ... }
because that is where the standard requires the specialization to be written.

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-5-10-1

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!)

…zations

RULE-5-10-1 (poorly-formed identifier) flagged ordinary, well-formed,
snake_case local variables and function parameters (e.g. `value`,
`result`, `view`, `instance_identifier`) with "Identifier 'X' is
defined in reserved namespace." whenever they were declared inside the
body of an explicit template specialization that C++ explicitly
permits users to add to namespace `std`, most commonly
`std::hash<UserType>::operator()`. On the eclipse-score/communication
codebase this produced 66 findings, all inside `std::hash<UserType>`-
style specializations in headers such as handle_type.h,
trace_point_key.h and service_identifier_type.h.

Root cause: `VariableDeclarationEntryIdentifier::getNamespace()` used
`Variable.getNamespace()` directly. For a `Parameter` or
`LocalVariable`, the upstream CodeQL C++ library defines this as the
namespace of the *enclosing function* (see
`codeql/cpp-all/.../Declaration.qll`), which is a reasonable general
notion of "namespace context" but is not what RULE-5-10-1 means by "is
defined in reserved namespace". A block-scope local or parameter is
scoped to the body of its enclosing function; it is not itself a
member of that function's enclosing namespace and therefore cannot
introduce a new name into (or "pollute") a reserved namespace such as
`std`, regardless of which namespace the enclosing function happens to
be declared in. This is exactly the situation for the body of a
permitted `std::hash<UserType>` specialization: the parameter and local
names are ordinary user-chosen identifiers that merely happen to be
lexically nested inside `namespace std { ... }` because the standard
requires the specialization to be written there.

Fix: in `VariableDeclarationEntryIdentifier`, only report a namespace
for variables that are not `LocalScopeVariable` (i.e. not a `Parameter`
or `LocalVariable`). This is a narrow, semantically-motivated scope
check, not a name- or path-based exclusion: any genuinely new
namespace-scope or class-scope member added directly to `std` (a new
global, function, type, or the specialization itself) is still checked
independently via `FunctionDeclarationEntryIdentifier` /
`TypeDeclarationEntryIdentifier`, and remains flagged. A new
NON_COMPLIANT test case (a function declared directly in `namespace
std`) confirms this is unaffected.

Added a regression test reproducing the `std::hash<UserType>`
specialization shape with a parameter and a local both named after
the FP pattern (COMPLIANT), and confirmed with the "teeth" test that
reverting only `Identifiers.qll` while keeping the new test makes it
fail with exactly the two spurious findings. The existing RULE-5-10-1
test suite and the shared Identifiers library test continue to pass.

Validated against the real eclipse-score/communication CodeQL database
(`//score/message_passing //score/mw/com`): "is defined in reserved
namespace" findings for this shape dropped from 66 to 0, while the
one, unrelated "starts with underscore" finding in the same rule is
unchanged. All 66 baseline findings were manually confirmed to be
locals/parameters inside `std::hash<UserType>`-style specializations,
i.e. genuine false positives.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@castler
castler marked this pull request as draft August 24, 2026 19:10
@castler
castler marked this pull request as ready for review August 25, 2026 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.

1 participant