Skip to content

Commit

Permalink
LVA: Don't connect from throw in catch to neighbouring catch (#9539)
Browse files Browse the repository at this point in the history
  • Loading branch information
mary-georgiou-sonarsource committed Jul 24, 2024
1 parent 066156b commit 655fe36
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ private void BuildBranchesFinally(BasicBlock source, ControlFlowRegion finallyRe

private void BuildBranchesRethrow(BasicBlock block)
{
var reachableHandlerRegions = block.EnclosingRegion(ControlFlowRegionKind.TryAndCatch).NestedRegion(ControlFlowRegionKind.Try).ReachableHandlers();
var reachableCatchAndFinallyBlocks = reachableHandlerRegions.Where(x => x.FirstBlockOrdinal > block.Ordinal).SelectMany(x => x.Blocks(Cfg));
var currentTryCatchRegion = block.EnclosingRegion(ControlFlowRegionKind.TryAndCatch);
var reachableHandlerRegions = currentTryCatchRegion.NestedRegion(ControlFlowRegionKind.Try).ReachableHandlers();
var reachableCatchAndFinallyBlocks = reachableHandlerRegions.Where(x => x.FirstBlockOrdinal > currentTryCatchRegion.LastBlockOrdinal).SelectMany(x => x.Blocks(Cfg));
foreach (var catchBlock in reachableCatchAndFinallyBlocks.Where(x => x.EnclosingRegion.Kind is ControlFlowRegionKind.Catch))
{
AddBranch(block, catchBlock);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1421,7 +1421,7 @@ public void TryCatchFinally_RethrowCatchRethrowOuterFinally()
}

[TestMethod]
public void TryCatchFinally_ConsecutiveCatchRethrowFinally()
public void TryCatchFinally_ConsecutiveCatchAllThrowRethrowFinally()
{
const string code = """
var value = 0;
Expand Down Expand Up @@ -1455,6 +1455,34 @@ public void TryCatchFinally_ConsecutiveCatchRethrowFinally()
context.ValidateExit();
}

[TestMethod]
public void TryCatchFinally_ConsecutiveCatchRethrow()
{
const string code = """
var value = 0;
try
{
Method(0);
value = 42;
}
catch (IOException)
{
Method(value);
value = 1;
throw;
}
catch (ArgumentNullException)
{
Method(value + 1);
}
""";
var context = CreateContextCS(code);
context.ValidateEntry();
context.Validate("value = 1;", LiveIn("value"));
context.Validate("Method(value + 1);", LiveIn("value"));
context.ValidateExit();
}

[TestMethod]
public void TryCatchFinally_FilteredCatchRethrowFinally()
{
Expand Down

0 comments on commit 655fe36

Please sign in to comment.