Skip to content

Commit 399967b

Browse files
committed
C++: Do not alert on unreachable code in cpp/incorrect-string-type-conversion
1 parent 2907861 commit 399967b

File tree

3 files changed

+5
-3
lines changed

3 files changed

+5
-3
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import cpp
1616
import semmle.code.cpp.controlflow.Guards
17+
import semmle.code.cpp.ir.IR
1718

1819
class WideCharPointerType extends PointerType {
1920
WideCharPointerType() { this.getBaseType() instanceof WideCharType }
@@ -108,7 +109,9 @@ where
108109
// Avoid cases where the cast is guarded by a check to determine if
109110
// unicode encoding is enabled in such a way to disallow the dangerous cast
110111
// at runtime.
111-
not isLikelyDynamicallyChecked(e1)
112+
not isLikelyDynamicallyChecked(e1) and
113+
// Avoid cases in unreachable blocks.
114+
any(EnterFunctionInstruction e).getASuccessor+().getAst() = e1
112115
select e1,
113116
"Conversion from " + e1.getType().toString() + " to " + e2.getType().toString() +
114117
". Use of invalid string can lead to undefined behavior."

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ size_t strlen(const char* str);
118118
template<typename C>
119119
size_t str_len(const C *str) {
120120
if (sizeof(C) != 1) {
121-
return wcslen((const wchar_t *)str); // $ SPURIOUS: Alert
121+
return wcslen((const wchar_t *)str); // GOOD -- unreachable code
122122
}
123123

124124
return strlen((const char *)str);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@
1111
| WcharCharConversion.cpp:103:21:103:26 | buffer | Conversion from LPTSTR to LPWSTR. Use of invalid string can lead to undefined behavior. |
1212
| WcharCharConversion.cpp:106:21:106:26 | buffer | Conversion from LPTSTR to LPWSTR. Use of invalid string can lead to undefined behavior. |
1313
| WcharCharConversion.cpp:110:20:110:25 | buffer | Conversion from LPTSTR to LPWSTR. Use of invalid string can lead to undefined behavior. |
14-
| WcharCharConversion.cpp:121:34:121:36 | str | Conversion from const char * to const wchar_t *. Use of invalid string can lead to undefined behavior. |
1514
| WcharCharConversion.cpp:130:34:130:36 | str | Conversion from const char * to const wchar_t *. Use of invalid string can lead to undefined behavior. |

0 commit comments

Comments
 (0)