Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Throw ArgumentException for invalid uses of global members in Expressions. - #15318

Merged
VSadov merged 4 commits into
dotnet:masterfrom
JonHanna:fix_12943
Jan 26, 2017
Merged

VSadov merged 4 commits into
dotnet:masterfrom
JonHanna:fix_12943

Conversation

@JonHanna

Copy link
Copy Markdown
Contributor

Several methods of Expression will throw NullReferenceException if called with a MethodInfo or FieldInfo for a global method or field. These exceptions are all thrown from validation of the (null) DeclaringType, so fix these validation methods to throw the same exception they would throw for a non-null but inappropriate DeclaringType.

Using a global field with a MemberBinding does not throw, but any attempt to use that binding will similarly throw NullReferenceException. Change to throw an ArgumentException immediately.

Fixes #12943

Using a global method with AndAlso or OrElse throws a
NullReferenceException.

Change this to throw the same ArgumentException thrown if the method
is not declared on a type with op_True and op_False methods, since it
isn't, so that is logically applicable.

Contributes to #12943
Using a global method with New throws a NullReferenceException.

Change this catch return false instead of throwing in
TypeUtils.AreEquivalent and TypeUtils.IsValidInstanceType, as clearly
the correct return value, resulting in ArgumentException being thrown
instead.

Contributes to #12943
Using a global method with Expression.Bind, Expression.ListBind or
Expression.MemberBind throws a NullReferenceException.

Change this by having global methods simply fail the test to check they
are property accessors, since they aren't, resulting in
ArgumentException.

Contributes to #12943
<value>'{0}' is not a member of type '{1}'</value>
</data>
<data name="NotAMemberOfAnyType" xml:space="preserve">
<value>'{0}' is not a member of any </value>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

last word missing (type)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oops. Thanks.

Using a global method with Expression.Bind, or Expression.MemberBind
returns a binding that will throw NullReferenceException on any attempt
to use it.

Change this to throw ArgumentException with the message:
… is not a member of any type

Contributes to #12943
public static bool AreEquivalent(Type t1, Type t2)
{
return t1.IsEquivalentTo(t2);
return t1 != null && t1.IsEquivalentTo(t2);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Type implements "==" operator. We just want a null check.
Would it be better to do something like: return t1?.IsEquivalentTo(t2) ?? false

{
return true;
}
if (targetType == null)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

it ((object)targetType == null) - just to make it cheaper by not using the "==" operator on Type

MethodInfo opTrue = TypeUtils.GetBooleanOperator(method.DeclaringType, "op_True");
MethodInfo opFalse = TypeUtils.GetBooleanOperator(method.DeclaringType, "op_False");
Type declaringType = method.DeclaringType;
if (declaringType == null)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

same thing about Type and "=="

Actually, It seems MethodInfo also has "==" implemented.
Perhaps it is worth going through and turn trivial null checks with Type/MemberInfo and the like into object equality or ?. operators. As a separate change of course.

@VSadov

VSadov commented Jan 26, 2017

Copy link
Copy Markdown
Member

LGTM

@VSadov
VSadov merged commit 74041f7 into dotnet:master Jan 26, 2017
@karelz karelz modified the milestone: 2.0.0 Feb 4, 2017
@JonHanna
JonHanna deleted the fix_12943 branch March 21, 2017 12:08
akoeplinger pushed a commit to mono/mono that referenced this pull request Jul 19, 2019
…ss (#15658)

`Expression.Bind` should throw `ArgumentException` if called with `FieldInfo` for a global field. The exception is thrown from validation of the (null) `DeclaringType` (see dotnet/corefx#15318).
Mono returns `"<Module>"` in this case and doesn't throw so the test fails. 

Fixes #14917 .
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
Throw ArgumentException for invalid uses of global members in Expressions.

Commit migrated from dotnet/corefx@74041f7
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

System.Linq.Expressions factories throw NullReferenceException with global fields and global methods

6 participants