Skip to content

Commit df241ad

Browse files
committed
C++: Fix lambda creation for objects with no constructor.
1 parent b53c354 commit df241ad

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowPrivate.qll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,17 @@ private class OperatorCall extends Cpp::MemberFunction {
14141414
OperatorCall() { this.hasName("operator()") }
14151415
}
14161416

1417+
private predicate isFunctorCreationWithoutConstructor(Node creation, OperatorCall operator) {
1418+
exists(UninitializedInstruction init, Instruction dest |
1419+
// A construction of an object with no constructor. In this case we use
1420+
// the `UninitializedInstruction` as the creation node.
1421+
init = creation.asInstruction() and
1422+
dest = init.getDestinationAddress() and
1423+
not any(ConstructorCallInstruction constructorCall).getThisArgument() = dest and
1424+
operator.getDeclaringType() = init.getResultType()
1425+
)
1426+
}
1427+
14171428
private predicate isFunctorCreationWithConstructor(Node creation, OperatorCall operator) {
14181429
exists(DataFlowCall constructorCall, IndirectionPosition pos |
14191430
// A construction of an object with a constructor. In this case we use
@@ -1432,6 +1443,8 @@ predicate lambdaCreation(Node creation, LambdaCallKind kind, DataFlowCallable c)
14321443
or
14331444
kind.isFunctor() and
14341445
exists(OperatorCall operator | operator = c.asSourceCallable() |
1446+
isFunctorCreationWithoutConstructor(creation, operator)
1447+
or
14351448
isFunctorCreationWithConstructor(creation, operator)
14361449
)
14371450
}

cpp/ql/test/library-tests/dataflow/external-models/flow.expected

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,19 @@ edges
7676
| test.cpp:59:55:59:64 | *& ... [x] | test.cpp:52:5:52:18 | [summary param] *3 in pthread_create [x] | provenance | |
7777
| test.cpp:63:6:63:21 | [summary param] 1 in callWithArgument | test.cpp:63:6:63:21 | [summary] to write: Argument[0].Parameter[0] in callWithArgument | provenance | MaD:23 |
7878
| test.cpp:63:6:63:21 | [summary param] 1 in callWithArgument | test.cpp:63:6:63:21 | [summary] to write: Argument[0].Parameter[0] in callWithArgument | provenance | MaD:23 |
79+
| test.cpp:63:6:63:21 | [summary param] 1 in callWithArgument | test.cpp:63:6:63:21 | [summary] to write: Argument[0].Parameter[0] in callWithArgument | provenance | MaD:23 |
7980
| test.cpp:63:6:63:21 | [summary] to write: Argument[0].Parameter[0] in callWithArgument | test.cpp:68:22:68:22 | y | provenance | |
81+
| test.cpp:63:6:63:21 | [summary] to write: Argument[0].Parameter[0] in callWithArgument | test.cpp:74:22:74:22 | y | provenance | |
8082
| test.cpp:63:6:63:21 | [summary] to write: Argument[0].Parameter[0] in callWithArgument | test.cpp:82:22:82:22 | y | provenance | |
8183
| test.cpp:68:22:68:22 | y | test.cpp:69:11:69:11 | y | provenance | Sink:MaD:1 |
84+
| test.cpp:74:22:74:22 | y | test.cpp:75:11:75:11 | y | provenance | Sink:MaD:1 |
8285
| test.cpp:82:22:82:22 | y | test.cpp:83:11:83:11 | y | provenance | Sink:MaD:1 |
8386
| test.cpp:94:10:94:18 | call to ymlSource | test.cpp:94:10:94:18 | call to ymlSource | provenance | Src:MaD:16 |
8487
| test.cpp:94:10:94:18 | call to ymlSource | test.cpp:97:26:97:26 | x | provenance | |
88+
| test.cpp:94:10:94:18 | call to ymlSource | test.cpp:101:26:101:26 | x | provenance | |
8589
| test.cpp:94:10:94:18 | call to ymlSource | test.cpp:103:63:103:63 | x | provenance | |
8690
| test.cpp:97:26:97:26 | x | test.cpp:63:6:63:21 | [summary param] 1 in callWithArgument | provenance | |
91+
| test.cpp:101:26:101:26 | x | test.cpp:63:6:63:21 | [summary param] 1 in callWithArgument | provenance | |
8792
| test.cpp:103:63:103:63 | x | test.cpp:63:6:63:21 | [summary param] 1 in callWithArgument | provenance | |
8893
| windows.cpp:17:8:17:25 | [summary param] *0 in CommandLineToArgvA | windows.cpp:17:8:17:25 | [summary] to write: ReturnValue[**] in CommandLineToArgvA | provenance | MaD:18 |
8994
| windows.cpp:22:15:22:29 | *call to GetCommandLineA | windows.cpp:22:15:22:29 | *call to GetCommandLineA | provenance | Src:MaD:3 |
@@ -223,15 +228,20 @@ nodes
223228
| test.cpp:59:55:59:64 | *& ... [x] | semmle.label | *& ... [x] |
224229
| test.cpp:63:6:63:21 | [summary param] 1 in callWithArgument | semmle.label | [summary param] 1 in callWithArgument |
225230
| test.cpp:63:6:63:21 | [summary param] 1 in callWithArgument | semmle.label | [summary param] 1 in callWithArgument |
231+
| test.cpp:63:6:63:21 | [summary param] 1 in callWithArgument | semmle.label | [summary param] 1 in callWithArgument |
232+
| test.cpp:63:6:63:21 | [summary] to write: Argument[0].Parameter[0] in callWithArgument | semmle.label | [summary] to write: Argument[0].Parameter[0] in callWithArgument |
226233
| test.cpp:63:6:63:21 | [summary] to write: Argument[0].Parameter[0] in callWithArgument | semmle.label | [summary] to write: Argument[0].Parameter[0] in callWithArgument |
227234
| test.cpp:63:6:63:21 | [summary] to write: Argument[0].Parameter[0] in callWithArgument | semmle.label | [summary] to write: Argument[0].Parameter[0] in callWithArgument |
228235
| test.cpp:68:22:68:22 | y | semmle.label | y |
229236
| test.cpp:69:11:69:11 | y | semmle.label | y |
237+
| test.cpp:74:22:74:22 | y | semmle.label | y |
238+
| test.cpp:75:11:75:11 | y | semmle.label | y |
230239
| test.cpp:82:22:82:22 | y | semmle.label | y |
231240
| test.cpp:83:11:83:11 | y | semmle.label | y |
232241
| test.cpp:94:10:94:18 | call to ymlSource | semmle.label | call to ymlSource |
233242
| test.cpp:94:10:94:18 | call to ymlSource | semmle.label | call to ymlSource |
234243
| test.cpp:97:26:97:26 | x | semmle.label | x |
244+
| test.cpp:101:26:101:26 | x | semmle.label | x |
235245
| test.cpp:103:63:103:63 | x | semmle.label | x |
236246
| windows.cpp:17:8:17:25 | [summary param] *0 in CommandLineToArgvA | semmle.label | [summary param] *0 in CommandLineToArgvA |
237247
| windows.cpp:17:8:17:25 | [summary] to write: ReturnValue[**] in CommandLineToArgvA | semmle.label | [summary] to write: ReturnValue[**] in CommandLineToArgvA |

cpp/ql/test/library-tests/dataflow/external-models/test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ struct StructWithOperatorCall_has_constructor {
7272

7373
struct StructWithOperatorCall_no_constructor {
7474
void operator()(int y) {
75-
ymlSink(y); // $ MISSING: ir
75+
ymlSink(y); // $ ir
7676
}
7777
};
7878

0 commit comments

Comments
 (0)