Skip to content

Commit 73882c9

Browse files
authored
Merge pull request github#3439 from jbj/passesByReference-qualifier
C++: Call qualifiers are passed by reference
2 parents df6abdc + bebd5ae commit 73882c9

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

cpp/ql/src/semmle/code/cpp/exprs/Call.qll

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ abstract class Call extends Expr, NameQualifiableElement {
8282

8383
/**
8484
* Holds if this call passes the variable accessed by `va` by
85-
* reference as the `i`th argument.
85+
* reference as the `i`th argument. The qualifier of a call to a member
86+
* function is `i = -1`.
8687
*
8788
* A variable is passed by reference if the `i`th parameter of the function
8889
* receives an address that points within the object denoted by `va`. For a
@@ -101,11 +102,15 @@ abstract class Call extends Expr, NameQualifiableElement {
101102
*/
102103
predicate passesByReference(int i, VariableAccess va) {
103104
variableAddressEscapesTree(va, this.getArgument(i).getFullyConverted())
105+
or
106+
variableAddressEscapesTree(va, this.getQualifier().getFullyConverted()) and
107+
i = -1
104108
}
105109

106110
/**
107111
* Holds if this call passes the variable accessed by `va` by
108-
* reference to non-const data as the `i`th argument.
112+
* reference to non-const data as the `i`th argument. The qualifier of a
113+
* call to a member function is `i = -1`.
109114
*
110115
* A variable is passed by reference if the `i`th parameter of the function
111116
* receives an address that points within the object denoted by `va`. For a
@@ -124,6 +129,9 @@ abstract class Call extends Expr, NameQualifiableElement {
124129
*/
125130
predicate passesByReferenceNonConst(int i, VariableAccess va) {
126131
variableAddressEscapesTreeNonConst(va, this.getArgument(i).getFullyConverted())
132+
or
133+
variableAddressEscapesTreeNonConst(va, this.getQualifier().getFullyConverted()) and
134+
i = -1
127135
}
128136
}
129137

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
| test.cpp:21:3:21:8 | call to remove | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test.cpp:21:10:21:14 | file1 | filename | test.cpp:19:7:19:12 | call to rename | checked |
2+
| test.cpp:35:3:35:8 | call to remove | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test.cpp:35:10:35:14 | file1 | filename | test.cpp:32:7:32:12 | call to rename | checked |
3+
| test.cpp:49:3:49:8 | call to remove | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test.cpp:49:10:49:14 | file1 | filename | test.cpp:47:7:47:12 | call to rename | checked |

cpp/ql/test/query-tests/Security/CWE/CWE-367/semmle/test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void test2()
3232
if (!rename(file1, file2))
3333
{
3434
file1.set("d.txt");
35-
remove(file1); // GOOD
35+
remove(file1); // GOOD [FALSE POSITIVE]
3636
}
3737
}
3838

@@ -46,6 +46,6 @@ void test3()
4646
create(file1);
4747
if (!rename(file1, file2))
4848
{
49-
remove(file1); // BAD [NOT DETECTED]
49+
remove(file1); // BAD
5050
}
5151
}

0 commit comments

Comments
 (0)