Skip to content

Commit dbc3dad

Browse files
committed
modernize code examples
1 parent 4d1ee70 commit dbc3dad

File tree

9 files changed

+58
-60
lines changed

9 files changed

+58
-60
lines changed

docs/fundamentals/code-analysis/quality-rules/ca1816.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Violations of this rule can be caused by:
3636

3737
## Rule description
3838

39-
The <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> method lets users release resources at any time before the object becoming available for garbage collection. If the <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> method is called, it frees resources of the object. This makes finalization unnecessary. <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> should call <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType> so the garbage collector doesn't call the finalizer of the object.
39+
The <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> method lets users release resources at any time before the object becomes available for garbage collection. If the <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> method is called, it frees resources of the object. This makes finalization unnecessary. <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> should call <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType> so the garbage collector doesn't call the finalizer of the object.
4040

4141
To prevent derived types with finalizers from having to reimplement <xref:System.IDisposable> and to call it, unsealed types without finalizers should still call <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType>.
4242

docs/fundamentals/code-analysis/quality-rules/ca2100.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "CA2100: Review SQL queries for security vulnerabilities (code analysis)"
33
description: "Learn about code analysis rule CA2100: Review SQL queries for security vulnerabilities"
4-
ms.date: 11/04/2016
4+
ms.date: 09/24/2025
55
f1_keywords:
66
- Review SQL queries for security vulnerabilities
77
- ReviewSqlQueriesForSecurityVulnerabilities
@@ -37,7 +37,7 @@ This rule assumes that any string whose value can't be determined at compile tim
3737
- Use a parameterized command string.
3838
- Validate the user input for both type and content before you build the command string.
3939

40-
The following .NET types implement the <xref:System.Data.IDbCommand.CommandText%2A> property or provide constructors that set the property by using a string argument.
40+
The following .NET types implement the <xref:System.Data.IDbCommand.CommandText%2A> property or provide constructors that set the property by using a string argument:
4141

4242
- <xref:System.Data.Odbc.OdbcCommand?displayProperty=fullName> and <xref:System.Data.Odbc.OdbcDataAdapter?displayProperty=fullName>
4343
- <xref:System.Data.OleDb.OleDbCommand?displayProperty=fullName> and <xref:System.Data.OleDb.OleDbDataAdapter?displayProperty=fullName>
@@ -64,7 +64,7 @@ To fix a violation of this rule, use a parameterized query.
6464

6565
## When to suppress warnings
6666

67-
It is safe to suppress a warning from this rule if the command text does not contain any user input.
67+
It's safe to suppress a warning from this rule if the command text does not contain any user input.
6868

6969
## Suppress a warning
7070

docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ dotnet_diagnostic.CA1822.severity = none
1717

1818
# CA2200: Rethrow to preserve stack details
1919
dotnet_diagnostic.CA2200.severity = suggestion
20+
21+
# CA2100
22+
dotnet_diagnostic.CA2100.severity = warning

docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1806.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ public class Book
77

88
public Book(string title)
99
{
10-
if (title != null)
11-
{
12-
// Violates this rule
13-
title.Trim();
14-
}
10+
// Violates this rule
11+
title?.Trim();
1512

1613
_Title = title;
1714
}

docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1816.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using System;
2-
using System.Data.SqlClient;
2+
using System.IO;
33

