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

4.4.1 - Fixing NRE in IsWebJobsAttribute #655

Merged
merged 3 commits into from
Jul 31, 2024
Merged

4.4.1 - Fixing NRE in IsWebJobsAttribute #655

merged 3 commits into from
Jul 31, 2024

Conversation

kshyju
Copy link
Member

@kshyju kshyju commented Jul 30, 2024

Fixes #641

  • Adding null check to the result of attribute.AttributeType?.Resolve() in IsWebJobsAttribute method, before calling other methods on the result of that.
  • Bumped patch version (new version is 4.4.1)

I also investigated the reason why the Resolve method was returning null when using nullable parameter.

The problem arises when the function parameter is nullable (e.g., string? id) in an in-process .NET 8.0 application. In such cases, the IsWebJobsAttribute method is called for System.Runtime.CompilerServices.NullableAttribute, and the attribute.AttributeType.Resolve() expression returns null, leading to a NullReferenceException.

This code is executed from Microsoft.NET.Sdk.Functions.Generator, which we start from the .NET 6.0 output (relevant code here). This causes the .NET 6 version of the System.Runtime module (among others) to be loaded. The NullableAttribute is not available in .NET 6, causing the Resolve method to return null. Discussed this with the team and we agreed that returning false when Resolve returns null is good enough to address the issue.

return attribute.AttributeType.Resolve().CustomAttributes.Any(a => a.AttributeType.FullName == "Microsoft.Azure.WebJobs.Description.BindingAttribute")
var attributeTypeDefinition = attribute.AttributeType?.Resolve();

if (attributeTypeDefinition == null)
Copy link
Contributor

@jviau jviau Jul 30, 2024

Choose a reason for hiding this comment

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

I am a fan of pattern matching, but no worries if you want to keep it as is:

if (attribute.AttributeType?.Resolve() is not { } attributeTypeDefinition)
{
    return false;
}

(may need to use actual type in place of { })

Copy link
Member

@fabiocav fabiocav left a comment

Choose a reason for hiding this comment

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

It would be good if we can have a simple set of tests against this method.

@kshyju
Copy link
Member Author

kshyju commented Jul 31, 2024

It would be good if we can have a simple set of tests against this method.

Updated E2E test to cover the scenario which caused NRE.

@kshyju kshyju merged commit 1876491 into main Jul 31, 2024
1 check passed
@kshyju kshyju deleted the shkr/fix_nre_gh641 branch July 31, 2024 16:57
fabiocav pushed a commit that referenced this pull request Jul 31, 2024
… method (#655)

* Adding null check to `IsWebJobsAttribute` extension method.

* Fix method summary.

* Updating E2E test to include a function with nullable parameter type.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NullReferenceException during build
3 participants