Skip to content

Commit ee27832

Browse files
[Release 5.0] Fix Null Reference Exception on assigning null to SqlConnectionStringBuilder.Encrypt (#1784)
* [Release 5.0] Fix Null Reference Exception on assigning null to SqlConnectionStringBuilder.Encrypt * Remove incompatible tests
1 parent 2d03ca3 commit ee27832

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

doc/snippets/Microsoft.Data.SqlClient/SqlConnectionEncryptOption.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<format type="text/markdown"><![CDATA[
88
99
## Remarks
10-
Implicit conversions have been added to maintain backwards compatibility with boolean behahavior for the <xref:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.Encrypt%2A> property. When converting from a boolean, a value of `true` converts to <xref:Microsoft.Data.SqlClient.SqlConnectionEncryptOption.Mandatory%2A> and a value of `false` converts to <xref:Microsoft.Data.SqlClient.SqlConnectionEncryptOption.Optional%2A>. When converting to a boolean, <xref:Microsoft.Data.SqlClient.SqlConnectionEncryptOption.Mandatory%2A> and <xref:Microsoft.Data.SqlClient.SqlConnectionEncryptOption.Strict%2A> convert to `true` and <xref:Microsoft.Data.SqlClient.SqlConnectionEncryptOption.Optional%2A> converts `false`.
10+
Implicit conversions have been added to maintain backwards compatibility with boolean behahavior for the <xref:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.Encrypt%2A> property. When converting from a boolean, a value of `true` converts to <xref:Microsoft.Data.SqlClient.SqlConnectionEncryptOption.Mandatory%2A> and a value of `false` converts to <xref:Microsoft.Data.SqlClient.SqlConnectionEncryptOption.Optional%2A>. When converting to a boolean, <xref:Microsoft.Data.SqlClient.SqlConnectionEncryptOption.Mandatory%2A>, <xref:Microsoft.Data.SqlClient.SqlConnectionEncryptOption.Strict%2A> , and `null` convert to `true` and <xref:Microsoft.Data.SqlClient.SqlConnectionEncryptOption.Optional%2A> converts `false`.
1111
1212
]]></format>
1313
</remarks>

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnectionStringBuilder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,8 +1237,9 @@ public SqlConnectionEncryptOption Encrypt
12371237
get => _encrypt;
12381238
set
12391239
{
1240-
SetSqlConnectionEncryptionValue(value);
1241-
_encrypt = value;
1240+
SqlConnectionEncryptOption newValue = value ?? DbConnectionStringDefaults.Encrypt;
1241+
SetSqlConnectionEncryptionValue(newValue);
1242+
_encrypt = newValue;
12421243
}
12431244
}
12441245

src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionStringBuilderTest.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,10 @@ public void ConnectionBuilderEncryptBackwardsCompatibility()
383383
builder.Encrypt = SqlConnectionEncryptOption.Strict;
384384
Assert.Equal("Encrypt=Strict", builder.ConnectionString);
385385
Assert.True(builder.Encrypt);
386+
387+
builder.Encrypt = null;
388+
Assert.Equal("Encrypt=True", builder.ConnectionString);
389+
Assert.True(builder.Encrypt);
386390
}
387391

388392
internal void ExecuteConnectionStringTests(string connectionString)

0 commit comments

Comments
 (0)