-
Notifications
You must be signed in to change notification settings - Fork 231
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
Fix S2583 FP: nested try catch blocks #8140
Comments
Hi @bart-vmware, thanks for reporting the issue and the detailed reproduction steps. I reproduce the warning on In the specific case, there are two issues with our analysis:
I am going to add a reproducer for this specific scenario in our code base, and I am adding this issue to our backlog, to be included in the planning of the hardening sprint on rules S2583 and S2589, which should happen later this year. As for the other issue you have been experiencing with rule S2583, you can:
|
That's what I would expect. But how is that relevant here? I intentionally extracted
I only added the toggle to make the reproduction steps easier to perform. You can delete them and modify the code in-place, which still produces the bug. But again, the toggle only affects the implementation of |
@bart-vmware Indeed, I can reproduce even without using the preprocessor directives for conditional compilation. Regarding interprocedural execution: while we don't run symbolic execution across methods, for For example, I can reproduce the False Positive with just two nested using System.Data;
List<Exception> failures = null;
try
{
try
{
ThrowMaybe();
}
catch (InvalidOperationException)
{
// Do nothing
}
}
catch (DataException)
{
failures = new List<Exception>();
}
if (failures != null) // S2583: Conditionally executed code should be reachable
{
Console.WriteLine("Found failures.");
}
void ThrowMaybe() { } But I can't with a multi-catch statement: using System.Data;
List<Exception> failures = null;
try
{
ThrowMaybe();
}
catch (InvalidOperationException)
{
// Do nothing
}
catch (DataException)
{
failures = new List<Exception>();
}
if (failures != null) // No report in this case
{
Console.WriteLine("Found failures.");
}
void ThrowMaybe() { } And this, as you pointed out, regardless of whether
The implementation of A deeper analysis to identify and fix the root cause of the issue will be run during the hardening sprint on this rule. |
Thanks for the explanation. I'm surprised that the analyzer looks into the implementation of called methods. That sounds pretty expensive and unreliable. I've never heard of a compiler doing that. |
After looking at this again: We do not look into the implementation of called methods. I apologize for the confusion. The issue is related to how the SE engine handles exception flows, which seems not to work correctly in this case. This needs a more profound investigation at some point. |
Please prioritize fixing this, it is a very dangerous bug. |
Description
We've found cases in our codebase where Sonar incorrectly suggests replacing a condition with
true
, claiming that the code is unreachable, which is incorrect.Repro steps
dotnet new console -f net6.0
dotnet add package SonarAnalyzer.CSharp
Program.cs
:Running this program prints:
Now comment out the first line (
#define TOGGLE
) and run again. It now prints nothing. This shows there's a difference in behavior, ie the condition in the markedif
statement is not redundant.Changing the
if
line toif (true)
, as suggested by the rule description, changes the output to always print:irrespective of the toggle. Therefore the Sonar warning is incorrect.
Expected behavior
No warning, because the code is reachable.
Actual behavior
Warning that suggests breaking the program behavior.
Related information
The text was updated successfully, but these errors were encountered: