Skip to content

Commit 44d68a7

Browse files
committed
CPP: Move 'baseType' into IncorrectPointerScalingCommon.qll.
1 parent 695df23 commit 44d68a7

File tree

4 files changed

+22
-54
lines changed

4 files changed

+22
-54
lines changed

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,6 @@ import cpp
1313
import semmle.code.cpp.controlflow.SSA
1414
import IncorrectPointerScalingCommon
1515

16-
private Type baseType(Type t) {
17-
(
18-
exists (PointerType dt
19-
| dt = t.getUnspecifiedType() and
20-
result = dt.getBaseType().getUnspecifiedType()) or
21-
exists (ArrayType at
22-
| at = t.getUnspecifiedType() and
23-
(not at.getBaseType().getUnspecifiedType() instanceof ArrayType) and
24-
result = at.getBaseType().getUnspecifiedType()) or
25-
exists (ArrayType at, ArrayType at2
26-
| at = t.getUnspecifiedType() and
27-
at2 = at.getBaseType().getUnspecifiedType() and
28-
result = baseType(at2))
29-
)
30-
// Make sure that the type has a size and that it isn't ambiguous.
31-
and strictcount(result.getSize()) = 1
32-
}
33-
3416
from Expr dest, Type destType, Type sourceType, Type sourceBase,
3517
Type destBase, Location sourceLoc
3618
where exists(pointerArithmeticParent(dest))

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,6 @@ import cpp
1313
import semmle.code.cpp.controlflow.SSA
1414
import IncorrectPointerScalingCommon
1515

16-
private Type baseType(Type t) {
17-
(
18-
exists (PointerType dt
19-
| dt = t.getUnspecifiedType() and
20-
result = dt.getBaseType().getUnspecifiedType()) or
21-
exists (ArrayType at
22-
| at = t.getUnspecifiedType() and
23-
(not at.getBaseType().getUnspecifiedType() instanceof ArrayType) and
24-
result = at.getBaseType().getUnspecifiedType()) or
25-
exists (ArrayType at, ArrayType at2
26-
| at = t.getUnspecifiedType() and
27-
at2 = at.getBaseType().getUnspecifiedType() and
28-
result = baseType(at2))
29-
)
30-
// Make sure that the type has a size and that it isn't ambiguous.
31-
and strictcount(result.getSize()) = 1
32-
}
33-
3416
from Expr dest, Type destType, Type sourceType, Type sourceBase,
3517
Type destBase, Location sourceLoc
3618
where exists(pointerArithmeticParent(dest))

cpp/ql/src/Security/CWE/CWE-468/IncorrectPointerScalingCommon.qll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,28 @@ predicate isPointerType(Type t) {
5555
t instanceof ArrayType
5656
}
5757

58+
/**
59+
* Gets the base type of a pointer or array type. In the case of an array of
60+
* arrays, the inner base type is returned.
61+
*/
62+
Type baseType(Type t) {
63+
(
64+
exists (PointerType dt
65+
| dt = t.getUnspecifiedType() and
66+
result = dt.getBaseType().getUnspecifiedType()) or
67+
exists (ArrayType at
68+
| at = t.getUnspecifiedType() and
69+
(not at.getBaseType().getUnspecifiedType() instanceof ArrayType) and
70+
result = at.getBaseType().getUnspecifiedType()) or
71+
exists (ArrayType at, ArrayType at2
72+
| at = t.getUnspecifiedType() and
73+
at2 = at.getBaseType().getUnspecifiedType() and
74+
result = baseType(at2))
75+
)
76+
// Make sure that the type has a size and that it isn't ambiguous.
77+
and strictcount(result.getSize()) = 1
78+
}
79+
5880
/**
5981
* Holds if there is a pointer expression with type `sourceType` at
6082
* location `sourceLoc` which might be the source expression for `use`.

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,6 @@ import cpp
1313
import semmle.code.cpp.controlflow.SSA
1414
import IncorrectPointerScalingCommon
1515

16-
private Type baseType(Type t) {
17-
(
18-
exists (PointerType dt
19-
| dt = t.getUnspecifiedType() and
20-
result = dt.getBaseType().getUnspecifiedType()) or
21-
exists (ArrayType at
22-
| at = t.getUnspecifiedType() and
23-
(not at.getBaseType().getUnspecifiedType() instanceof ArrayType) and
24-
result = at.getBaseType().getUnspecifiedType()) or
25-
exists (ArrayType at, ArrayType at2
26-
| at = t.getUnspecifiedType() and
27-
at2 = at.getBaseType().getUnspecifiedType() and
28-
result = baseType(at2))
29-
)
30-
// Make sure that the type has a size and that it isn't ambiguous.
31-
and strictcount(result.getSize()) = 1
32-
}
33-
3416
from Expr dest, Type destType, Type sourceType, Type sourceBase,
3517
Type destBase, Location sourceLoc
3618
where exists(pointerArithmeticParent(dest))

0 commit comments

Comments
 (0)