-
Notifications
You must be signed in to change notification settings - Fork 1.8k
C++: Fix IR generation for switch statements #2887
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it now be the case that all SwitchInstruction
s have a DefaultEdge
out of them? If so, you can add a sanity check for it at the top of Instruction.qll
.
If we change it so that the IR generates a |
It should definitely be a |
I don't see a need to distinguish those. The AST is available for queries that are interested in surface syntax. |
I've changed the IR generation so that a Specifically, looking at https://github.com/Semmle/ql/blob/master/csharp/ql/src/semmle/code/csharp/ir/implementation/raw/internal/TranslatedStmt.qll#L830 it seems like a similar bug as this one exists in that translation. Can you confirm this @Semmle/cs? |
The C# IR is not really owned by the C# team. It's effectively owned by @dbartol. So feel free to either apply the same fix to C# or leave the sanity test there as documentation that this needs fixing. |
Ah, I see. I'm pretty sure it needs the same fix (the original code is identical to the C++ one), so I'll apply the same fix to the C# translation and let @dbartol verify that it's the correct thing to do. |
cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/Instruction.qll
Show resolved
Hide resolved
b03fa51
to
af364e6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test results LGTM now, but there's one issue with the implementation.
IR generation for
switch
statements with only one case (or multiple cases with fallthrough) such as:was incorrectly being generated to something with the semantics of:
This caused a false positive in
cpp/pointer-overflow-check
onphp/php-src
since the IR-based GVN would assign the same value number toa
andb
in the<
expression in a function like:With this PR the IR generated by a
switch
statement will contain aGoto
edge if there's nodefault
case in theswitch
statement. Another choice would also be to generate the newGoto
edge as aDefault
edge, meaning that allswitch
statements in the IR would have aDefault
edge.