Skip to content

Commit 3bfce3b

Browse files
authored
Handle empty string parameter value in MetadataLoadContext (#61457)
1 parent 40dc863 commit 3bfce3b

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/Ecma/EcmaDefaultValueProcessing.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ internal static class EcmaDefaultValueProcessing
1515
throw new BadImageFormatException();
1616

1717
Constant constantValue = metadataReader.GetConstant(constantHandle);
18-
if (constantValue.Value.IsNil)
18+
// Partition II section 24.2.4:
19+
// The first entry in both these heaps is the empty 'blob' that consists of the single byte 0x00.
20+
//
21+
// This means zero value is valid for string and is used to represent the empty string.
22+
if (constantValue.Value.IsNil && constantValue.TypeCode != ConstantTypeCode.String)
1923
throw new BadImageFormatException();
2024

2125
BlobReader reader = metadataReader.GetBlobReader(constantValue.Value);

src/libraries/System.Reflection.MetadataLoadContext/tests/src/SampleMetadata/SampleMetadata.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ public void Foo3(int i = 42) { }
198198
public void Foo4(short s = -34) { }
199199
public void Foo5(decimal d = 1234m) { }
200200
public void Foo6([DateTimeConstant(ticks: 8736726782)] DateTime dt) { }
201+
public void Foo7(string s1 = "foo", string s2 = "", string s3 = null) { }
202+
public void Foo8(Action a = null) { }
201203
}
202204

203205
public class ParametersWithPseudoCustomtAttributes

src/libraries/System.Reflection.MetadataLoadContext/tests/src/Tests/Parameter/ParameterTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,32 @@ public static void TestRawDefaultValue6()
6666
Assert.Equal(8736726782, ((DateTime)dv).Ticks);
6767
}
6868

69+
[Fact]
70+
public static void TestRawDefaultValue7()
71+
{
72+
var parameters = typeof(ParametersWithDefaultValues).Project().GetTypeInfo().GetDeclaredMethod("Foo7").GetParameters();
73+
ParameterInfo p1 = parameters[0];
74+
Assert.True(p1.HasDefaultValue);
75+
object dv1 = p1.RawDefaultValue;
76+
Assert.Equal("foo", dv1);
77+
ParameterInfo p2 = parameters[1];
78+
Assert.True(p2.HasDefaultValue);
79+
object dv2 = p2.RawDefaultValue;
80+
Assert.Equal("", dv2);
81+
ParameterInfo p3 = parameters[2];
82+
Assert.True(p3.HasDefaultValue);
83+
Assert.Null(p3.RawDefaultValue);
84+
}
85+
86+
[Fact]
87+
public static void TestRawDefaultValue8()
88+
{
89+
ParameterInfo p = typeof(ParametersWithDefaultValues).Project().GetTypeInfo().GetDeclaredMethod("Foo8").GetParameters()[0];
90+
Assert.True(p.HasDefaultValue);
91+
object dv = p.RawDefaultValue;
92+
Assert.Null(dv);
93+
}
94+
6995
[Fact]
7096
[ActiveIssue("https://github.com/mono/mono/issues/15340", TestRuntimes.Mono)]
7197
public static void TestPseudoCustomAttributes()

0 commit comments

Comments
 (0)