Skip to content

Commit c5237c3

Browse files
author
Elad Zelingher
committed
Disallow invalid ValueTuple types.
1 parent bd5d478 commit c5237c3

File tree

3 files changed

+47
-42
lines changed

3 files changed

+47
-42
lines changed

src/net45/WampSharp/Core/Utilities/ValueTuple/ValueTupleTypeExtensions.cs

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -40,56 +40,43 @@ public static int GetValueTupleLength(this Type type)
4040

4141
Type genericTypeDefinition = type.GetGenericTypeDefinition();
4242

43-
if (genericTypeDefinition == typeof(ValueTuple<>))
43+
if (!genericTypeDefinition.IsLongTuple())
4444
{
45-
return 1;
45+
return genericTypeDefinition.GetGenericArguments().Length;
4646
}
47-
if (genericTypeDefinition == typeof(ValueTuple<,>))
48-
{
49-
return 2;
50-
}
51-
if (genericTypeDefinition == typeof(ValueTuple<,,>))
52-
{
53-
return 3;
54-
}
55-
if (genericTypeDefinition == typeof(ValueTuple<,,,>))
56-
{
57-
return 4;
58-
}
59-
if (genericTypeDefinition == typeof(ValueTuple<,,,,>))
60-
{
61-
return 5;
62-
}
63-
if (genericTypeDefinition == typeof(ValueTuple<,,,,,>))
64-
{
65-
return 6;
66-
}
67-
if (genericTypeDefinition == typeof(ValueTuple<,,,,,,>))
68-
{
69-
return 7;
70-
}
71-
if (genericTypeDefinition == typeof(ValueTuple<,,,,,,,>))
47+
else
7248
{
7349
Type last = type.GetGenericArguments().Last();
7450

75-
if (!last.IsValueTuple())
76-
{
77-
return 8;
78-
}
79-
else
80-
{
81-
return 7 + last.GetValueTupleLength();
82-
}
51+
return (genericTypeDefinition.GetGenericArguments().Length - 1) +
52+
last.GetValueTupleLength();
8353
}
84-
85-
return 0;
8654
}
8755

8856
public static bool IsLongTuple(this Type tupleType)
8957
{
9058
return tupleType.IsGenericType() &&
91-
tupleType.GetGenericTypeDefinition() == typeof(ValueTuple<,,,,,,,>) &&
92-
tupleType.GetGenericArguments().Last().IsValueTuple();
59+
tupleType.GetGenericTypeDefinition() == typeof(ValueTuple<,,,,,,,>);
60+
}
61+
62+
public static bool IsValidTupleType(this Type tupleType)
63+
{
64+
if (tupleType.IsValueTuple())
65+
{
66+
if (tupleType.IsLongTuple())
67+
{
68+
Type rest = tupleType.GetGenericArguments().Last();
69+
70+
if (!rest.IsValueTuple())
71+
{
72+
return false;
73+
}
74+
}
75+
76+
return true;
77+
}
78+
79+
return false;
9380
}
9481

9582
public static IEnumerable<Type> GetValueTupleElementTypes(this Type tupleType)

src/net45/WampSharp/WAMP2/V2/Api/Rx/WampEventValueTupleConverter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ protected WampEventValueTupleConverter()
3737
throw new ArgumentException("Expected TTuple to be a ValueTuple");
3838
}
3939

40+
if (!tupleType.IsValidTupleType())
41+
{
42+
throw new ArgumentException("TTuple is an invalid ValueTuple. Expected TRest to be a ValueTuple.");
43+
}
44+
4045
mArrayConverter = ValueTupleArrayConverter<TTuple>.Value;
4146

4247
Type converterType = GetConverterType();

src/net45/WampSharp/WAMP2/V2/Rpc/Callee/Reflection/MethodInfoValidation.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ public static void ValidateTupleReturnType(MethodInfo method)
1414
{
1515
if (method.ReturnsTuple())
1616
{
17-
if (method.ReturnParameter.IsDefined(typeof(TupleElementNamesAttribute)))
17+
Type returnType = method.ReturnType;
18+
Type tupleType = TaskExtensions.UnwrapReturnType(returnType);
19+
20+
if (!tupleType.IsValidTupleType())
1821
{
19-
Type returnType = method.ReturnType;
20-
Type tupleType = TaskExtensions.UnwrapReturnType(returnType);
22+
ThrowHelper.MethodReturnsInvalidValueTuple(method);
23+
}
2124

25+
if (method.ReturnParameter.IsDefined(typeof(TupleElementNamesAttribute)))
26+
{
2227
int tupleLength = tupleType.GetValueTupleLength();
2328

2429
TupleElementNamesAttribute attribute =
@@ -157,6 +162,14 @@ public static void TupleReturnTypeAndOutRefParametersHaveCommonNames(MethodInfo
157162
method.Name, method.DeclaringType.FullName,
158163
string.Join(", ", intersection)));
159164
}
165+
166+
public static void MethodReturnsInvalidValueTuple(MethodInfo method)
167+
{
168+
throw new ArgumentException
169+
(String.Format(
170+
"Method {0} of type {1} returns an invalid ValueTuple. Expected TRest to be a ValueTuple.",
171+
method.Name, method.DeclaringType.FullName));
172+
}
160173
}
161174
}
162175
}

0 commit comments

Comments
 (0)