Skip to content

C++: Fix multiple parameter types in IR - #23165

Merged
MathiasVP merged 3 commits into
github:mainfrom
MathiasVP:fix-ir-variable-type
Nov 18, 2026
Merged

MathiasVP merged 3 commits into
github:mainfrom
MathiasVP:fix-ir-variable-type

Conversation

@MathiasVP

Copy link
Copy Markdown
Contributor

A parameter can have multiple types (e.g., one for its declaration, and another for its definition). As a result, the getVariableType predicate (which is used to assign a type for an IR variable) could also have multiple types. This resulted in invalid IR.

@MathiasVP
MathiasVP requested a review from a team as a code owner November 18, 2026 13:16
Copilot AI adaptive review requested due to automatic review settings November 18, 2026 13:16
@MathiasVP MathiasVP added the no-change-note-required This PR does not need a change note label Nov 18, 2026
@MathiasVP
MathiasVP force-pushed the fix-ir-variable-type branch from 6e77b20 to 7f14962 Compare November 18, 2026 13:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Definition-based parameter typing changes IR identities and dataflow provenance across C++ decay, variable construction, and parameter initialization, requiring broader cross-layer semantic review.

No open issues 🎉

What changed in this PR

Fixes invalid C++ intermediate representation (IR) caused by parameters having different types across declarations and definitions. Type selection now prefers the parameter’s definition before applying decay.

Changes:

  • Use definition-specific parameter types while preserving existing handling of other variables.
  • Add regression cases for pointer, array, function, const-qualified, and typedef parameter types.
  • Update unbounded-write expectations to remove redundant paths and nodes.
File Description
cpp/​ql/​lib/​semmle/​code/​cpp/​ir/​internal/​IRUtilities.qll Prefer definition-specific parameter types when constructing IR.
cpp/​ql/​test/​library-tests/​ir/​parameter-types/​test.cpp Add parameter definitions with inline type expectations.
cpp/​ql/​test/​library-tests/​ir/​parameter-types/​declarations.cpp Supply differing declarations for the regression cases.
cpp/​ql/​test/​library-tests/​ir/​parameter-types/​parameter-types.ql Check IR parameter-address types against inline expectations.
cpp/​ql/​test/​library-tests/​ir/​parameter-types/​parameter-types.expected Add the regression test’s expected-output file.
cpp/​ql/​test/​query-tests/​Security/​CWE/​CWE-119/​semmle/​tests/​UnboundedWrite.expected Remove redundant expected paths and nodes after the type fix.

Review effort: Balanced (adaptive)

@github-actions github-actions Bot added the C++ label Nov 18, 2026
Comment on lines +1 to +14
// The parameter is declared as `char *argv[]` in `test.cpp`.
void array_param_with_pointer_declaration(char **argv);

// The parameter is declared as `char **argv` in `test.cpp`.
void pointer_param_with_array_declaration(char *argv[]);

// The parameter is declared as `void f()` in `test.cpp`.
void function_param_with_pointer_declaration(void (*f)());

// The parameter is declared as `const int x` in `test.cpp`.
void const_param_with_non_const_declaration(int x);

// The parameter is declared as `size_t n` in `test.cpp`.
void typedef_param_with_underlying_type_declaration(unsigned long n);

@jketema jketema Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Are all of these legal? As in: are the definition and declaration actually allowed to differ this much (e.g, wrt. const).

@MathiasVP MathiasVP Nov 18, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

My reading of the standard certainly makes me believe there are no violations here. From https://eel.is/c++draft/dcl.fct:

After producing the list of parameter types, any top-level cv-qualifiers modifying a parameter type are deleted when forming the function type

(and prior to that there's also a note about "array" vs "pointer")

@jketema jketema left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@MathiasVP
MathiasVP merged commit fe4820a into github:main Nov 18, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C++ no-change-note-required This PR does not need a change note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants