From ae4eddec5cdf8cd353135cacf20e73fdfa50f975 Mon Sep 17 00:00:00 2001 From: Tim Pohlmann Date: Wed, 21 Jun 2023 11:12:07 +0200 Subject: [PATCH 1/3] Support unsafe type declarations --- .../Rules/Hotspots/UnsafeCodeBlocks.cs | 8 ++++++++ .../Rules/Hotspots/UnsafeCodeBlocksTest.cs | 2 +- .../TestCases/Hotspots/UnsafeCodeBlocks.cs | 6 +++--- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/analyzers/src/SonarAnalyzer.CSharp/Rules/Hotspots/UnsafeCodeBlocks.cs b/analyzers/src/SonarAnalyzer.CSharp/Rules/Hotspots/UnsafeCodeBlocks.cs index e8a91bfc39c..84d903f45ec 100644 --- a/analyzers/src/SonarAnalyzer.CSharp/Rules/Hotspots/UnsafeCodeBlocks.cs +++ b/analyzers/src/SonarAnalyzer.CSharp/Rules/Hotspots/UnsafeCodeBlocks.cs @@ -45,6 +45,14 @@ protected override void Initialize(SonarAnalysisContext context) } }, SyntaxKind.MethodDeclaration, SyntaxKind.ConstructorDeclaration, SyntaxKind.DestructorDeclaration, SyntaxKind.OperatorDeclaration); + context.RegisterNodeAction(c => + { + if (c.Node is BaseTypeDeclarationSyntax { Modifiers: var modifiers } + && modifiers.Find(SyntaxKind.UnsafeKeyword) is { } unsafeModifier) + { + Report(c, unsafeModifier); + } + }, SyntaxKind.ClassDeclaration, SyntaxKind.InterfaceDeclaration, SyntaxKind.StructDeclaration, SyntaxKindEx.RecordClassDeclaration); } private void ReportIfUnsafe(SonarSyntaxNodeReportingContext context, SyntaxTokenList modifiers) diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/Rules/Hotspots/UnsafeCodeBlocksTest.cs b/analyzers/tests/SonarAnalyzer.UnitTest/Rules/Hotspots/UnsafeCodeBlocksTest.cs index 542a076f13b..d0b969209de 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/Rules/Hotspots/UnsafeCodeBlocksTest.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/Rules/Hotspots/UnsafeCodeBlocksTest.cs @@ -36,7 +36,7 @@ public void UnsafeCodeBlocks() => [TestMethod] public void UnsafeRecord() => - builder.AddSnippet("""unsafe record MyRecord(byte* Pointer); // FN""") + builder.AddSnippet("""unsafe record MyRecord(byte* Pointer); // Noncompliant""") .WithOptions(ParseOptionsHelper.FromCSharp9).Verify(); [TestMethod] diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/Hotspots/UnsafeCodeBlocks.cs b/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/Hotspots/UnsafeCodeBlocks.cs index 2d709a9f44c..81ea30c569e 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/Hotspots/UnsafeCodeBlocks.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/Hotspots/UnsafeCodeBlocks.cs @@ -21,14 +21,14 @@ unsafe void Local(byte* pointer) { } // FN void SafeLocal(byte noPointer) { } // Compliant } - unsafe class UnsafeClass { } // FN + unsafe class UnsafeClass { } // Noncompliant - unsafe struct UnsafeStruct // FN + unsafe struct UnsafeStruct // Noncompliant { unsafe fixed byte unsafeFixedBuffer[16]; // FN } - unsafe interface IUnsafeInterface { } // FN + unsafe interface IUnsafeInterface { } // Noncompliant unsafe Sample(byte* pointer) { } // Noncompliant From 4df673a2f03b60b6a1bc0a58aa693c4cad5736ec Mon Sep 17 00:00:00 2001 From: Tim Pohlmann Date: Wed, 21 Jun 2023 11:16:03 +0200 Subject: [PATCH 2/3] Update ITs --- .../its/expected/Net5/Net5--net5.0-S6640.json | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 analyzers/its/expected/Net5/Net5--net5.0-S6640.json diff --git a/analyzers/its/expected/Net5/Net5--net5.0-S6640.json b/analyzers/its/expected/Net5/Net5--net5.0-S6640.json new file mode 100644 index 00000000000..86f61a937f4 --- /dev/null +++ b/analyzers/its/expected/Net5/Net5--net5.0-S6640.json @@ -0,0 +1,30 @@ +{ +"issues": [ +{ +"id": "S6640", +"message": "Make sure that using "unsafe" is safe here.", +"location": { +"uri": "sources\Net5\Net5\FunctionPointers.cs", +"region": { +"startLine": 3, +"startColumn": 12, +"endLine": 3, +"endColumn": 18 +} +} +}, +{ +"id": "S6640", +"message": "Make sure that using "unsafe" is safe here.", +"location": { +"uri": "sources\Net5\Net5\S4000.cs", +"region": { +"startLine": 5, +"startColumn": 12, +"endLine": 5, +"endColumn": 18 +} +} +} +] +} From 2a19fa31d722ee48bb03325bb000763b9c5e2624 Mon Sep 17 00:00:00 2001 From: Tim Pohlmann Date: Wed, 21 Jun 2023 15:22:21 +0200 Subject: [PATCH 3/3] Review 1 --- .../Rules/Hotspots/UnsafeCodeBlocks.cs | 20 +++++-------------- .../Rules/Hotspots/UnsafeCodeBlocksTest.cs | 2 +- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/analyzers/src/SonarAnalyzer.CSharp/Rules/Hotspots/UnsafeCodeBlocks.cs b/analyzers/src/SonarAnalyzer.CSharp/Rules/Hotspots/UnsafeCodeBlocks.cs index 84d903f45ec..5c856aa771e 100644 --- a/analyzers/src/SonarAnalyzer.CSharp/Rules/Hotspots/UnsafeCodeBlocks.cs +++ b/analyzers/src/SonarAnalyzer.CSharp/Rules/Hotspots/UnsafeCodeBlocks.cs @@ -37,22 +37,12 @@ public UnsafeCodeBlocks(IAnalyzerConfiguration configuration) : base(configurati protected override void Initialize(SonarAnalysisContext context) { context.RegisterNodeAction(c => Report(c, ((UnsafeStatementSyntax)c.Node).UnsafeKeyword), SyntaxKind.UnsafeStatement); - context.RegisterNodeAction(c => - { - if (c.Node is BaseMethodDeclarationSyntax { Modifiers: var modifiers }) - { - ReportIfUnsafe(c, modifiers); - } - }, + context.RegisterNodeAction( + c => ReportIfUnsafe(c, ((BaseMethodDeclarationSyntax)c.Node).Modifiers), SyntaxKind.MethodDeclaration, SyntaxKind.ConstructorDeclaration, SyntaxKind.DestructorDeclaration, SyntaxKind.OperatorDeclaration); - context.RegisterNodeAction(c => - { - if (c.Node is BaseTypeDeclarationSyntax { Modifiers: var modifiers } - && modifiers.Find(SyntaxKind.UnsafeKeyword) is { } unsafeModifier) - { - Report(c, unsafeModifier); - } - }, SyntaxKind.ClassDeclaration, SyntaxKind.InterfaceDeclaration, SyntaxKind.StructDeclaration, SyntaxKindEx.RecordClassDeclaration); + context.RegisterNodeAction( + c => ReportIfUnsafe(c, ((BaseTypeDeclarationSyntax)c.Node).Modifiers), + SyntaxKind.ClassDeclaration, SyntaxKind.InterfaceDeclaration, SyntaxKind.StructDeclaration, SyntaxKindEx.RecordClassDeclaration, SyntaxKindEx.RecordStructDeclaration); } private void ReportIfUnsafe(SonarSyntaxNodeReportingContext context, SyntaxTokenList modifiers) diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/Rules/Hotspots/UnsafeCodeBlocksTest.cs b/analyzers/tests/SonarAnalyzer.UnitTest/Rules/Hotspots/UnsafeCodeBlocksTest.cs index d0b969209de..74d1b8f1792 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/Rules/Hotspots/UnsafeCodeBlocksTest.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/Rules/Hotspots/UnsafeCodeBlocksTest.cs @@ -41,7 +41,7 @@ public void UnsafeRecord() => [TestMethod] public void UnsafeRecordStruct() => - builder.AddSnippet("""unsafe record struct MyRecord(byte* Pointer); // FN""") + builder.AddSnippet("""unsafe record struct MyRecord(byte* Pointer); // Noncompliant""") .WithOptions(ParseOptionsHelper.FromCSharp10).Verify(); #endif