44
namespace ca1816
55
{
66
//<snippet1>
7-
public class DatabaseConnector : IDisposable
7+
public class MyStreamClass : IDisposable
88
{
9-
private SqlConnection? _Connection = new SqlConnection();
9+
private MemoryStream? _stream = new();
1010

1111
public void Dispose()
1212
{
@@ -18,11 +18,8 @@ protected virtual void Dispose(bool disposing)
1818
{
1919
if (disposing)
2020
{
21-
if (_Connection != null)
22-
{
23-
_Connection.Dispose();
24-
_Connection = null;
25-
}
21+
_stream?.Dispose();
22+
_stream = null;
2623
}
2724
}
2825
}
@@ -32,9 +29,9 @@ protected virtual void Dispose(bool disposing)
3229
namespace ca1816_2
3330
{
3431
//<snippet2>
35-
public class DatabaseConnector : IDisposable
32+
public class MyStreamClass : IDisposable
3633
{
37-
private SqlConnection? _Connection = new SqlConnection();
34+
private MemoryStream? _stream = new();
3835

3936
public void Dispose()
4037
{
@@ -46,11 +43,8 @@ protected virtual void Dispose(bool disposing)
4643
{
4744
if (disposing)
4845
{
49-
if (_Connection != null)
50-
{
51-
_Connection.Dispose();
52-
_Connection = null;
53-
}
46+
_stream?.Dispose();
47+
_stream = null;
5448
}
5549
}
5650
}

docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca2100.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
using System.Data;
2-
using System.Data.SqlClient;
2+
using System.Data.OleDb;
33

44
namespace ca2100
55
{
66
//<snippet1>
7-
public class SqlQueries
7+
public class OleDbQueries
88
{
99
public object UnsafeQuery(
1010
string connection, string name, string password)
1111
{
12-
SqlConnection someConnection = new SqlConnection(connection);
13-
SqlCommand someCommand = new SqlCommand();
12+
using OleDbConnection someConnection = new(connection);
13+
using OleDbCommand someCommand = new();
1414
someCommand.Connection = someConnection;
1515

1616
someCommand.CommandText = "SELECT AccountNumber FROM Users " +
@@ -26,14 +26,14 @@ public object UnsafeQuery(
2626
public object SaferQuery(
2727
string connection, string name, string password)
2828
{
29-
SqlConnection someConnection = new SqlConnection(connection);
30-
SqlCommand someCommand = new SqlCommand();
29+
using OleDbConnection someConnection = new(connection);
30+
using OleDbCommand someCommand = new();
3131
someCommand.Connection = someConnection;
3232

3333
someCommand.Parameters.Add(
34-
"@username", SqlDbType.NChar).Value = name;
34+
"@username", OleDbDbType.NChar).Value = name;
3535
someCommand.Parameters.Add(
36-
"@password", SqlDbType.NChar).Value = password;
36+
"@password", OleDbDbType.NChar).Value = password;
3737
someCommand.CommandText = "SELECT AccountNumber FROM Users " +
3838
"WHERE Username=@username AND Password=@password";
3939

@@ -48,7 +48,7 @@ class MaliciousCode
4848
{
4949
static void Main2100(string[] args)
5050
{
51-
SqlQueries queries = new SqlQueries();
51+
OleDbQueries queries = new OleDbQueries();
5252
queries.UnsafeQuery(args[0], "' OR 1=1 --", "[PLACEHOLDER]");
5353
// Resultant query (which is always true):
5454
// SELECT AccountNumber FROM Users WHERE Username='' OR 1=1

docs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/.editorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
dotnet_diagnostic.CA1420.severity = suggestion
33
dotnet_diagnostic.CA1422.severity = suggestion
44
dotnet_diagnostic.CA2200.severity = suggestion
5+
dotnet_diagnostic.CA2100.severity = warning

docs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca1816-call-gc-suppressfinalize-correctly_1.vb

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
Imports System
2-
Imports System.Data.SqlClient
1+
Imports System.IO
32

43
Namespace ca1816
54

65
'<snippet1>
7-
Public Class DatabaseConnector
6+
Public Class MyStreamClass
87
Implements IDisposable
98

10-
Private _Connection As New SqlConnection
9+
Private _stream As New MemoryStream
1110

1211
Public Sub Dispose() Implements IDisposable.Dispose
1312
Dispose(True)
14-
' Violates rules
13+
' Violates rule.
1514
GC.SuppressFinalize(True)
1615
End Sub
1716

1817
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
1918
If disposing Then
20-
If _Connection IsNot Nothing Then
21-
_Connection.Dispose()
22-
_Connection = Nothing
19+
If _stream IsNot Nothing Then
20+
_stream.Dispose()
21+
_stream = Nothing
2322
End If
2423
End If
2524
End Sub
@@ -31,10 +30,10 @@ End Namespace
3130

3231
Namespace ca1816_2
3332
'<snippet2>
34-
Public Class DatabaseConnector
33+
Public Class MyStreamClass
3534
Implements IDisposable
3635

37-
Private _Connection As New SqlConnection
36+
Private _stream As New MemoryStream
3837

3938
Public Sub Dispose() Implements IDisposable.Dispose
4039
Dispose(True)
@@ -43,9 +42,9 @@ Namespace ca1816_2
4342

4443
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
4544
If disposing Then
46-
If _Connection IsNot Nothing Then
47-
_Connection.Dispose()
48-
_Connection = Nothing
45+
If _stream IsNot Nothing Then
46+
_stream.Dispose()
47+
_stream = Nothing
4948
End If
5049
End If
5150
End Sub

docs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca2100-review-sql-queries-for-security-vulnerabilities_1.vb

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
Imports System
2-
Imports System.Data
3-
Imports System.Data.SqlClient
1+
Imports System.Data
2+
Imports System.Data.OleDb
3+
Imports System.Runtime.Versioning
44

55
Namespace ca2100
66

77
Public Class SqlQueries
88

9+
<SupportedOSPlatform("windows")>
910
Function UnsafeQuery(connection As String,
1011
name As String, password As String) As Object
1112

12-
Dim someConnection As New SqlConnection(connection)
13-
Dim someCommand As New SqlCommand()
14-
someCommand.Connection = someConnection
15-
16-
someCommand.CommandText = "SELECT AccountNumber FROM Users " &
17-
"WHERE Username='" & name & "' AND Password='" & password & "'"
13+
Dim someConnection As New OleDbConnection(connection)
14+
Dim someCommand As New OleDbCommand With {
15+
.Connection = someConnection,
16+
.CommandText = "SELECT AccountNumber FROM Users " &
17+
"WHERE Username='" & name & "' AND Password='" & password & "'"
18+
}
1819

1920
someConnection.Open()
2021
Dim accountNumber As Object = someCommand.ExecuteScalar()
@@ -23,16 +24,18 @@ Namespace ca2100
2324

2425
End Function
2526

27+
<SupportedOSPlatform("windows")>
2628
Function SaferQuery(connection As String,
2729
name As String, password As String) As Object
2830

29-
Dim someConnection As New SqlConnection(connection)
30-
Dim someCommand As New SqlCommand()
31-
someCommand.Connection = someConnection
31+
Dim someConnection As New OleDbConnection(connection)
32+
Dim someCommand As New OleDbCommand With {
33+
.Connection = someConnection
34+
}
3235

33-
someCommand.Parameters.Add(
36+
someCommand.Parameters.AddWithValue(
3437
"@username", SqlDbType.NChar).Value = name
35-
someCommand.Parameters.Add(
38+
someCommand.Parameters.AddWithValue(
3639
"@password", SqlDbType.NChar).Value = password
3740
someCommand.CommandText = "SELECT AccountNumber FROM Users " &
3841
"WHERE Username=@username AND Password=@password"
@@ -48,6 +51,7 @@ Namespace ca2100
4851

4952
Class MaliciousCode
5053

54+
<SupportedOSPlatform("windows")>
5155
Shared Sub Main2100(args As String())
5256

5357
Dim queries As New SqlQueries()

0 commit comments

Comments
 (0)