Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implicit operator throws when checking ArraySegment != null or ArraySegment == null #8949

Closed
MindyStivers opened this issue Sep 15, 2017 · 8 comments
Assignees
Milestone

Comments

@MindyStivers
Copy link

Symptom

Using != or == operator to compare an ArraySegment to null throws an ArgumentNullException. For example:
if (new ArraySegment<byte>() == null)

Cause

The != and == operator trigger implicit conversion from an array to ArraySegment on null. The resulting call to the ArraySegment constructor with a null array parameter throws an ArgumentNullException:

public static implicit operator ArraySegment<T>(T[] array) => new ArraySegment<T>(array);
results in:
new ArraySegment<T>(null);
which throws ArgumentNullException.

dotnet/corefx#10528
dotnet/coreclr#9926

@benaadams
Copy link
Member

ArraySegment is a struct so can never actually be null; do you want to check if its array is null?

if (new ArraySegment<byte>().Array == null)

@MindyStivers
Copy link
Author

D'oh! This just started throwing out of the depths of an older test when we moved to .net core 2.0. Fair 'nuf that the answer is we have bad test code that was previously sneaking through.

I know the implicit operator docs say it should not throw so I wanted to make sure to follow up.

@benaadams
Copy link
Member

I do wonder what it will do for struct tests in generics e.g.

if (default(T) == null)
if (default(T) != null)

Where T is ArraySegment

@MindyStivers
Copy link
Author

The generic tests you suggested work fine when T is an ArraySegment. Perhaps worth noting that (although it's rather pointless) we can compare other value types == or != null without an exception.

@serkantkaraca
Copy link

serkantkaraca commented Oct 5, 2017

I think we are talking here about breaking the contract on null check on struct types. It doesn't really matter whether it makes sense or not to do a null check. Null check on struct compiles but throws an undescriptive NullRef which breaks the former behavior. We need to fix this.

Mirandatz referenced this issue in CoreOpenMMO/CoreOpenMMO Jan 8, 2018
@stephentoub stephentoub self-assigned this Feb 9, 2018
@ghost
Copy link

ghost commented Feb 9, 2018

Thanks @stephentoub and prioritizing it. Redirected from https://github.com/dotnet/corefx/issues/26987.

@jkotas
Copy link
Member

jkotas commented Feb 9, 2018

We should add a test to corefx (if there is not one already).

@stephentoub
Copy link
Member

We should add a test to corefx (if there is not one already).

I'll do so.

@msftgits msftgits transferred this issue from dotnet/coreclr Jan 31, 2020
@msftgits msftgits added this to the 2.1.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants