Skip to content

Commit 7cdd2e9

Browse files
authored
Don't throw when deserializing SettingsPropertyValue that's not byte[] (#105057)
1 parent 2d17105 commit 7cdd2e9

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

src/libraries/System.Configuration.ConfigurationManager/src/System/Configuration/SettingsPropertyValue.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ private object Deserialize()
9595
{
9696
value = GetObjectFromString(Property.PropertyType, Property.SerializeAs, (string)SerializedValue);
9797
}
98-
else
98+
else if (SerializedValue is byte[] serializedBytes)
9999
{
100100
if (SettingsProperty.EnableUnsafeBinaryFormatterInPropertyValueSerialization)
101101
{
102-
using (MemoryStream ms = new MemoryStream((byte[])SerializedValue))
102+
using (MemoryStream ms = new MemoryStream(serializedBytes))
103103
{
104104
#pragma warning disable SYSLIB0011 // BinaryFormatter serialization is obsolete and should not be used.
105105
value = (new BinaryFormatter()).Deserialize(ms);

src/libraries/System.Configuration.ConfigurationManager/tests/System.Configuration.ConfigurationManager.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<DefineConstants>$(DefineConstants);LEGACY_GETRESOURCESTRING_USER</DefineConstants>
77
</PropertyGroup>
88
<ItemGroup>
9+
<Compile Include="$(CommonTestPath)TestUtilities\System\DisableParallelization.cs" Link="Common\TestUtilities\System\DisableParallelization.cs" />
910
<Compile Include="$(CommonTestPath)System\IO\TempDirectory.cs" Link="Common\System\IO\TempDirectory.cs" />
1011
<Compile Include="$(CommonTestPath)System\IO\TempFile.cs" Link="Common\System\IO\TempFile.cs" />
1112
<Compile Include="..\src\System\Configuration\ConfigPathUtility.cs" Link="Source\ConfigPathUtility.cs" />

src/libraries/System.Configuration.ConfigurationManager/tests/System/Configuration/LocalFileSettingsProviderTests.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
45
using System.Configuration;
56
using Xunit;
67

78
namespace System.ConfigurationTests
89
{
10+
[Collection(nameof(DisableParallelization))]
911
public class LocalFileSettingsProviderTests
1012
{
1113
private readonly SettingsContext _testContext = new SettingsContext
@@ -14,7 +16,6 @@ public class LocalFileSettingsProviderTests
1416
["SettingsKey"] = "SettingsKeyFoo"
1517
};
1618

17-
[ActiveIssue("https://github.com/dotnet/runtime/issues/29429")]
1819
[Fact]
1920
public void GetPropertyValues_NotStoredProperty_ValueEqualsNull()
2021
{
@@ -30,7 +31,6 @@ public void GetPropertyValues_NotStoredProperty_ValueEqualsNull()
3031
Assert.Null(propertyValues["PropertyName"].PropertyValue);
3132
}
3233

33-
[ActiveIssue("https://github.com/dotnet/runtime/issues/29429")]
3434
[Fact]
3535
public void GetPropertyValues_NotStoredConnectionStringProperty_ValueEqualsEmptyString()
3636
{
@@ -47,5 +47,32 @@ public void GetPropertyValues_NotStoredConnectionStringProperty_ValueEqualsEmpty
4747
Assert.Equal(1, propertyValues.Count);
4848
Assert.Equal(string.Empty, propertyValues["PropertyName"].PropertyValue);
4949
}
50+
51+
[Theory]
52+
[InlineData(true)]
53+
[InlineData(42)]
54+
[InlineData(867.5309)]
55+
[InlineData(StringComparison.Ordinal)]
56+
public void GetPropertyValues_DefaultValueApplied(object defaultValue)
57+
{
58+
var provider = new LocalFileSettingsProvider();
59+
var property = new SettingsProperty(
60+
"Test",
61+
defaultValue.GetType(),
62+
provider,
63+
false,
64+
defaultValue,
65+
SettingsSerializeAs.Xml,
66+
new SettingsAttributeDictionary(),
67+
false,
68+
false);
69+
property.Attributes.Add(typeof(UserScopedSettingAttribute), new UserScopedSettingAttribute());
70+
71+
var properties = new SettingsPropertyCollection() { property };
72+
var propertyValues = provider.GetPropertyValues(_testContext, properties);
73+
74+
Assert.Equal(1, propertyValues.Count);
75+
Assert.Equal(defaultValue, propertyValues["Test"].PropertyValue);
76+
}
5077
}
5178
}

0 commit comments

Comments
 (0)