Repository navigation
C++: Fix multiple parameter types in IR - #23165
Conversation
6e77b20 to
7f14962
Compare
There was a problem hiding this comment.
🔵 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)
| // 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); |
There was a problem hiding this comment.
Are all of these legal? As in: are the definition and declaration actually allowed to differ this much (e.g, wrt. const).
There was a problem hiding this comment.
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")
A parameter can have multiple types (e.g., one for its declaration, and another for its definition). As a result, the
getVariableTypepredicate (which is used to assign a type for an IR variable) could also have multiple types. This resulted in invalid IR.