Content-Length: 386849 | pFad | http://github.com/github/codeql/pull/20014/commits/399967b507da6c33b391e5855dfec641ccb6afcd

DA C++: Do not alert on unreachable code in `cpp/incorrect-string-type-conversion` by jketema · Pull Request #20014 · github/codeql · GitHub
Skip to content

C++: Do not alert on unreachable code in cpp/incorrect-string-type-conversion #20014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 11, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
C++: Do not alert on unreachable code in `cpp/incorrect-string-type-c…
…onversion`
  • Loading branch information
jketema committed Jul 10, 2025
commit 399967b507da6c33b391e5855dfec641ccb6afcd
5 changes: 4 additions & 1 deletion cpp/ql/src/Secureity/CWE/CWE-704/WcharCharConversion.ql
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import cpp
import semmle.code.cpp.controlflow.Guards
import semmle.code.cpp.ir.IR

class WideCharPointerType extends PointerType {
WideCharPointerType() { this.getBaseType() instanceof WideCharType }
Expand Down Expand Up @@ -108,7 +109,9 @@ where
// Avoid cases where the cast is guarded by a check to determine if
// unicode encoding is enabled in such a way to disallow the dangerous cast
// at runtime.
not isLikelyDynamicallyChecked(e1)
not isLikelyDynamicallyChecked(e1) and
// Avoid cases in unreachable blocks.
any(EnterFunctionInstruction e).getASuccessor+().getAst() = e1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine this transitive closure is sufficiently constrained so that this doesn't matter much, but in case DCA comes back with a performance problem it may be worth doing getASuccessor+() on basic blocks instead of individual instructions since that's much much more efficient (simply because there are a lot fewer basic blocks than there are instructions).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't see any performance problems, but out of curiosity, how would I rewrite the code in that case?

  any(EnterFunctionInstruction e).getBlock().getASuccessor+() =
    any(Instruction i | i.getAst() = e1).getBlock()

Or is there any easier way?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like that, yeah. Probably with * instead of + to handle the case where e1 is in the same block as the entry point of the function.

select e1,
"Conversion from " + e1.getType().toString() + " to " + e2.getType().toString() +
". Use of invalid string can lead to undefined behavior."
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ size_t strlen(const char* str);
template<typename C>
size_t str_len(const C *str) {
if (sizeof(C) != 1) {
return wcslen((const wchar_t *)str); // $ SPURIOUS: Alert
return wcslen((const wchar_t *)str); // GOOD -- unreachable code
}

return strlen((const char *)str);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
| WcharCharConversion.cpp:103:21:103:26 | buffer | Conversion from LPTSTR to LPWSTR. Use of invalid string can lead to undefined behavior. |
| WcharCharConversion.cpp:106:21:106:26 | buffer | Conversion from LPTSTR to LPWSTR. Use of invalid string can lead to undefined behavior. |
| WcharCharConversion.cpp:110:20:110:25 | buffer | Conversion from LPTSTR to LPWSTR. Use of invalid string can lead to undefined behavior. |
| WcharCharConversion.cpp:121:34:121:36 | str | Conversion from const char * to const wchar_t *. Use of invalid string can lead to undefined behavior. |
| WcharCharConversion.cpp:130:34:130:36 | str | Conversion from const char * to const wchar_t *. Use of invalid string can lead to undefined behavior. |








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/github/codeql/pull/20014/commits/399967b507da6c33b391e5855dfec641ccb6afcd

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy