Skip to content

Commit 78d5d9a

Browse files
authored
Merge pull request #15448 from microsoft/false_positive_cpp_incorrect_string_type_conversion
cpp/incorrect-string-type-conversion false positive fixes
2 parents 56e44f9 + 308a3b5 commit 78d5d9a

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

cpp/ql/src/Security/CWE/CWE-704/WcharCharConversion.ql

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,31 @@ class WideCharPointerType extends PointerType {
1818
WideCharPointerType() { this.getBaseType() instanceof WideCharType }
1919
}
2020

21+
/**
22+
* A type 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+
2133
from Expr e1, Cast e2
2234
where
2335
e2 = e1.getConversion() and
2436
exists(WideCharPointerType w, CharPointerType c |
2537
w = e2.getUnspecifiedType().(PointerType) and
2638
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)
2846
select e1,
2947
"Conversion from " + e1.getType().toString() + " to " + e2.getType().toString() +
3048
". Use of invalid string can lead to undefined behavior."
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Corrected 2 false positive with `cpp/incorrect-string-type-conversion`: conversion of byte arrays to wchar and new array allocations converted to wchar.

cpp/ql/test/query-tests/Security/CWE/CWE-704/WcharCharConversion.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,26 @@ void Test()
3131

3232
fconstWChar((LPCWSTR)lpWchar); // Valid
3333
fWChar(lpWchar); // Valid
34+
}
35+
36+
void NewBufferFalsePositiveTest()
37+
{
38+
wchar_t *lpWchar = NULL;
39+
40+
lpWchar = (LPWSTR)new char[56]; // Possible False Positive
41+
}
42+
43+
typedef unsigned char BYTE;
44+
typedef BYTE* PBYTE;
45+
46+
void NonStringFalsePositiveTest1(PBYTE buffer)
47+
{
48+
wchar_t *lpWchar = NULL;
49+
lpWchar = (LPWSTR)buffer; // Possible False Positive
50+
}
51+
52+
void NonStringFalsePositiveTest2(unsigned char* buffer)
53+
{
54+
wchar_t *lpWchar = NULL;
55+
lpWchar = (LPWSTR)buffer; // Possible False Positive
3456
}

0 commit comments

Comments
 (0)