dotnet / dotnet/vblang

Invalid CIL for chained null-conditional extension method calls, within an Async method.

Open
#340 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
328
Forks
71
PR merge metrics
No merged PRs in 30d

Description

I managed to crash the CLR today with the following line of code, inside an `Async` method.

```vb
Dim billDate = dateTimeString?.ParseDateTimeString()?.TruncateMilliseconds()
```

Both `ParseDateTimeString()` and `TruncateMilliseconds()` are extension methods, and after inspecting the IL, the issue is that the compiler essentially produces `ParseDateTimeString(this)` instead of `ParseDateTimeString(this.dateTimeString)` where `this` is the anonymous state machine object for the `Async` method.

The issue only occurs within an `Async` method, and only when a chain of at least 2 null-conditional invocations are involved. (changing to a non-null conditional, or removing the second invocation eliminates the issue). Also, the standard build options for "Release" mode do not produce the issue.

Full reproduction code: https://github.com/JakenVeina/dotnet-vblang-issue-340

The behavior in this example doesn't actually crash the CLR, as it did for me in the full original solution. However, it does produce a `FormatException` from `Date.Parse()` for an input string that clearly works in other circumstances. It appears that the CLR massages the invalid parameter to `ParseDateTimeString` into a `String` by calling `.ToString()` on the object, which of course is not a parse-able date string. In the full solution, `ParseDateTimeString()` happens to be in a C# assembly, and does not perform this extra conversion.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.