Skip to content

Commit 7a9d341

Browse files
authored
Merge pull request github#18416 from MathiasVP/more-robust-param-name-matching-arrays
C++: Also resolve typedefs nested inside `ArrayType`s
2 parents 0c2e057 + d935e9f commit 7a9d341

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

cpp/ql/lib/semmle/code/cpp/dataflow/ExternalFlow.qll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,8 +529,21 @@ private string getTypeName(Type t, boolean needsSpace) {
529529
needsSpace = false and
530530
(if needsSpace0 = true then result = s + " *" else result = s + "*")
531531
or
532+
// We don't need to check for `needsSpace0` here because the type of
533+
// `x` in `int x[1024]` is formatted without a space between the bracket
534+
// and the `int` by `Type.getName`. That is, calling `Type.getName` on
535+
// the type of `x` gives `int[1024]` and not `int [1024]`.
536+
needsSpace = false and
537+
exists(ArrayType array | array = dt |
538+
result = s + "[" + array.getArraySize() + "]"
539+
or
540+
not array.hasArraySize() and
541+
result = s + "[]"
542+
)
543+
or
532544
not dt instanceof ReferenceType and
533545
not dt instanceof PointerType and
546+
not dt instanceof ArrayType and
534547
result = s and
535548
needsSpace = needsSpace0
536549
)

cpp/ql/test/library-tests/dataflow/models-as-data/FlowSummaryNode.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,5 @@
7676
| tests.cpp:437:5:437:36 | [summary] to write: ReturnValue in madCallReturnValueIgnoreFunction | ReturnNode | madCallReturnValueIgnoreFunction | madCallReturnValueIgnoreFunction |
7777
| tests.cpp:459:5:459:31 | [summary param] *0 in parameter_ref_to_return_ref | ParameterNode | parameter_ref_to_return_ref | parameter_ref_to_return_ref |
7878
| tests.cpp:459:5:459:31 | [summary] to write: ReturnValue[*] in parameter_ref_to_return_ref | ReturnNode | parameter_ref_to_return_ref | parameter_ref_to_return_ref |
79+
| tests.cpp:471:5:471:17 | [summary param] *0 in receive_array | ParameterNode | receive_array | receive_array |
80+
| tests.cpp:471:5:471:17 | [summary] to write: ReturnValue in receive_array | ReturnNode | receive_array | receive_array |

cpp/ql/test/library-tests/dataflow/models-as-data/SummaryCall.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ summarizedCallables
3030
| tests.cpp:436:6:436:25 | madCallArg0WithValue |
3131
| tests.cpp:437:5:437:36 | madCallReturnValueIgnoreFunction |
3232
| tests.cpp:459:5:459:31 | parameter_ref_to_return_ref |
33+
| tests.cpp:471:5:471:17 | receive_array |
3334
sourceCallables
3435
| tests.cpp:3:5:3:10 | source |
3536
| tests.cpp:4:6:4:14 | sourcePtr |
@@ -230,3 +231,9 @@ sourceCallables
230231
| tests.cpp:463:6:463:6 | x |
231232
| tests.cpp:464:36:464:36 | s |
232233
| tests.cpp:465:6:465:6 | y |
234+
| tests.cpp:469:7:469:9 | INT |
235+
| tests.cpp:471:23:471:23 | a |
236+
| tests.cpp:473:6:473:23 | test_receive_array |
237+
| tests.cpp:474:6:474:6 | x |
238+
| tests.cpp:475:6:475:10 | array |
239+
| tests.cpp:476:6:476:6 | y |

cpp/ql/test/library-tests/dataflow/models-as-data/testModels.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ private class TestSummaries extends SummaryModelCsv {
9898
";;false;madCallArg0ReturnToReturnFirst;;;Argument[0].ReturnValue;ReturnValue.Field[first];value",
9999
";;false;madCallArg0WithValue;;;Argument[1];Argument[0].Parameter[0];value",
100100
";;false;madCallReturnValueIgnoreFunction;;;Argument[1];ReturnValue;value",
101-
";StructWithTypedefInParameter<T>;true;parameter_ref_to_return_ref;(const T &);;Argument[*0];ReturnValue[*];value"
101+
";StructWithTypedefInParameter<T>;true;parameter_ref_to_return_ref;(const T &);;Argument[*0];ReturnValue[*];value",
102+
";;false;receive_array;(int[20]);;Argument[*0];ReturnValue;taint"
102103
]
103104
}
104105
}

cpp/ql/test/library-tests/dataflow/models-as-data/tests.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,4 +464,15 @@ void test_parameter_ref_to_return_ref() {
464464
StructWithTypedefInParameter<int> s;
465465
int y = s.parameter_ref_to_return_ref(x);
466466
sink(y); // $ ir
467+
}
468+
469+
using INT = int;
470+
471+
int receive_array(INT a[20]); // $ interpretElement
472+
473+
void test_receive_array() {
474+
int x = source();
475+
int array[10] = {x};
476+
int y = receive_array(array);
477+
sink(y); // $ ir
467478
}

0 commit comments

Comments
 (0)