Skip to content
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 S2930 FN: Track System.Threading.CancellationTokenSource #8302

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions analyzers/its/expected/Nancy/Nancy--net452-S2930.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"issues": [
{
"id": "S2930",
"message": "Dispose 'engineDisposedCts' when it is no longer needed.",
"location": {
"uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/sources/Nancy/src/Nancy/NancyEngine.cs#L98",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a FP, as it is disposed in dispose method and we can't go cross-procedure

"region": {
"startLine": 98,
"startColumn": 13,
"endLine": 98,
"endColumn": 67
}
}
}
]
}
17 changes: 17 additions & 0 deletions analyzers/its/expected/Nancy/Nancy--netstandard2.0-S2930.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"issues": [
{
"id": "S2930",
"message": "Dispose 'engineDisposedCts' when it is no longer needed.",
"location": {
"uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/sources/Nancy/src/Nancy/NancyEngine.cs#L98",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also FP, stored to a field, disposed in Dispose method

"region": {
"startLine": 98,
"startColumn": 13,
"endLine": 98,
"endColumn": 67
}
}
}
]
}
17 changes: 17 additions & 0 deletions analyzers/its/expected/akka.net/Akka--netstandard2.0-S2930.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"issues": [
{
"id": "S2930",
"message": "Dispose 'cancellationSource' when it is no longer needed.",
"location": {
"uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/sources/akka.net/src/core/Akka/Actor/Futures.cs#L356",
"region": {
"startLine": 356,
"startColumn": 17,
"endLine": 356,
"endColumn": 67
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"issues": [
{
"id": "S2930",
"message": "Dispose 'cts' when it is no longer needed.",
"location": {
"uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/sources/akka.net/src/core/Akka.Cluster.Tests.Performance/Startup/ClusterStartupSpec.cs#L58",
"region": {
"startLine": 58,
"startColumn": 17,
"endLine": 58,
"endColumn": 76
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"issues": [
{
"id": "S2930",
"message": "Dispose '_pendingRequestsCancellation' when it is no longer needed.",
"location": {
"uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/sources/akka.net/src/contrib/persistence/Akka.Persistence.Sql.Common/Journal/SqlJournal.cs#L45",
"region": {
"startLine": 45,
"startColumn": 13,
"endLine": 45,
"endColumn": 73
}
}
},
{
"id": "S2930",
"message": "Dispose '_pendingRequestsCancellation' when it is no longer needed.",
"location": {
"uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/sources/akka.net/src/contrib/persistence/Akka.Persistence.Sql.Common/Snapshot/SqlSnapshotStore.cs#L53",
"region": {
"startLine": 53,
"startColumn": 13,
"endLine": 53,
"endColumn": 73
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"issues": [
{
"id": "S2930",
"message": "Dispose 'cts' when it is no longer needed.",
"location": {
"uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/sources/akka.net/src/core/Akka.Remote.Tests.Performance/Transports/RemoteMessagingThroughputSpecBase.cs#L56",
"region": {
"startLine": 56,
"startColumn": 29,
"endLine": 56,
"endColumn": 88
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"issues": [
{
"id": "S2930",
"message": "Dispose '_cancellation' when it is no longer needed.",
"location": {
"uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/sources/akka.net/src/core/Akka.Streams/Implementation/IO/OutputStreamSourceStage.cs#L124",
"region": {
"startLine": 124,
"startColumn": 54,
"endLine": 124,
"endColumn": 99
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"issues": [
{
"id": "S2930",
"message": "Dispose 'cancel' when it is no longer needed.",
"location": {
"uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/sources/akka.net/src/core/Akka.Tests.Performance/IO/TcpInboundOnlySpec.cs#L73",
"region": {
"startLine": 73,
"startColumn": 41,
"endLine": 73,
"endColumn": 79
}
}
}
]
}
17 changes: 17 additions & 0 deletions analyzers/its/expected/akka.net/RemotePingPong--net471-S2930.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"issues": [
{
"id": "S2930",
"message": "Dispose 'cts' when it is no longer needed.",
"location": {
"uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/sources/akka.net/src/benchmark/RemotePingPong/Program.cs#L239",
"region": {
"startLine": 239,
"startColumn": 29,
"endLine": 239,
"endColumn": 88
}
}
}
]
}
17 changes: 17 additions & 0 deletions analyzers/its/expected/akka.net/RemotePingPong--net5.0-S2930.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"issues": [
{
"id": "S2930",
"message": "Dispose 'cts' when it is no longer needed.",
"location": {
"uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/sources/akka.net/src/benchmark/RemotePingPong/Program.cs#L239",
"region": {
"startLine": 239,
"startColumn": 29,
"endLine": 239,
"endColumn": 88
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"issues": [
{
"id": "S2930",
"message": "Dispose 'cts' when it is no longer needed.",
"location": {
"uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/sources/akka.net/src/benchmark/RemotePingPong/Program.cs#L239",
"region": {
"startLine": 239,
"startColumn": 29,
"endLine": 239,
"endColumn": 88
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"issues": [
{
"id": "S2930",
"message": "Dispose 'cts' when it is no longer needed.",
"location": {
"uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/sources/akka.net/src/examples/AspNetCore/Samples.Akka.AspNetCore/Startup.cs#L56",
"region": {
"startLine": 56,
"startColumn": 25,
"endLine": 56,
"endColumn": 83
}
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public sealed class DisposableNotDisposed : SonarDiagnosticAnalyzer
KnownType.System_IO_StreamWriter,
KnownType.System_Net_WebClient,
KnownType.System_Net_Sockets_TcpClient,
KnownType.System_Net_Sockets_UdpClient);
KnownType.System_Net_Sockets_UdpClient,
KnownType.System_Threading_CancellationTokenSource);

private static readonly ImmutableArray<KnownType> DisposableTypes = ImmutableArray.Create(KnownType.System_IDisposable, KnownType.System_IAsyncDisposable);

Expand All @@ -49,7 +50,8 @@ public sealed class DisposableNotDisposed : SonarDiagnosticAnalyzer
"System.IO.File.Create",
"System.IO.File.Open",
"System.Drawing.Image.FromFile",
"System.Drawing.Image.FromStream"
"System.Drawing.Image.FromStream",
"System.Threading.CancellationTokenSource.CreateLinkedTokenSource"
};

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule);
Expand Down
1 change: 1 addition & 0 deletions analyzers/src/SonarAnalyzer.Common/Helpers/KnownType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ public sealed partial class KnownType
public static readonly KnownType System_Text_RegularExpressions_Regex = new("System.Text.RegularExpressions.Regex");
public static readonly KnownType System_Text_RegularExpressions_RegexOptions = new("System.Text.RegularExpressions.RegexOptions");
public static readonly KnownType System_Text_StringBuilder = new("System.Text.StringBuilder");
public static readonly KnownType System_Threading_CancellationTokenSource = new("System.Threading.CancellationTokenSource");
public static readonly KnownType System_Threading_Monitor = new("System.Threading.Monitor");
public static readonly KnownType System_Threading_Mutex = new("System.Threading.Mutex");
public static readonly KnownType System_Threading_ReaderWriterLock = new("System.Threading.ReaderWriterLock");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Tests.Diagnostics
{
Expand Down Expand Up @@ -109,6 +110,11 @@ public DisposableNotDisposed(FileStream fs)
field_fs7 = new FileStream(@"c:\foo.txt", FileMode.Open); // Noncompliant - even if field_fs7's type is object

NoOperation(this.field_fs8);

var tokenSource1 = new CancellationTokenSource(); // Noncompliant
var tokenSource2 = CancellationTokenSource.CreateLinkedTokenSource(CancellationToken.None, CancellationToken.None); // Noncompliant
var tokenSource3 = CancellationTokenSource.CreateLinkedTokenSource(CancellationToken.None, CancellationToken.None); // Compliant, disposed
tokenSource3.Dispose();
}

private void Conditions(bool cond, string x)
Expand Down