Skip to content

Commit

Permalink
fix Fp
Browse files Browse the repository at this point in the history
  • Loading branch information
mary-georgiou-sonarsource committed Jul 16, 2024
1 parent 333ad30 commit a4a64cf
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ private void BuildBranches(BasicBlock block)
AddPredecessorsOutsideRegion(finallyBlock);
}
}
if (block.IsEnclosedIn(ControlFlowRegionKind.Catch) && block.Successors.FirstOrDefault(x => x.Semantics == ControlFlowBranchSemantics.Rethrow) is { })
{
BuildBranchesNestedCatchRethrow(block);
}

void AddPredecessorsOutsideRegion(BasicBlock destination)
{
Expand All @@ -161,6 +165,17 @@ private void BuildBranchesFinally(BasicBlock source, ControlFlowRegion finallyRe
}
}

private void BuildBranchesNestedCatchRethrow(BasicBlock block)
{
var tryRegion = block.EnclosingRegion(ControlFlowRegionKind.TryAndCatch).NestedRegion(ControlFlowRegionKind.Try);
foreach (var catchBlock in tryRegion.ReachableHandlers()
.Where(x => x.Kind == ControlFlowRegionKind.Catch && x != block.EnclosingRegion && x.FirstBlockOrdinal > block.Ordinal)
.SelectMany(x => x.Blocks(Cfg)))
{
AddBranch(block, catchBlock);
}
}

private void AddBranch(BasicBlock source, BasicBlock destination)
{
blockSuccessors[source.Ordinal].Add(destination);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,123 @@ public void Catch_Loop_Propagates_LiveIn()
context.Validate("Method(5);");
}

[TestMethod]
public void Throw_NestedCatch_LiveOut()
{
var code = """
var value = 100;
try
{
try
{
Method(value);
Method(0);
}
catch
{
value = 200;
throw;
}
}
catch
{
Method(value);
Method(1);
}
""";
var context = CreateContextCS(code);
context.ValidateEntry();
context.Validate("Method(0);", LiveIn("value"), LiveOut("value"));
context.Validate("value = 200;", LiveOut("value"));
context.Validate("Method(1);", LiveIn("value"));
context.ValidateExit();
}

[TestMethod]
public void Throw_NestedCatch_LiveInInConsecutiveOuterCatch()
{
var code = """
var value = 100;
try
{
try
{
Method(value);
Method(0);
}
catch
{
value = 200;
throw;
}
}
catch (ArgumentNullException)
{
Method(value);
Method(1);
}
catch (IOException)
{
Method(value);
Method(2);
}
catch (NullReferenceException)
{
Method(value);
Method(3);
}
""";
var context = CreateContextCS(code);
context.ValidateEntry();
context.Validate("Method(0);", LiveIn("value"), LiveOut("value"));
context.Validate("value = 200;", LiveOut("value"));
context.Validate("Method(1);", LiveIn("value"));
context.Validate("Method(2);", LiveIn("value"));
context.Validate("Method(3);", LiveIn("value"));
context.ValidateExit();
}

[TestMethod]
public void Throw_NestedCatch_OuterCatchRethrows_LiveOutOuterCathc()
{
var code = """
var value = 100;
try{
try
{
try
{
Method(value);
Method(0);
}
catch
{
value = 200;
throw;
}
}
catch // Outer catch
{
Method(value);
Method(1);
throw;
}
}
catch
{
Method(value);
Method(2);
}
""";
var context = CreateContextCS(code);
context.ValidateEntry();
context.Validate("Method(0);", LiveIn("value"), LiveOut("value"));
context.Validate("value = 200;", LiveOut("value"));
context.Validate("Method(1);", LiveIn("value"), LiveOut("value"));
context.Validate("Method(2);", LiveIn("value"));
context.ValidateExit();
}

[TestMethod]
public void Finally_LiveIn()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1612,7 +1612,7 @@ public void NestedCatchAndRethrow()
}
catch
{
value = 200; // Noncompliant FP
value = 200; // Compliant, catch rethrows and moves to the next catch.
throw;
}
}
Expand Down

0 comments on commit a4a64cf

Please sign in to comment.