File tree Expand file tree Collapse file tree 1 file changed +19
-1
lines changed
cpp/ql/src/Security/CWE/CWE-704 Expand file tree Collapse file tree 1 file changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -18,13 +18,31 @@ class WideCharPointerType extends PointerType {
18
18
WideCharPointerType ( ) { this .getBaseType ( ) instanceof WideCharType }
19
19
}
20
20
21
+ /**
22
+ * types that may also be `CharPointerType`, but that are likely used as arbitrary buffers
23
+ */
24
+ class UnlikelyToBeAStringType extends Type {
25
+ UnlikelyToBeAStringType ( ) {
26
+ this .( PointerType ) .getBaseType ( ) .( CharType ) .isUnsigned ( ) or
27
+ this .( PointerType ) .getBaseType ( ) .getName ( ) .toLowerCase ( ) .matches ( "%byte" ) or
28
+ this .getName ( ) .toLowerCase ( ) .matches ( "%byte" ) or
29
+ this .( PointerType ) .getBaseType ( ) .hasName ( "uint8_t" )
30
+ }
31
+ }
32
+
21
33
from Expr e1 , Cast e2
22
34
where
23
35
e2 = e1 .getConversion ( ) and
24
36
exists ( WideCharPointerType w , CharPointerType c |
25
37
w = e2 .getUnspecifiedType ( ) .( PointerType ) and
26
38
c = e1 .getUnspecifiedType ( ) .( PointerType )
27
- )
39
+ ) and
40
+ // Avoid `BYTE`-like casting as they are typically false positives
41
+ // Example: `BYTE* buffer;` ... `(wchar_t*) buffer;`
42
+ not e1 .getType ( ) instanceof UnlikelyToBeAStringType and
43
+ // Avoid castings from 'new' expressions as typically these will be safe
44
+ // Example: `__Type* ret = reinterpret_cast<__Type*>(New(m_pmo) char[num * sizeof(__Type)]);`
45
+ not exists ( NewOrNewArrayExpr newExpr | newExpr .getAChild * ( ) = e1 )
28
46
select e1 ,
29
47
"Conversion from " + e1 .getType ( ) .toString ( ) + " to " + e2 .getType ( ) .toString ( ) +
30
48
". Use of invalid string can lead to undefined behavior."
You can’t perform that action at this time.
0 commit comments