Skip to content

Commit f2517d4

Browse files
Fix | NRE on assigning null to SqlConnectionStringBuilder.Encrypt (#1778)
1 parent f62ac3b commit f2517d4

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
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: 9 additions & 2 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
[Theory]
@@ -414,7 +418,7 @@ public void EncryptParserInvalidValuesThrowsException(string value)
414418
[InlineData("strict", "Strict")]
415419
public void EncryptTryParseValidValuesReturnsTrue(string value, string expectedValue)
416420
{
417-
Assert.True(SqlConnectionEncryptOption.TryParse(value, out var result));
421+
Assert.True(SqlConnectionEncryptOption.TryParse(value, out SqlConnectionEncryptOption result));
418422
Assert.Equal(expectedValue, result.ToString());
419423
}
420424

@@ -424,7 +428,10 @@ public void EncryptTryParseValidValuesReturnsTrue(string value, string expectedV
424428
[InlineData(null)]
425429
[InlineData(" true ")]
426430
public void EncryptTryParseInvalidValuesReturnsFalse(string value)
427-
=> Assert.False(SqlConnectionEncryptOption.TryParse(value, out _));
431+
{
432+
Assert.False(SqlConnectionEncryptOption.TryParse(value, out SqlConnectionEncryptOption result));
433+
Assert.Null(result);
434+
}
428435

429436
internal void ExecuteConnectionStringTests(string connectionString)
430437
{

0 commit comments

Comments
 (0)