Skip to content

Commit 4ec0ae6

Browse files
authored
Merge pull request github#3388 from geoffw0/cleanupstuff
C++: Small tidy up
2 parents 62c7387 + 9b4884d commit 4ec0ae6

File tree

4 files changed

+26
-27
lines changed

4 files changed

+26
-27
lines changed

cpp/ql/src/Security/CWE/CWE-468/IncorrectPointerScaling.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ where
2121
destBase = baseType(destType) and
2222
destBase.getSize() != sourceBase.getSize() and
2323
not dest.isInMacroExpansion() and
24-
// If the source type is a char* or void* then don't
24+
// If the source type is a `char*` or `void*` then don't
2525
// produce a result, because it is likely to be a false
2626
// positive.
2727
not sourceBase instanceof CharType and

cpp/ql/src/Security/CWE/CWE-468/IncorrectPointerScalingChar.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ where
2121
destBase = baseType(destType) and
2222
destBase.getSize() != sourceBase.getSize() and
2323
not dest.isInMacroExpansion() and
24-
// If the source type is a char* or void* then don't
24+
// If the source type is a `char*` or `void*` then don't
2525
// produce a result, because it is likely to be a false
2626
// positive.
2727
not sourceBase instanceof CharType and

cpp/ql/src/Security/CWE/CWE-468/SuspiciousAddWithSizeof.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ private predicate isCharSzPtrExpr(Expr e) {
2424
from Expr sizeofExpr, Expr e
2525
where
2626
// If we see an addWithSizeof then we expect the type of
27-
// the pointer expression to be char* or void*. Otherwise it
27+
// the pointer expression to be `char*` or `void*`. Otherwise it
2828
// is probably a mistake.
2929
addWithSizeof(e, sizeofExpr, _) and not isCharSzPtrExpr(e)
3030
select sizeofExpr,

cpp/ql/src/semmle/code/cpp/models/implementations/Allocation.qll

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -271,33 +271,32 @@ class OperatorNewAllocationFunction extends AllocationFunction {
271271
}
272272

273273
/**
274-
* The predicate analyzes a `sizeExpr`, which is an argument to an allocation
275-
* function like malloc, and tries to split it into an expression `lengthExpr`
276-
* that describes the length of the allocated array, and the size of the allocated
277-
* element type `sizeof`.
278-
* If this is not possible, the allocation is considered to be of size 1 and of
279-
* length `sizeExpr`.
274+
* Holds if `sizeExpr` is an expression consisting of a subexpression
275+
* `lengthExpr` multiplied by a constant `sizeof` that is the result of a
276+
* `sizeof()` expression. Alternatively if there isn't a suitable `sizeof()`
277+
* expression, `lengthExpr = sizeExpr` and `sizeof = 1`. For example:
278+
* ```
279+
* malloc(a * 2 * sizeof(char32_t));
280+
* ```
281+
* In this case if the `sizeExpr` is the argument to `malloc`, the `lengthExpr`
282+
* is `a * 2` and `sizeof` is `4`.
280283
*/
281284
private predicate deconstructSizeExpr(Expr sizeExpr, Expr lengthExpr, int sizeof) {
282-
if
283-
sizeExpr instanceof MulExpr and
284-
exists(SizeofOperator sizeofOp, Expr lengthOp |
285-
sizeofOp = sizeExpr.(MulExpr).getAnOperand() and
286-
lengthOp = sizeExpr.(MulExpr).getAnOperand() and
287-
not lengthOp instanceof SizeofOperator and
288-
exists(sizeofOp.getValue().toInt())
289-
)
290-
then
291-
exists(SizeofOperator sizeofOp |
292-
sizeofOp = sizeExpr.(MulExpr).getAnOperand() and
293-
lengthExpr = sizeExpr.(MulExpr).getAnOperand() and
294-
not lengthExpr instanceof SizeofOperator and
295-
sizeof = sizeofOp.getValue().toInt()
296-
)
297-
else (
298-
lengthExpr = sizeExpr and
299-
sizeof = 1
285+
exists(SizeofOperator sizeofOp |
286+
sizeofOp = sizeExpr.(MulExpr).getAnOperand() and
287+
lengthExpr = sizeExpr.(MulExpr).getAnOperand() and
288+
not lengthExpr instanceof SizeofOperator and
289+
sizeof = sizeofOp.getValue().toInt()
300290
)
291+
or
292+
not exists(SizeofOperator sizeofOp, Expr lengthOp |
293+
sizeofOp = sizeExpr.(MulExpr).getAnOperand() and
294+
lengthOp = sizeExpr.(MulExpr).getAnOperand() and
295+
not lengthOp instanceof SizeofOperator and
296+
exists(sizeofOp.getValue().toInt())
297+
) and
298+
lengthExpr = sizeExpr and
299+
sizeof = 1
301300
}
302301

303302
/**

0 commit comments

Comments
 (0)