MongoAsyncEnumerableAdapter icon indicating copy to clipboard operation
MongoAsyncEnumerableAdapter copied to clipboard

Potential `NullReferenceException` at runtime

Open shocklateboy92 opened this issue 1 year ago • 1 comments

In ToAsyncEnumerable you've checked for null in: https://github.com/i3arnon/MongoAsyncEnumerableAdapter/blob/8539306221b4cfba0d77ba6589693a32d71a992e/src/MongoAsyncEnumerableAdapter/AsyncCursorExtensions.cs#L34

but that check won't get executed in Release builds. Which means in production you'll get a NullReferenceException from the ! in this line: https://github.com/i3arnon/MongoAsyncEnumerableAdapter/blob/8539306221b4cfba0d77ba6589693a32d71a992e/src/MongoAsyncEnumerableAdapter/AsyncCursorExtensions.cs#L36

You should always check for null at runtime and never use the ! operator.

The C# compiler is quite smart at figuring out when things can't be null. If you ever need to use the ! operator, there's a strong chance you've made a mistake somewhere and might get a NullReferenceException at runtime.

shocklateboy92 avatar May 03 '24 03:05 shocklateboy92

there's a strong chance you've made a mistake somewhere

So did I?

i3arnon avatar Jun 01 '24 12:06 i3arnon