Skip to content

Commit 451c58f

Browse files
authored
Merge pull request castleproject#642 from stakx/bug/custom-attribute-with-null-ctor-arg
Fix `NullReferenceException` caused by `null` argument for custom attribute's array-typed ctor parameter
2 parents 323c03f + b6801c4 commit 451c58f

File tree

4 files changed

+60
-26
lines changed

4 files changed

+60
-26
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
Bugfixes:
6+
- Failure proxying type that has a non-inheritable custom attribute type applied where `null` argument is given for array parameter (@stakx, #637)
67
- Nested custom attribute types do not get replicated (@stakx, #638)
78

89
## 5.1.0 (2022-08-02)

src/Castle.Core.Tests/DynamicProxy.Tests/ClassWithAttributesTestCase.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
namespace Castle.DynamicProxy.Tests
1616
{
17+
using System;
1718
using System.IO;
1819
using System.Linq;
1920
using System.Reflection;
@@ -178,5 +179,22 @@ public void Can_proxy_type_with_non_inheritable_attribute_depending_on_array_of_
178179
.Cast<NonInheritableWithArray2Attribute>().Single();
179180
CollectionAssert.AreEqual(attribute.Values, new[] { "1", "2", "3" });
180181
}
182+
183+
[TestCase(typeof(HasAttributeWithNullAsPositionalArgument))]
184+
[TestCase(typeof(HasAttributeWithEmptyArrayAsPositionalArgument))]
185+
[TestCase(typeof(HasAttributeWithNullInArrayAsPositionalArgument))]
186+
public void Can_proxy_type_with_attribute_with_positional_array_parameter(Type classType)
187+
{
188+
_ = generator.CreateClassProxy(classType);
189+
}
190+
191+
[NonInheritableAttributeWithPositionalArrayParameter(null)]
192+
public class HasAttributeWithNullAsPositionalArgument{ }
193+
194+
[NonInheritableAttributeWithPositionalArrayParameter(new object[0])]
195+
public class HasAttributeWithEmptyArrayAsPositionalArgument { }
196+
197+
[NonInheritableAttributeWithPositionalArrayParameter(new object[1] { null })]
198+
public class HasAttributeWithNullInArrayAsPositionalArgument { }
181199
}
182200
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2004-2022 Castle Project - http://www.castleproject.org/
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
namespace Castle.DynamicProxy.Tests.Classes
16+
{
17+
using System;
18+
19+
#if FEATURE_SERIALIZATION
20+
[Serializable]
21+
#endif
22+
[AttributeUsage(AttributeTargets.All, Inherited = false)]
23+
public class NonInheritableAttributeWithPositionalArrayParameterAttribute : Attribute
24+
{
25+
public NonInheritableAttributeWithPositionalArrayParameterAttribute(object[] arg)
26+
{
27+
}
28+
}
29+
}

src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,7 @@ public static CustomAttributeInfo CreateInfo(CustomAttributeData attribute)
2828
{
2929
Debug.Assert(attribute != null, "attribute != null");
3030

31-
// .NET Core does not provide CustomAttributeData.Constructor, so we'll implement it
32-
// by finding a constructor ourselves
33-
Type[] constructorArgTypes;
34-
object[] constructorArgs;
35-
GetArguments(attribute.ConstructorArguments, out constructorArgTypes, out constructorArgs);
36-
var constructor = attribute.AttributeType.GetConstructor(constructorArgTypes);
31+
object[] constructorArgs = GetArguments(attribute.ConstructorArguments);
3732

3833
PropertyInfo[] properties;
3934
object[] propertyValues;
@@ -43,26 +38,14 @@ public static CustomAttributeInfo CreateInfo(CustomAttributeData attribute)
4338
attribute.AttributeType,
4439
attribute.NamedArguments, out properties, out propertyValues, out fields, out fieldValues);
4540

46-
return new CustomAttributeInfo(constructor,
41+
return new CustomAttributeInfo(attribute.Constructor,
4742
constructorArgs,
4843
properties,
4944
propertyValues,
5045
fields,
5146
fieldValues);
5247
}
5348

54-
private static void GetArguments(IList<CustomAttributeTypedArgument> constructorArguments,
55-
out Type[] constructorArgTypes, out object[] constructorArgs)
56-
{
57-
constructorArgTypes = new Type[constructorArguments.Count];
58-
constructorArgs = new object[constructorArguments.Count];
59-
for (var i = 0; i < constructorArguments.Count; i++)
60-
{
61-
constructorArgTypes[i] = constructorArguments[i].ArgumentType;
62-
constructorArgs[i] = ReadAttributeValue(constructorArguments[i]);
63-
}
64-
}
65-
6649
private static object[] GetArguments(IList<CustomAttributeTypedArgument> constructorArguments)
6750
{
6851
var arguments = new object[constructorArguments.Count];
@@ -77,15 +60,18 @@ private static object[] GetArguments(IList<CustomAttributeTypedArgument> constru
7760
private static object ReadAttributeValue(CustomAttributeTypedArgument argument)
7861
{
7962
var value = argument.Value;
80-
if (argument.ArgumentType.IsArray == false)
63+
64+
if (argument.ArgumentType.IsArray && value is IList<CustomAttributeTypedArgument> values)
8165
{
82-
return value;
66+
// `CustomAttributeInfo` represents array values as `ReadOnlyCollection<CustomAttributeTypedArgument>`,
67+
// but `CustomAttributeBuilder` will require plain arrays, so we need a (recursive) conversion:
68+
var arguments = GetArguments(values);
69+
var array = new object[arguments.Length];
70+
arguments.CopyTo(array, 0);
71+
return array;
8372
}
84-
//special case for handling arrays in attributes
85-
var arguments = GetArguments((IList<CustomAttributeTypedArgument>)value);
86-
var array = new object[arguments.Length];
87-
arguments.CopyTo(array, 0);
88-
return array;
73+
74+
return value;
8975
}
9076

9177
private static void GetSettersAndFields(Type attributeType, IEnumerable<CustomAttributeNamedArgument> namedArguments,

0 commit comments

Comments
 (0)