Skip to content

Commit 40fb6f9

Browse files
authored
Merge branch 'main' into swift/extract-mainactor
2 parents 893ec33 + cca0722 commit 40fb6f9

File tree

379 files changed

+6136
-3569
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

379 files changed

+6136
-3569
lines changed

cpp/ql/lib/semmle/code/cpp/models/interfaces/Allocation.qll

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,38 @@ import semmle.code.cpp.Function
1212
import semmle.code.cpp.models.Models
1313

1414
/**
15-
* An allocation function such as `malloc`.
15+
* An allocation expression such as call to `malloc` or a `new` expression.
1616
*/
17-
abstract class AllocationFunction extends Function {
17+
abstract class AllocationExpr extends Expr {
1818
/**
19-
* Gets the index of the argument for the allocation size, if any. The actual
20-
* allocation size is the value of this argument multiplied by the result of
19+
* Gets an expression for the allocation size, if any. The actual allocation
20+
* size is the value of this expression multiplied by the result of
2121
* `getSizeMult()`, in bytes.
2222
*/
23-
int getSizeArg() { none() }
23+
Expr getSizeExpr() { none() }
2424

2525
/**
26-
* Gets the index of an argument that multiplies the allocation size given by
27-
* `getSizeArg`, if any.
26+
* Gets a constant multiplier for the allocation size given by `getSizeExpr`,
27+
* in bytes.
2828
*/
2929
int getSizeMult() { none() }
3030

3131
/**
32-
* Gets the index of the input pointer argument to be reallocated, if this
33-
* is a `realloc` function.
32+
* Gets the size of this allocation in bytes, if it is a fixed size and that
33+
* size can be determined.
3434
*/
35-
int getReallocPtrArg() { none() }
35+
int getSizeBytes() { none() }
36+
37+
/**
38+
* Gets the expression for the input pointer argument to be reallocated, if
39+
* this is a `realloc` function.
40+
*/
41+
Expr getReallocPtr() { none() }
42+
43+
/**
44+
* Gets the type of the elements that are allocated, if it can be determined.
45+
*/
46+
Type getAllocatedElementType() { none() }
3647

3748
/**
3849
* Whether or not this allocation requires a corresponding deallocation of
@@ -44,38 +55,30 @@ abstract class AllocationFunction extends Function {
4455
}
4556

4657
/**
47-
* An allocation expression such as call to `malloc` or a `new` expression.
58+
* An allocation function such as `malloc`.
59+
*
60+
* Note: `AllocationExpr` includes calls to allocation functions, so prefer
61+
* to use that class unless you specifically need to reason about functions.
4862
*/
49-
abstract class AllocationExpr extends Expr {
63+
abstract class AllocationFunction extends Function {
5064
/**
51-
* Gets an expression for the allocation size, if any. The actual allocation
52-
* size is the value of this expression multiplied by the result of
65+
* Gets the index of the argument for the allocation size, if any. The actual
66+
* allocation size is the value of this argument multiplied by the result of
5367
* `getSizeMult()`, in bytes.
5468
*/
55-
Expr getSizeExpr() { none() }
69+
int getSizeArg() { none() }
5670

5771
/**
58-
* Gets a constant multiplier for the allocation size given by `getSizeExpr`,
59-
* in bytes.
72+
* Gets the index of an argument that multiplies the allocation size given by
73+
* `getSizeArg`, if any.
6074
*/
6175
int getSizeMult() { none() }
6276

6377
/**
64-
* Gets the size of this allocation in bytes, if it is a fixed size and that
65-
* size can be determined.
66-
*/
67-
int getSizeBytes() { none() }
68-
69-
/**
70-
* Gets the expression for the input pointer argument to be reallocated, if
71-
* this is a `realloc` function.
72-
*/
73-
Expr getReallocPtr() { none() }
74-
75-
/**
76-
* Gets the type of the elements that are allocated, if it can be determined.
78+
* Gets the index of the input pointer argument to be reallocated, if this
79+
* is a `realloc` function.
7780
*/
78-
Type getAllocatedElementType() { none() }
81+
int getReallocPtrArg() { none() }
7982

8083
/**
8184
* Whether or not this allocation requires a corresponding deallocation of

cpp/ql/lib/semmle/code/cpp/models/interfaces/Deallocation.qll

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,26 @@ import semmle.code.cpp.Function
1212
import semmle.code.cpp.models.Models
1313

1414
/**
15-
* A deallocation function such as `free`.
15+
* An deallocation expression such as call to `free` or a `delete` expression.
1616
*/
17-
abstract class DeallocationFunction extends Function {
17+
abstract class DeallocationExpr extends Expr {
1818
/**
19-
* Gets the index of the argument that is freed by this function.
19+
* Gets the expression that is freed by this function.
2020
*/
21-
int getFreedArg() { none() }
21+
Expr getFreedExpr() { none() }
2222
}
2323

2424
/**
25-
* An deallocation expression such as call to `free` or a `delete` expression.
25+
* A deallocation function such as `free`.
26+
*
27+
* Note: `DeallocationExpr` includes calls to deallocation functions, so prefer
28+
* to use that class unless you specifically need to reason about functions.
2629
*/
27-
abstract class DeallocationExpr extends Expr {
30+
abstract class DeallocationFunction extends Function {
2831
/**
29-
* Gets the expression that is freed by this function.
32+
* Gets the index of the argument that is freed by this function.
3033
*/
31-
Expr getFreedExpr() { none() }
34+
int getFreedArg() { none() }
3235
}
3336

3437
/**

cpp/ql/src/Critical/MissingCheckScanf.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ BasicBlock blockGuardedBy(int value, string op, ScanfFunctionCall call) {
115115
from ScanfOutput output, ScanfFunctionCall call, Access access
116116
where
117117
output.getCall() = call and
118-
output.hasGuardedAccess(access, false)
118+
output.hasGuardedAccess(access, false) and
119+
not exists(DeallocationExpr dealloc | dealloc.getFreedExpr() = access)
119120
select access,
120121
"This variable is read, but may not have been written. " +
121122
"It should be guarded by a check that the $@ returns at least " +
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The `cpp/missing-check-scanf` query no longer reports the free'ing of `scanf` output variables as potential reads.
Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
| test.cpp:30:7:30:7 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:29:3:29:7 | call to scanf | call to scanf |
2-
| test.cpp:46:7:46:7 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:45:3:45:7 | call to scanf | call to scanf |
3-
| test.cpp:63:7:63:7 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:62:3:62:7 | call to scanf | call to scanf |
4-
| test.cpp:75:7:75:7 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:74:3:74:7 | call to scanf | call to scanf |
5-
| test.cpp:87:7:87:7 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:86:3:86:8 | call to fscanf | call to fscanf |
6-
| test.cpp:94:7:94:7 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:93:3:93:8 | call to sscanf | call to sscanf |
7-
| test.cpp:143:8:143:8 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:141:7:141:11 | call to scanf | call to scanf |
8-
| test.cpp:152:8:152:8 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:150:7:150:11 | call to scanf | call to scanf |
9-
| test.cpp:184:8:184:8 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:183:7:183:11 | call to scanf | call to scanf |
10-
| test.cpp:203:8:203:8 | j | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 2. | test.cpp:200:7:200:11 | call to scanf | call to scanf |
11-
| test.cpp:227:9:227:9 | d | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 2. | test.cpp:225:25:225:29 | call to scanf | call to scanf |
12-
| test.cpp:231:9:231:9 | d | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 2. | test.cpp:229:14:229:18 | call to scanf | call to scanf |
13-
| test.cpp:243:7:243:7 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:242:3:242:7 | call to scanf | call to scanf |
14-
| test.cpp:251:7:251:7 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:250:3:250:7 | call to scanf | call to scanf |
15-
| test.cpp:259:7:259:7 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:258:3:258:7 | call to scanf | call to scanf |
16-
| test.cpp:271:7:271:7 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:270:3:270:7 | call to scanf | call to scanf |
17-
| test.cpp:281:8:281:12 | ptr_i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:280:3:280:7 | call to scanf | call to scanf |
18-
| test.cpp:289:7:289:7 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:288:3:288:7 | call to scanf | call to scanf |
19-
| test.cpp:383:25:383:25 | u | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:382:6:382:11 | call to sscanf | call to sscanf |
1+
| test.cpp:35:7:35:7 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:34:3:34:7 | call to scanf | call to scanf |
2+
| test.cpp:51:7:51:7 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:50:3:50:7 | call to scanf | call to scanf |
3+
| test.cpp:68:7:68:7 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:67:3:67:7 | call to scanf | call to scanf |
4+
| test.cpp:80:7:80:7 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:79:3:79:7 | call to scanf | call to scanf |
5+
| test.cpp:90:8:90:8 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:89:3:89:7 | call to scanf | call to scanf |
6+
| test.cpp:98:8:98:8 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:97:3:97:7 | call to scanf | call to scanf |
7+
| test.cpp:108:7:108:7 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:107:3:107:8 | call to fscanf | call to fscanf |
8+
| test.cpp:115:7:115:7 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:114:3:114:8 | call to sscanf | call to sscanf |
9+
| test.cpp:164:8:164:8 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:162:7:162:11 | call to scanf | call to scanf |
10+
| test.cpp:173:8:173:8 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:171:7:171:11 | call to scanf | call to scanf |
11+
| test.cpp:205:8:205:8 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:204:7:204:11 | call to scanf | call to scanf |
12+
| test.cpp:224:8:224:8 | j | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 2. | test.cpp:221:7:221:11 | call to scanf | call to scanf |
13+
| test.cpp:248:9:248:9 | d | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 2. | test.cpp:246:25:246:29 | call to scanf | call to scanf |
14+
| test.cpp:252:9:252:9 | d | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 2. | test.cpp:250:14:250:18 | call to scanf | call to scanf |
15+
| test.cpp:264:7:264:7 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:263:3:263:7 | call to scanf | call to scanf |
16+
| test.cpp:272:7:272:7 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:271:3:271:7 | call to scanf | call to scanf |
17+
| test.cpp:280:7:280:7 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:279:3:279:7 | call to scanf | call to scanf |
18+
| test.cpp:292:7:292:7 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:291:3:291:7 | call to scanf | call to scanf |
19+
| test.cpp:302:8:302:12 | ptr_i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:301:3:301:7 | call to scanf | call to scanf |
20+
| test.cpp:310:7:310:7 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:309:3:309:7 | call to scanf | call to scanf |
21+
| test.cpp:404:25:404:25 | u | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:403:6:403:11 | call to sscanf | call to sscanf |

cpp/ql/test/query-tests/Critical/MissingCheckScanf/test.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ FILE *get_a_stream();
1919
const char *get_a_string();
2020
extern locale_t get_a_locale();
2121

22+
typedef long size_t;
23+
24+
void *malloc(size_t size);
25+
void free(void *ptr);
26+
2227
int main()
2328
{
2429
// --- simple cases ---
@@ -78,6 +83,22 @@ int main()
7883
use(i); // GOOD
7984
}
8085

86+
{
87+
int *i = (int*)malloc(sizeof(int)); // Allocated variable
88+
89+
scanf("%d", i);
90+
use(*i); // BAD
91+
free(i); // GOOD
92+
}
93+
94+
{
95+
int *i = new int; // Allocated variable
96+
97+
scanf("%d", i);
98+
use(*i); // BAD
99+
delete i; // GOOD
100+
}
101+
81102
// --- different scanf functions ---
82103

83104
{

csharp/ql/lib/ext/Dapper.model.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
extensions:
22
- addsTo:
33
pack: codeql/csharp-all
4-
extensible: extSinkModel
4+
extensible: sinkModel
55
data:
66
- ["Dapper", "SqlMapper", False, "Execute", "(System.Data.IDbConnection,System.String,System.Object,System.Data.IDbTransaction,System.Nullable<System.Int32>,System.Nullable<System.Data.CommandType>)", "", "Argument[1]", "sql", "manual"]
77
- ["Dapper", "SqlMapper", False, "ExecuteAsync", "(System.Data.IDbConnection,System.String,System.Object,System.Data.IDbTransaction,System.Nullable<System.Int32>,System.Nullable<System.Data.CommandType>)", "", "Argument[1]", "sql", "manual"]

csharp/ql/lib/ext/Microsoft.ApplicationBlocks.Data.model.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
extensions:
22
- addsTo:
33
pack: codeql/csharp-all
4-
extensible: extSinkModel
4+
extensible: sinkModel
55
data:
66
- ["Microsoft.ApplicationBlocks.Data", "SqlHelper", False, "ExecuteDataset", "(System.Data.SqlClient.SqlConnection,System.Data.CommandType,System.String)", "", "Argument[2]", "sql", "manual"]
77
- ["Microsoft.ApplicationBlocks.Data", "SqlHelper", False, "ExecuteDataset", "(System.Data.SqlClient.SqlConnection,System.Data.CommandType,System.String,System.Data.SqlClient.SqlParameter[])", "", "Argument[2]", "sql", "manual"]

csharp/ql/lib/ext/Microsoft.EntityFrameworkCore.model.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
extensions:
22
- addsTo:
33
pack: codeql/csharp-all
4-
extensible: extSinkModel
4+
extensible: sinkModel
55
data:
66
- ["Microsoft.EntityFrameworkCore", "RelationalDatabaseFacadeExtensions", False, "ExecuteSqlRaw", "(Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade,System.String,System.Collections.Generic.IEnumerable<System.Object>)", "", "Argument[1]", "sql", "manual"]
77
- ["Microsoft.EntityFrameworkCore", "RelationalDatabaseFacadeExtensions", False, "ExecuteSqlRaw", "(Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade,System.String,System.Object[])", "", "Argument[1]", "sql", "manual"]

csharp/ql/lib/ext/Microsoft.Extensions.Primitives.model.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
extensions:
22
- addsTo:
33
pack: codeql/csharp-all
4-
extensible: extSummaryModel
4+
extensible: summaryModel
55
data:
66
- ["Microsoft.Extensions.Primitives", "StringValues", False, "Add", "(System.String)", "", "Argument[0]", "ReturnValue", "taint", "manual"]
77
- ["Microsoft.Extensions.Primitives", "StringValues", False, "Add", "(System.String)", "", "Argument[this]", "ReturnValue", "taint", "manual"]

0 commit comments

Comments
 (0)