[release/6.0] [mono] Use string.Empty for empty string custom arg values (#58113)
authorgithub-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Fri, 27 Aug 2021 16:47:37 +0000 (18:47 +0200)
committerGitHub <noreply@github.com>
Fri, 27 Aug 2021 16:47:37 +0000 (18:47 +0200)
Co-authored-by: Ulrich Weigand <ulrich.weigand@de.ibm.com>
Co-authored-by: Aleksey Kliger <alklig@microsoft.com>
src/libraries/System.Reflection/tests/CustomAttributeTests.cs
src/mono/mono/metadata/custom-attrs.c

index bb9c928..6b10288 100644 (file)
@@ -76,5 +76,58 @@ namespace System.Reflection.Tests
             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);
+        }
     }
 }
index 48571da..29c64c4 100644 (file)
@@ -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 ());
+               else
+                       *out_obj = (MonoObject*)mono_string_new_wtf8_len_checked (p, slen, error);
                return NULL;
        }
        case MONO_TYPE_CLASS: {