Skip to content
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
53 changes: 53 additions & 0 deletions src/libraries/System.Reflection/tests/CustomAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,58 @@ public void AttributeWithDifferentPropertyTypes()
Assert.Equal(1, attr.ObjectArray.Length);
Assert.Null(attr.StringArray);
}

public class StringValuedAttribute : Attribute
{
public StringValuedAttribute (string s)
{
NamedField = s;
}
public StringValuedAttribute () {}
public string NamedProperty
{
get => NamedField;
set { NamedField = value; }
}
public string NamedField;
}

internal class ClassWithAttrs
{
[StringValuedAttribute("")]
public void M1() {}

[StringValuedAttribute(NamedProperty = "")]
public void M2() {}

[StringValuedAttribute(NamedField = "")]
public void M3() {}
}

[Fact]
public void StringAttributeValueRefEqualsStringEmpty () {
StringValuedAttribute attr;
attr = typeof (ClassWithAttrs).GetMethod("M1")
.GetCustomAttributes(typeof(StringValuedAttribute), true)
.Cast<StringValuedAttribute>()
.Single();

Assert.Same(string.Empty, attr.NamedField);

attr = typeof (ClassWithAttrs).GetMethod("M2")
.GetCustomAttributes(typeof(StringValuedAttribute), true)
.Cast<StringValuedAttribute>()
.Single();

Assert.Same(string.Empty, attr.NamedField);


attr = typeof (ClassWithAttrs).GetMethod("M3")
.GetCustomAttributes(typeof(StringValuedAttribute), true)
.Cast<StringValuedAttribute>()
.Single();

Assert.Same(string.Empty, attr.NamedField);
}
}
}
6 changes: 5 additions & 1 deletion src/mono/mono/metadata/custom-attrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,11 @@ MONO_RESTORE_WARNING
// to decode some attributes in assemblies that Windows .NET Framework
// and CoreCLR both manage to decode.
// See https://simonsapin.github.io/wtf-8/ for a description of wtf-8.
*out_obj = (MonoObject*)mono_string_new_wtf8_len_checked (p, slen, error);
// Always use string.Empty for empty strings
if (slen == 0)
*out_obj = (MonoObject*)mono_string_empty_internal (mono_domain_get ());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: wouldn't mono_string_empty_wrapper be better?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that one is marked MONO_RT_EXTERNAL_ONLY so cannot be used in runtime code.

else
*out_obj = (MonoObject*)mono_string_new_wtf8_len_checked (p, slen, error);
return NULL;
}
case MONO_TYPE_CLASS: {
Expand Down