Skip to content

Commit

Permalink
Handle empty string parameter value in MetadataLoadContext (#61457)
Browse files Browse the repository at this point in the history
  • Loading branch information
svick authored Nov 24, 2021
1 parent 40dc863 commit 3bfce3b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ internal static class EcmaDefaultValueProcessing
throw new BadImageFormatException();

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

BlobReader reader = metadataReader.GetBlobReader(constantValue.Value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ public void Foo3(int i = 42) { }
public void Foo4(short s = -34) { }
public void Foo5(decimal d = 1234m) { }
public void Foo6([DateTimeConstant(ticks: 8736726782)] DateTime dt) { }
public void Foo7(string s1 = "foo", string s2 = "", string s3 = null) { }
public void Foo8(Action a = null) { }
}

public class ParametersWithPseudoCustomtAttributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,32 @@ public static void TestRawDefaultValue6()
Assert.Equal(8736726782, ((DateTime)dv).Ticks);
}

[Fact]
public static void TestRawDefaultValue7()
{
var parameters = typeof(ParametersWithDefaultValues).Project().GetTypeInfo().GetDeclaredMethod("Foo7").GetParameters();
ParameterInfo p1 = parameters[0];
Assert.True(p1.HasDefaultValue);
object dv1 = p1.RawDefaultValue;
Assert.Equal("foo", dv1);
ParameterInfo p2 = parameters[1];
Assert.True(p2.HasDefaultValue);
object dv2 = p2.RawDefaultValue;
Assert.Equal("", dv2);
ParameterInfo p3 = parameters[2];
Assert.True(p3.HasDefaultValue);
Assert.Null(p3.RawDefaultValue);
}

[Fact]
public static void TestRawDefaultValue8()
{
ParameterInfo p = typeof(ParametersWithDefaultValues).Project().GetTypeInfo().GetDeclaredMethod("Foo8").GetParameters()[0];
Assert.True(p.HasDefaultValue);
object dv = p.RawDefaultValue;
Assert.Null(dv);
}

[Fact]
[ActiveIssue("https://github.com/mono/mono/issues/15340", TestRuntimes.Mono)]
public static void TestPseudoCustomAttributes()
Expand Down

0 comments on commit 3bfce3b

Please sign in to comment.