Skip to content

Commit

Permalink
Merge pull request #3514 from dotpaul/fix
Browse files Browse the repository at this point in the history
Handle local functions in DoNotCallDangerousMethodsInDeserialization
  • Loading branch information
dotpaul committed Apr 16, 2020
2 parents f970239 + 62200cf commit e5c68c9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,12 @@ public sealed override void Initialize(AnalysisContext context)
calledMethods.TryAdd(calledSymbol, true);
// calledSymbol.ContainingSymbol.Kind == SymbolKind.Method => local function
// For the purposes of this rule, we'll treat invocations inside the local function as part of
// the containing method's set of invocations.
if (!calledSymbol.IsInSource() ||
calledSymbol.ContainingType.TypeKind == TypeKind.Interface ||
calledSymbol.ContainingSymbol.Kind == SymbolKind.Method ||
calledSymbol.IsAbstract ||
possibleDelegateSymbol.TypeKind == TypeKind.Delegate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2499,6 +2499,59 @@ private void TestMethod()
}");
}

[Fact]
public async Task TestLocalFunctionDiagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(@"
using System;
using System.IO;
using System.Runtime.Serialization;
[Serializable()]
public class TestClass
{
private string member;
[OnDeserializing()]
internal void OnDeserializingMethod(StreamingContext context)
{
byte[] bytes = new byte[] {0x20, 0x20, 0x20};
ALocalFunction();
void ALocalFunction()
{
File.WriteAllBytes(""C:\\"", bytes);
}
}
}",
GetCSharpResultAt(12, 19, "TestClass", "OnDeserializingMethod", "WriteAllBytes"));
}

[Fact]
public async Task TestLocalFunctionNoDiagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(@"
using System;
using System.Runtime.Serialization;
[Serializable()]
public class TestClass
{
private string member;
[OnDeserializing()]
internal void OnDeserializingMethod(StreamingContext context)
{
ALocalFunction();
void ALocalFunction()
{
object o = new Object();
}
}
}");
}

private static DiagnosticResult GetCSharpResultAt(int line, int column, params string[] arguments)
=> VerifyCS.Diagnostic()
.WithLocation(line, column)
Expand Down

0 comments on commit e5c68c9

Please sign in to comment.