Skip to content

Commit 695df23

Browse files
committed
CPP: Equalize the definitions of 'baseType'.
1 parent 373075e commit 695df23

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@ import semmle.code.cpp.controlflow.SSA
1414
import IncorrectPointerScalingCommon
1515

1616
private Type baseType(Type t) {
17-
exists (DerivedType dt
18-
| dt = t.getUnspecifiedType() and
19-
isPointerType(dt) and
20-
result = dt.getBaseType().getUnspecifiedType())
21-
22-
// Make sure that the type has a size and that it isn't ambiguous.
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.
2331
and strictcount(result.getSize()) = 1
2432
}
2533

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@ import semmle.code.cpp.controlflow.SSA
1414
import IncorrectPointerScalingCommon
1515

1616
private Type baseType(Type t) {
17-
exists (DerivedType dt
18-
| dt = t.getUnspecifiedType() and
19-
isPointerType(dt) and
20-
result = dt.getBaseType().getUnspecifiedType())
21-
22-
// Make sure that the type has a size and that it isn't ambiguous.
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.
2331
and strictcount(result.getSize()) = 1
2432
}
2533

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
| test.cpp:13:19:13:29 | charPointer | This pointer might have type $@ (size 4), but the pointer arithmetic here is done with type char * (size 1). | test.cpp:10:31:10:38 | test.cpp:10:31:10:38 | int |
22
| test.cpp:77:17:77:17 | x | This pointer might have type $@ (size 4), but the pointer arithmetic here is done with type char * (size 1). | test.cpp:72:19:72:19 | test.cpp:72:19:72:19 | int |
33
| test.cpp:119:26:119:26 | p | This pointer might have type $@ (size 8), but the pointer arithmetic here is done with type char * (size 1). | test.cpp:114:22:114:22 | test.cpp:114:22:114:22 | mystruct |
4-
| test.cpp:147:19:147:29 | charPointer | This pointer might have type $@ (size 8), but the pointer arithmetic here is done with type char * (size 1). | test.cpp:145:31:145:38 | test.cpp:145:31:145:38 | int[2] |
4+
| test.cpp:147:19:147:29 | charPointer | This pointer might have type $@ (size 4), but the pointer arithmetic here is done with type char * (size 1). | test.cpp:145:31:145:38 | test.cpp:145:31:145:38 | int |

0 commit comments

Comments
 (0)