Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle empty string parameter value in MetadataLoadContext #61457

Merged
merged 2 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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